From 3ecade8f9ff02225426372163ac09f2729ada738 Mon Sep 17 00:00:00 2001 From: SvetoslavTsenov Date: Thu, 4 Jan 2018 12:05:38 +0200 Subject: [PATCH 1/4] fix: get app path automatically Fix to get app path automatically when the test are called from parent directory. --- lib/device-controller.ts | 2 +- lib/ns-capabilities.ts | 2 +- lib/parser.ts | 6 +++++- lib/utils.d.ts | 2 +- lib/utils.ts | 26 ++++++++++++++++---------- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lib/device-controller.ts b/lib/device-controller.ts index 9b51e4b..256b3a3 100644 --- a/lib/device-controller.ts +++ b/lib/device-controller.ts @@ -47,7 +47,7 @@ export class DeviceManger implements IDeviceManager { console.error("", d); throw new Error("Missing device: " + d); } - + console.log(`Device: ${d}`) DeviceManger._emulators.set(args.runType, d); return d; diff --git a/lib/ns-capabilities.ts b/lib/ns-capabilities.ts index e123714..77315e5 100644 --- a/lib/ns-capabilities.ts +++ b/lib/ns-capabilities.ts @@ -88,7 +88,7 @@ export class NsCapabilities implements INsCapabilities { } if (!this._appiumCaps.app) { - this._appiumCaps.app = getAppPath(this._appiumCaps.platformName.toLowerCase(), this._runType.toLowerCase()); + this._appiumCaps.app = getAppPath(this); } console.log("Application full path: " + this._appiumCaps.app); } diff --git a/lib/parser.ts b/lib/parser.ts index ff19f22..c23cbf1 100644 --- a/lib/parser.ts +++ b/lib/parser.ts @@ -29,10 +29,14 @@ const config = (() => { .argv; let appRootPath = options.path; - if (appRootPath.includes("nativescript-dev-appium") || appRootPath.includes("mocha")) { + if (appRootPath.includes("nativescript-dev-appium")) { appRootPath = require('app-root-path').toString(); } + if (appRootPath.includes("mocha")) { + appRootPath = join(appRootPath, "../../.."); + } + const projectDir = appRootPath; const projectBinary = resolve(projectDir, "node_modules", ".bin"); const pluginRoot = resolve(projectDir, "node_modules", "nativescript-dev-appium"); diff --git a/lib/utils.d.ts b/lib/utils.d.ts index b35be8b..29a3c4a 100644 --- a/lib/utils.d.ts +++ b/lib/utils.d.ts @@ -20,7 +20,7 @@ export declare function isWin(): boolean; export declare function getStorageByDeviceName(args: INsCapabilities): string; export declare function getStorageByPlatform(args: INsCapabilities): string; export declare function getReportPath(args: INsCapabilities): string; -export declare function getAppPath(platform: any, runType: any): any; +export declare function getAppPath(caps: INsCapabilities): any; export declare function calculateOffset(direction: any, y: number, yOffset: number, x: number, xOffset: number, isIOS: boolean, verbose: any): { point: Point; duration: number; diff --git a/lib/utils.ts b/lib/utils.ts index 8ef59ea..7765ed9 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -287,25 +287,31 @@ function getAppName(args: INsCapabilities) { return appName; } -export function getAppPath(platform, runType) { - if (platform.includes("android")) { +export function getAppPath(caps: INsCapabilities) { + if (caps.appiumCaps.platformName.toLowerCase().includes("android")) { //platforms/android/build/outputs/apk/ //platforms/android/app/build/outputs/apk let apks = glob.sync("platforms/android/build/outputs/apk/*.apk").filter(function (file) { return file.indexOf("unaligned") < 0; }); if (!apks || apks.length === 0) { apks = glob.sync("platforms/android/app/build/outputs/apk/*.apk").filter(function (file) { return file.indexOf("unaligned") < 0; }); } + if (!apks || apks.length === 0) { + apks = glob.sync(`${caps.projectDir}/platforms/android/app/build/outputs/apk/*.apk`).filter(function (file) { return file.indexOf("unaligned") < 0; }); + } return apks[0]; - } else if (platform.includes("ios")) { - if (runType.includes("sim")) { - const simulatorApps = glob.sync("platforms/ios/build/emulator/**/*.app"); - return simulatorApps[0]; - } else if (runType.includes("device")) { - const deviceApps = glob.sync("platforms/ios/build/device/**/*.ipa"); - return deviceApps[0]; + } else if (caps.appiumCaps.platformName.toLowerCase().includes("ios")) { + let path = "platforms/ios/build/emulator/**/*.app"; + if (caps.runType.includes("device")) { + path = "platforms/ios/build/device/**/*.ipa"; + } + + let apps = glob.sync("platforms/ios/build/device/**/*.ipa"); + if (!apps || apps.length === 0) { + apps = glob.sync(`${caps.projectDir}/${path}`).filter(function (file) { return file.indexOf("unaligned") < 0; }); } + return apps[0]; } else { - throw new Error("No 'app' capability provided and incorrect 'runType' convention used: " + runType + + throw new Error("No 'app' capability provided and incorrect 'runType' convention used: " + caps.runType + ". In order to automatically search and locate app package please use 'android','device','sim' in your 'runType' option. E.g --runType android25, --runType sim.iPhone7.iOS110"); } } From 2edeaeedbe4c30cda365844f39b72deba6014480 Mon Sep 17 00:00:00 2001 From: SvetoslavTsenov Date: Thu, 4 Jan 2018 21:55:40 +0200 Subject: [PATCH 2/4] chore: DEVICE_TOKEN --- index.ts | 5 +++++ lib/appium-driver.ts | 7 ++++++- lib/device-controller.ts | 17 ++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/index.ts b/index.ts index f8392a3..1b0143c 100644 --- a/index.ts +++ b/index.ts @@ -23,6 +23,11 @@ const appiumServer = new AppiumServer(nsCapabilities); let appiumDriver = null; export async function startServer(port?: number, deviceManager?: IDeviceManager) { await appiumServer.start(port || 8300, deviceManager); + appiumServer.server.on("exit", async (code) => await killProcesses(code)); + appiumServer.server.on("close", async (code) => await killProcesses(code)); + appiumServer.server.on("SIGINT", async (code) => await killProcesses(code)); + appiumServer.server.on("error", async (code) => await killProcesses(code)); + appiumServer.server.on("uncaughtException", () => async (code) => await killProcesses(code)); }; export async function stopServer() { diff --git a/lib/appium-driver.ts b/lib/appium-driver.ts index d42efce..ef7e323 100644 --- a/lib/appium-driver.ts +++ b/lib/appium-driver.ts @@ -431,13 +431,18 @@ export class AppiumDriver { const test = await driver.init(args.appiumCaps); hasStarted = true; } catch (error) { + console.log(error); + console.log("Rety with new wdaLocalPort!"); if (error && error.message && error.message.includes("WebDriverAgent")) { let freePort = await findFreePort(10, args.appiumCaps.port, args); console.log(" args.appiumCaps['wdaLocalPort']", freePort) args.appiumCaps["wdaLocalPort"] = freePort; } - console.log(error); } + if (hasStarted) { + console.log("Appium driver has started successfully!", test); + } + retries--; } diff --git a/lib/device-controller.ts b/lib/device-controller.ts index 256b3a3..6a00320 100644 --- a/lib/device-controller.ts +++ b/lib/device-controller.ts @@ -33,6 +33,11 @@ export class DeviceManger implements IDeviceManager { public async startDevice(args: INsCapabilities): Promise { let device: IDevice = DeviceManger.getDefaultDevice(args); + if (process.env["DEVICE_TOKEN"]) { + device.token = process.env["DEVICE_TOKEN"]; + console.log("Device", device); + return device; + } // When isSauceLab specified we simply do nothing; if (args.isSauceLab || args.ignoreDeviceController) { DeviceManger._emulators.set(args.runType, device); @@ -43,12 +48,13 @@ export class DeviceManger implements IDeviceManager { // Using serve to manage deivces. if (args.useDeviceControllerServer) { const d = await this._serveiceContext.subscribe(args.appiumCaps.deviceName, args.appiumCaps.platformName.toLowerCase(), args.appiumCaps.platformVersion, args.appiumCaps.app); + delete d['__v']; + delete d['_id'] if (!d || !(d as IDevice)) { console.error("", d); throw new Error("Missing device: " + d); } - console.log(`Device: ${d}`) - DeviceManger._emulators.set(args.runType, d); + console.log(`Device:`, d); return d; } @@ -102,10 +108,11 @@ export class DeviceManger implements IDeviceManager { } public async stopDevice(args: INsCapabilities) { + if (process.env["DEVICE_TOKEN"]) { + return; + } if (args.useDeviceControllerServer) { - const device = DeviceManger._emulators.get(args.runType); - - const d = await this._serveiceContext.unsubscribe(device.token); + const d = await this._serveiceContext.unsubscribe(args.device.token); if (!d) { console.error("", d); throw new Error("Missing device: " + d); From fb1d09de768eba093410412cd9011bc3cc4ce21e Mon Sep 17 00:00:00 2001 From: SvetoslavTsenov Date: Thu, 4 Jan 2018 22:12:58 +0200 Subject: [PATCH 3/4] chore: fix test variable --- lib/appium-driver.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/appium-driver.ts b/lib/appium-driver.ts index ef7e323..dfb9c08 100644 --- a/lib/appium-driver.ts +++ b/lib/appium-driver.ts @@ -427,8 +427,9 @@ export class AppiumDriver { let hasStarted = false; let retries = 10; while (retries > 0 && !hasStarted) { + let test = ''; try { - const test = await driver.init(args.appiumCaps); + test = await driver.init(args.appiumCaps); hasStarted = true; } catch (error) { console.log(error); From c5b805ab89106ee4265b1d523a9d7992338a4e7e Mon Sep 17 00:00:00 2001 From: SvetoslavTsenov Date: Thu, 4 Jan 2018 22:19:27 +0200 Subject: [PATCH 4/4] chore: update log --- lib/appium-driver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/appium-driver.ts b/lib/appium-driver.ts index dfb9c08..83fb8c1 100644 --- a/lib/appium-driver.ts +++ b/lib/appium-driver.ts @@ -441,7 +441,7 @@ export class AppiumDriver { } } if (hasStarted) { - console.log("Appium driver has started successfully!", test); + console.log("Appium driver has started successfully!"); } retries--;