Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c5af031

Browse files
authoredJan 4, 2018
fix: get app path automatically (#79)
* fix: get app path automatically Fix to get app path automatically when the tests are called from parent directory.
1 parent 26ea5f2 commit c5af031

File tree

7 files changed

+48
-20
lines changed

7 files changed

+48
-20
lines changed
 

‎index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const appiumServer = new AppiumServer(nsCapabilities);
2323
let appiumDriver = null;
2424
export async function startServer(port?: number, deviceManager?: IDeviceManager) {
2525
await appiumServer.start(port || 8300, deviceManager);
26+
appiumServer.server.on("exit", async (code) => await killProcesses(code));
27+
appiumServer.server.on("close", async (code) => await killProcesses(code));
28+
appiumServer.server.on("SIGINT", async (code) => await killProcesses(code));
29+
appiumServer.server.on("error", async (code) => await killProcesses(code));
30+
appiumServer.server.on("uncaughtException", () => async (code) => await killProcesses(code));
2631
};
2732

2833
export async function stopServer() {

‎lib/appium-driver.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,17 +427,23 @@ export class AppiumDriver {
427427
let hasStarted = false;
428428
let retries = 10;
429429
while (retries > 0 && !hasStarted) {
430+
let test = '';
430431
try {
431-
const test = await driver.init(args.appiumCaps);
432+
test = await driver.init(args.appiumCaps);
432433
hasStarted = true;
433434
} catch (error) {
435+
console.log(error);
436+
console.log("Rety with new wdaLocalPort!");
434437
if (error && error.message && error.message.includes("WebDriverAgent")) {
435438
let freePort = await findFreePort(10, args.appiumCaps.port, args);
436439
console.log(" args.appiumCaps['wdaLocalPort']", freePort)
437440
args.appiumCaps["wdaLocalPort"] = freePort;
438441
}
439-
console.log(error);
440442
}
443+
if (hasStarted) {
444+
console.log("Appium driver has started successfully!");
445+
}
446+
441447
retries--;
442448
}
443449

‎lib/device-controller.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export class DeviceManger implements IDeviceManager {
3333

3434
public async startDevice(args: INsCapabilities): Promise<IDevice> {
3535
let device: IDevice = DeviceManger.getDefaultDevice(args);
36+
if (process.env["DEVICE_TOKEN"]) {
37+
device.token = process.env["DEVICE_TOKEN"];
38+
console.log("Device", device);
39+
return device;
40+
}
3641
// When isSauceLab specified we simply do nothing;
3742
if (args.isSauceLab || args.ignoreDeviceController) {
3843
DeviceManger._emulators.set(args.runType, device);
@@ -43,12 +48,13 @@ export class DeviceManger implements IDeviceManager {
4348
// Using serve to manage deivces.
4449
if (args.useDeviceControllerServer) {
4550
const d = await this._serveiceContext.subscribe(args.appiumCaps.deviceName, args.appiumCaps.platformName.toLowerCase(), args.appiumCaps.platformVersion, args.appiumCaps.app);
51+
delete d['__v'];
52+
delete d['_id']
4653
if (!d || !(d as IDevice)) {
4754
console.error("", d);
4855
throw new Error("Missing device: " + d);
4956
}
50-
51-
DeviceManger._emulators.set(args.runType, d);
57+
console.log(`Device:`, d);
5258

5359
return d;
5460
}
@@ -102,10 +108,11 @@ export class DeviceManger implements IDeviceManager {
102108
}
103109

104110
public async stopDevice(args: INsCapabilities) {
111+
if (process.env["DEVICE_TOKEN"]) {
112+
return;
113+
}
105114
if (args.useDeviceControllerServer) {
106-
const device = DeviceManger._emulators.get(args.runType);
107-
108-
const d = await this._serveiceContext.unsubscribe(device.token);
115+
const d = await this._serveiceContext.unsubscribe(args.device.token);
109116
if (!d) {
110117
console.error("", d);
111118
throw new Error("Missing device: " + d);

‎lib/ns-capabilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class NsCapabilities implements INsCapabilities {
8888
}
8989

9090
if (!this._appiumCaps.app) {
91-
this._appiumCaps.app = getAppPath(this._appiumCaps.platformName.toLowerCase(), this._runType.toLowerCase());
91+
this._appiumCaps.app = getAppPath(this);
9292
}
9393
console.log("Application full path: " + this._appiumCaps.app);
9494
}

‎lib/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ const config = (() => {
2929
.argv;
3030

3131
let appRootPath = options.path;
32-
if (appRootPath.includes("nativescript-dev-appium") || appRootPath.includes("mocha")) {
32+
if (appRootPath.includes("nativescript-dev-appium")) {
3333
appRootPath = require('app-root-path').toString();
3434
}
3535

36+
if (appRootPath.includes("mocha")) {
37+
appRootPath = join(appRootPath, "../../..");
38+
}
39+
3640
const projectDir = appRootPath;
3741
const projectBinary = resolve(projectDir, "node_modules", ".bin");
3842
const pluginRoot = resolve(projectDir, "node_modules", "nativescript-dev-appium");

‎lib/utils.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export declare function isWin(): boolean;
2020
export declare function getStorageByDeviceName(args: INsCapabilities): string;
2121
export declare function getStorageByPlatform(args: INsCapabilities): string;
2222
export declare function getReportPath(args: INsCapabilities): string;
23-
export declare function getAppPath(platform: any, runType: any): any;
23+
export declare function getAppPath(caps: INsCapabilities): any;
2424
export declare function calculateOffset(direction: any, y: number, yOffset: number, x: number, xOffset: number, isIOS: boolean, verbose: any): {
2525
point: Point;
2626
duration: number;

‎lib/utils.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,25 +287,31 @@ function getAppName(args: INsCapabilities) {
287287
return appName;
288288
}
289289

290-
export function getAppPath(platform, runType) {
291-
if (platform.includes("android")) {
290+
export function getAppPath(caps: INsCapabilities) {
291+
if (caps.appiumCaps.platformName.toLowerCase().includes("android")) {
292292
//platforms/android/build/outputs/apk/
293293
//platforms/android/app/build/outputs/apk
294294
let apks = glob.sync("platforms/android/build/outputs/apk/*.apk").filter(function (file) { return file.indexOf("unaligned") < 0; });
295295
if (!apks || apks.length === 0) {
296296
apks = glob.sync("platforms/android/app/build/outputs/apk/*.apk").filter(function (file) { return file.indexOf("unaligned") < 0; });
297297
}
298+
if (!apks || apks.length === 0) {
299+
apks = glob.sync(`${caps.projectDir}/platforms/android/app/build/outputs/apk/*.apk`).filter(function (file) { return file.indexOf("unaligned") < 0; });
300+
}
298301
return apks[0];
299-
} else if (platform.includes("ios")) {
300-
if (runType.includes("sim")) {
301-
const simulatorApps = glob.sync("platforms/ios/build/emulator/**/*.app");
302-
return simulatorApps[0];
303-
} else if (runType.includes("device")) {
304-
const deviceApps = glob.sync("platforms/ios/build/device/**/*.ipa");
305-
return deviceApps[0];
302+
} else if (caps.appiumCaps.platformName.toLowerCase().includes("ios")) {
303+
let path = "platforms/ios/build/emulator/**/*.app";
304+
if (caps.runType.includes("device")) {
305+
path = "platforms/ios/build/device/**/*.ipa";
306+
}
307+
308+
let apps = glob.sync("platforms/ios/build/device/**/*.ipa");
309+
if (!apps || apps.length === 0) {
310+
apps = glob.sync(`${caps.projectDir}/${path}`).filter(function (file) { return file.indexOf("unaligned") < 0; });
306311
}
312+
return apps[0];
307313
} else {
308-
throw new Error("No 'app' capability provided and incorrect 'runType' convention used: " + runType +
314+
throw new Error("No 'app' capability provided and incorrect 'runType' convention used: " + caps.runType +
309315
". 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");
310316
}
311317
}

0 commit comments

Comments
 (0)
Please sign in to comment.