Skip to content

fix: get app path automatically #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 8 additions & 2 deletions lib/appium-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,23 @@ 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);
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!");
}

retries--;
}

Expand Down
17 changes: 12 additions & 5 deletions lib/device-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export class DeviceManger implements IDeviceManager {

public async startDevice(args: INsCapabilities): Promise<IDevice> {
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);
Expand All @@ -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);
}

DeviceManger._emulators.set(args.runType, d);
console.log(`Device:`, d);

return d;
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/ns-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 16 additions & 10 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down