Skip to content

Commit 48946fd

Browse files
Tsenov/fix sauce lab comp (#118)
* fix: sauceLab compatibiity * fix: process termination * chore: donwgrade "mocha-multi": "^0.11.0", * update mocha multi * refactor: add system port
1 parent 897cebb commit 48946fd

8 files changed

+79
-71
lines changed

Diff for: index.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,27 @@ const appiumServer = new AppiumServer(nsCapabilities);
2525
let frameComparer: FrameComparer;
2626
let appiumDriver = null;
2727

28+
const attachToExitProcessHoockup = (processToExitFrom, processName ) => {
29+
const signals = ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
30+
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'];
31+
signals.forEach(function (sig) {
32+
processToExitFrom.once(sig, async function () {
33+
await killProcesses(sig);
34+
console.log(`Exited from ${processName}`);
35+
processToExitFrom.removeListener(sig, killProcesses);
36+
});
37+
});
38+
}
2839
export async function startServer(port?: number, deviceManager?: IDeviceManager) {
2940
await appiumServer.start(port || 8300, deviceManager);
30-
appiumServer.server.on("exit", async (code) => await killProcesses(code));
31-
appiumServer.server.on("close", async (code) => await killProcesses(code));
32-
appiumServer.server.on("SIGINT", async (code) => await killProcesses(code));
33-
appiumServer.server.on("error", async (code) => await killProcesses(code));
34-
appiumServer.server.on("uncaughtException", () => async (code) => await killProcesses(code));
35-
};
41+
await attachToExitProcessHoockup(appiumServer.server, "appium");
42+
}
3643

