forked from nightwatchjs/nightwatch
/
appAutomate.js
87 lines (69 loc) · 2.15 KB
/
appAutomate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const path = require('path');
const Utils = require('../../../utils');
const BrowserStack = require('./browserstack.js');
const {Logger} = Utils;
class AppAutomate extends BrowserStack {
get ApiUrl() {
return `https://api.browserstack.com/${this.productNamespace}`;
}
get productNamespace() {
return 'app-automate';
}
createSessionOptions() {
this.extractAppiumOptions();
return this.desiredCapabilities;
}
async uploadAppToBrowserStack(options) {
const {appUploadPath, appUploadUrl} = options;
const multiPartFormData = {};
if (appUploadPath) {
multiPartFormData['file'] = {
filePath: path.resolve(appUploadPath)
};
} else if (appUploadUrl) {
multiPartFormData['url'] = {
data: appUploadUrl
};
}
if (options['appium:app'] && !options['appium:app'].startsWith('bs://')) {
multiPartFormData['custom_id'] = {
data: options['appium:app']
};
}
// eslint-disable-next-line no-console
console.log(`Uploading app to BrowserStack from '${appUploadPath || appUploadUrl}'...`);
try {
const response = await this.sendHttpRequest({
url: 'https://api-cloud.browserstack.com/app-automate/upload',
method: 'POST',
use_ssl: true,
auth: {
user: this.username,
pass: this.accessKey
},
multiPartFormData
});
if (response.error) {
Logger.error(response.error);
return;
}
// eslint-disable-next-line no-console
console.log(Logger.colors.green(Utils.symbols.ok), 'App uploaded successfully:', response, '\n');
if (response.app_url && !response.custom_id) {
// custom_id not being used
options['appium:app'] = response.app_url;
// to display url when test suite is finished
this.uploadedAppUrl = response.app_url;
}
} catch (err) {
Logger.error(err);
}
}
async createDriver({options}) {
if (options && (options.appUploadUrl || options.appUploadPath)) {
await this.uploadAppToBrowserStack(options);
}
return this.createAppiumDriver({options});
}
}
module.exports = AppAutomate;