3744
export async function stopServer() {
38-
if (appiumDriver !== null && appiumDriver.isAlive) {
45+
if (appiumDriver && appiumDriver.isAlive) {
3946
await appiumDriver.quit();
4047
}
41-
if (appiumServer !== null && appiumServer.server && !appiumServer.server.killed) {
48+
if (appiumServer && appiumServer.server && !appiumServer.server.killed) {
4249
await appiumServer.stop();
4350
}
4451
};
@@ -73,7 +80,7 @@ export async function createDriver() {
7380
* Provide instance of FrameComparer in order to compare frames/ images from video
7481
* Please read carefully README.md before using it.
7582
* @throws exception in order the dependecies are not installed properly.
76-
*/
83+
*/
7784
export function loadFrameComparer() {
7885
if (!frameComparer) {
7986
frameComparer = frameComparerHelper.loadFrameComparer(nsCapabilities);
@@ -83,13 +90,12 @@ export function loadFrameComparer() {
8390
}
8491

8592
const killProcesses = async (code) => {
93+
console.log(`About to exit with code: ${code}`);
8694
if (appiumServer) {
87-
return await stopServer();
95+
await stopServer();
8896
}
8997
}
9098

91-
process.on("exit", async (code) => await killProcesses(code));
92-
process.on("close", async (code) => await killProcesses(code));
93-
process.on("SIGINT", async (code) => await killProcesses(code));
94-
process.on("error", async (code) => await killProcesses(code));
95-
process.on("uncaughtException", () => async (code) => await killProcesses(code));
99+
process.once("exit", async (code) => await killProcesses(code));
100+
101+
attachToExitProcessHoockup(process,"main process");

Diff for: lib/appium-driver.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,15 @@ export class AppiumDriver {
166166
}
167167

168168
driverConfig = "https://" + sauceUser + ":" + sauceKey + "@ondemand.saucelabs.com:443/wd/hub";
169-
170-
args.appiumCaps.app = "sauce-storage:" + args.appPath;
171-
console.log("Using Sauce Labs. The application path is changed to: " + args.appPath);
172169
}
173170

174171
log("Creating driver!", args.verbose);
175172

176-
args.appiumCaps['udid'] = args.appiumCaps['udid'] || args.device.token;
173+
args.appiumCaps['udid'] = args.appiumCaps['udid'] || (args.device.type === DeviceType.EMULATOR && !args.device.token.startsWith("emulator")) ? `emulator-${args.device.token}` : args.device.token;
177174
await AppiumDriver.applyAdditionalSettings(args);
178175
const _webio = webdriverio.remote({
179176
baseUrl: driverConfig.host,
180177
port: driverConfig.port,
181-
logLevel: 'warn',
182178
desiredCapabilities: args.appiumCaps
183179
});
184180

@@ -586,6 +582,10 @@ export class AppiumDriver {
586582
console.log(`args.appiumCaps['wdaLocalPort']: ${args.wdaLocalPort}`);
587583
args.appiumCaps["wdaLocalPort"] = args.wdaLocalPort;
588584
}
585+
} else {
586+
if (process.env["SYSTEM_PORT"]) {
587+
args.appiumCaps['systemPort'] = process.env["SYSTEM_PORT"];
588+
}
589589
}
590590
}
591591

Diff for: lib/appium-server.ts

+26-25
Original file line numberDiff line numberDiff line change
@@ -144,36 +144,37 @@ export class AppiumServer {
144144
const appPackage = this._args.isAndroid ? "appPackage" : "bundleId";
145145
const appFullPath = this._args.appiumCaps.app;
146146

147-
if (appFullPath && !this._args.appiumCaps[appPackage]) {
148-
console.log(`Trying to resolve automatically ${appPackage}!`);
149-
this._args.appiumCaps[appPackage] = this._deviceManager.getPackageId(this._args.device, appFullPath);
150-
console.log(`Setting capabilities ${this._args.runType}{ "${appPackage}" : "${this._args.appiumCaps[appPackage]}" }!`);
151-
}
147+
if (!this._args.ignoreDeviceController) {
148+
if (appFullPath && !this._args.appiumCaps[appPackage]) {
149+
console.log(`Trying to resolve automatically ${appPackage}!`);
150+
this._args.appiumCaps[appPackage] = this._deviceManager.getPackageId(this._args.device, appFullPath);
151+
console.log(`Setting capabilities ${this._args.runType}{ "${appPackage}" : "${this._args.appiumCaps[appPackage]}" }!`);
152+
}
152153

153-
const appActivityProp = "appActivity";
154-
if (this._args.isAndroid && appFullPath && !this._args.appiumCaps[appActivityProp]) {
155-
console.log(`Trying to resolve automatically ${appActivityProp}!`);
156-
this._args.appiumCaps[appActivityProp] = AndroidController.getLaunchableActivity(appFullPath);
157-
console.log(`Setting capabilities ${this._args.runType}{ "${appActivityProp} : "${this._args.appiumCaps[appActivityProp]}" }!`);
158-
}
154+
const appActivityProp = "appActivity";
155+
if (this._args.isAndroid && appFullPath && !this._args.appiumCaps[appActivityProp]) {
156+
console.log(`Trying to resolve automatically ${appActivityProp}!`);
157+
this._args.appiumCaps[appActivityProp] = AndroidController.getLaunchableActivity(appFullPath);
158+
console.log(`Setting capabilities ${this._args.runType}{ "${appActivityProp} : "${this._args.appiumCaps[appActivityProp]}" }!`);
159+
}
159160

160-
if (!this._args.appiumCaps[appPackage]) {
161-
throw new Error(`Please, provide ${appPackage} in ${this._args.appiumCapsLocation} file!`);
162-
}
161+
if (!this._args.appiumCaps[appPackage]) {
162+
throw new Error(`Please, provide ${appPackage} in ${this._args.appiumCapsLocation} file!`);
163+
}
163164

164-
if (this._args.isAndroid && !this._args.appiumCaps[appActivityProp]) {
165-
throw new Error(`Please, provide ${appActivityProp} in ${this._args.appiumCapsLocation} file!`);
166-
}
165+
if (this._args.isAndroid && !this._args.appiumCaps[appActivityProp]) {
166+
throw new Error(`Please, provide ${appActivityProp} in ${this._args.appiumCapsLocation} file!`);
167+
}
167168

168-
const groupings = getRegexResultsAsArray(/(\w+)/gi, this._args.appiumCaps[appPackage]);
169-
this._args.appName = groupings[groupings.length - 1];
170-
console.log(`Setting application name as ${this._args.appName}`);
171-
if (!this._args.devMode) {
172-
await this._deviceManager.uninstallApp(this._args);
173-
} else {
174-
this._args.appiumCaps.app = "";
169+
const groupings = getRegexResultsAsArray(/(\w+)/gi, this._args.appiumCaps[appPackage]);
170+
this._args.appName = groupings[groupings.length - 1];
171+
console.log(`Setting application name as ${this._args.appName}`);
172+
if (!this._args.devMode && !this._args.ignoreDeviceController) {
173+
await this._deviceManager.uninstallApp(this._args);
174+
} else {
175+
this._args.appiumCaps.app = "";
176+
}
175177
}
176-
177178
}
178179

179180
// Resolve appium dependency

Diff for: lib/capabilities-helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const sreachCapabilitiesByFolder = (location) => {
5656

5757
const seCapabilities = appiumCapabilitiesLocation => {
5858
const file = readFileSync(appiumCapabilitiesLocation);
59-
process.env.APPIUM_CAPABILITIES = file;
59+
process.env.APPIUM_CAPABILITIES = file.toString();
6060
utils.log("Capabilities found at: " + appiumCapabilitiesLocation, true);
6161
return file;
6262
}

Diff for: lib/device-controller.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export class DeviceManger implements IDeviceManager {
3131
args.appiumCaps.platformName = args.appiumCaps.platformName.toLowerCase();
3232
let device: IDevice = DeviceManger.getDefaultDevice(args);
3333
if (process.env["DEVICE_TOKEN"]) {
34+
args.ignoreDeviceController = true;
3435
device.token = process.env["DEVICE_TOKEN"];
36+
device.name = process.env["DEVICE_NAME"] || device.name;
3537
const allDevices = await DeviceController.getDevices({ platform: device.platform });
3638
const foundDevice = DeviceController.filter(allDevices, { token: device.token.replace("emulator-", "") })[0];
3739
console.log("Device: ", foundDevice);
@@ -93,9 +95,7 @@ export class DeviceManger implements IDeviceManager {
9395
}
9496

9597
public async stopDevice(args: INsCapabilities): Promise<any> {
96-
if (process.env["DEVICE_TOKEN"]) {
97-
return;
98-
} else if (DeviceManger._emulators.has(args.runType)
98+
if (DeviceManger._emulators.has(args.runType)
9999
&& !args.reuseDevice
100100
&& !args.isSauceLab
101101
&& !args.ignoreDeviceController) {

Diff for: lib/ns-capabilities.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export declare class NsCapabilities implements INsCapabilities {
4545
readonly isAndroid: any;
4646
readonly isIOS: any;
4747
readonly isSauceLab: any;
48-
appPath: string;
48+
readonly appPath: string;
4949
appName: string;
5050
readonly ignoreDeviceController: boolean;
5151
readonly wdaLocalPort: number;

Diff for: lib/ns-capabilities.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,22 @@ export class NsCapabilities implements INsCapabilities {
8181
set appName(appName: string) { this._appName = appName; }
8282
get ignoreDeviceController() { return this._ignoreDeviceController; }
8383
get wdaLocalPort() { return this._wdaLocalPort; }
84-
set appPath(appPath: string) { this._appPath = appPath; }
8584
get device() { return this._device; }
8685
set device(device: IDevice) { this._device = device; }
8786
get emulatorOptions() { return (this._emulatorOptions || "-wipe-data -gpu on") }
8887

8988
private isAndroidPlatform() { return this._appiumCaps.platformName.toLowerCase().includes("android"); }
9089

9190
private resolveApplication() {
92-
this._appiumCaps.app = getAppPath(this);
93-
this.appPath = this._appiumCaps.app;
94-
console.log("Application full path: " + this._appiumCaps.app);
91+
if (this.isSauceLab) {
92+
this._appiumCaps.app = `sauce-storage:${this.appPath}`
93+
this._ignoreDeviceController = true;
94+
console.log("Using Sauce Labs. The application path is changed to: " + this.appPath);
95+
} else {
96+
this.appiumCaps.app = getAppPath(this);
97+
this._appPath = this._appiumCaps.app;
98+
console.log("Application full path: " + this._appiumCaps.app);
99+
}
95100
}
96101

97102
private checkMandatoryCapabiliies() {

Diff for: package.json

+13-17
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
"test"
1919
],
2020
"maintainers": [
21-
{
22-
"name": "hdeshev",
23-
"email": "[email protected]"
24-
},
2521
{
2622
"name": "SvetoslavTsenov",
2723
"email": "[email protected]"
@@ -30,24 +26,24 @@
3026
"dependencies": {
3127
"app-root-path": "^2.0.1",
3228
"blink-diff": "^1.0.13",
33-
"chai": "~4.1.1",
34-
"chai-as-promised": "~7.1.1",
29+
"chai": "^4.1.0",
30+
"chai-as-promised": "^7.1.0",
3531
"frame-comparer": "^1.0.3",
36-
"glob": "*",
37-
"mobile-devices-controller": "^2.0.1",
38-
"mocha": "~3.5.0",
39-
"mocha-junit-reporter": "^1.13.0",
40-
"mocha-multi": "^0.11.0",
41-
"wd": "^1.4.1",
42-
"webdriverio": "~4.8.0",
32+
"glob": "7.1.0",
33+
"mobile-devices-controller": "^2.4.0",
34+
"mocha": "^5.1.0",
35+
"mocha-junit-reporter": "^1.17.0",
36+
"mocha-multi": "^1.0.0",
37+
"wd": "^1.6.0",
38+
"webdriverio": "^4.12.0",
4339
"yargs": "^8.0.2"
4440
},
4541
"devDependencies": {
46-
"typescript": "^2.5.0",
47-
"@types/node": "^7.0.5",
42+
"@types/chai": "^4.1.3",
43+
"@types/mocha": "^5.2.0",
44+
"@types/node": "^8.10.13",
4845
"@types/webdriverio": "~4.8.4",
49-
"@types/chai": "^4.0.2",
50-
"@types/mocha": "^2.2.41"
46+
"typescript": "^2.8.0"
5147
},
5248
"scripts": {
5349
"postinstall": "node ./postinstall.js",

0 commit comments

Comments
 (0)