Skip to content

Commit d5b773e

Browse files
refactor(devMode): use android/ ios or --device.[prop] as option instead devMode (#192)
* refactor(devMode): replace with ios or android as a first option. BREAKING CHANGES: validate() args returns a promise
1 parent 0bf4e79 commit d5b773e

14 files changed

+229
-113
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ samples/reports
1212
!samples/config
1313
!samples/e2e-tsc/*.ts
1414
!samples/e2e-js/*.js
15-
!samples/e2e-tsc/tsconfig.json
15+
!samples/e2e-tsc/tsconfig.json
16+
test/out/*

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export async function createDriver(args?: INsCapabilitiesArgs) {
9494
if (!nsCapabilities.appiumCapsLocation) {
9595
throw new Error("Provided path to appium capabilities is not correct!");
9696
}
97-
if (!nsCapabilities.runType) {
97+
if (!nsCapabilities.runType && !nsCapabilities.appiumCaps) {
9898
throw new Error("--runType is missing! Make sure it is provided correctly! It is used to parse the configuration for appium driver!");
9999
}
100100

lib/appium-driver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
DeviceController,
1717
IDevice,
1818
DeviceType,
19-
AndroidController
19+
AndroidController,
20+
killAllProcessAndRelatedCommand
2021
} from "mobile-devices-controller";
2122
import {
2223
addExt,
@@ -189,7 +190,7 @@ export class AppiumDriver {
189190
};
190191

191192
if (!args.isValidated) {
192-
args.validateArgs();
193+
await args.validateArgs();
193194
}
194195

195196
if (args.isSauceLab) {

lib/appium-server.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { INsCapabilities } from "./interfaces/ns-capabilities";
1515
import { IDeviceManager } from "./interfaces/device-manager";
1616
import { DeviceManager } from "./device-manager";
1717
import { existsSync } from "fs";
18+
import { killAllProcessAndRelatedCommand } from "mobile-devices-controller";
1819

1920
export class AppiumServer {
2021
private _server: child_process.ChildProcess;
@@ -59,12 +60,12 @@ export class AppiumServer {
5960
}
6061

6162
public async start(port, deviceManager: IDeviceManager = new DeviceManager()) {
62-
if (!this._args.isValidated) {
63-
this._args.validateArgs();
63+
if (!this._args.isValidated && this._args.validateArgs) {
64+
await this._args.validateArgs();
6465
this._args.port = port;
6566
}
6667
this._args.deviceManager = deviceManager;
67-
if (!this._args.attachToDebug && !this._args.sessionId) {
68+
if (this._args.isValidated && !this._args.attachToDebug && !this._args.sessionId) {
6869
await this.prepDevice(deviceManager);
6970
await this.prepApp();
7071
}
@@ -84,6 +85,9 @@ export class AppiumServer {
8485

8586
let retries = 11;
8687
while (retries > 0 && !response) {
88+
if (retries < 5) {
89+
killAllProcessAndRelatedCommand(this._port);
90+
}
8791
retries--;
8892
this.port += 10;
8993
this.port = (await findFreePort(100, this.port));
@@ -109,10 +113,7 @@ export class AppiumServer {
109113

110114
logInfo(`Server args: `, startingServerArgs);
111115

112-
this._server = child_process.spawn(this._appium, startingServerArgs, {
113-
shell: true,
114-
detached: false
115-
});
116+
this._server = child_process.spawn(this._appium, startingServerArgs);
116117
}
117118

118119
public async stop() {
@@ -124,22 +125,22 @@ export class AppiumServer {
124125
await this._args.deviceManager.stopDevice(this._args.device, this._args);
125126
return new Promise((resolve, reject) => {
126127
this._server.once("close", (code, signal) => {
127-
onServerKilled(this._server, signal,code, this._args.verbose);
128+
onServerKilled(this._server, signal, code, this._args.verbose);
128129
resolve();
129130
});
130131

131132
this._server.once("exit", (code, signal) => {
132-
onServerKilled(this._server, signal,code, this._args.verbose);
133+
onServerKilled(this._server, signal, code, this._args.verbose);
133134
resolve();
134135
});
135136

136137
this._server.once("error", (code, signal) => {
137-
onServerKilled(this._server, signal,code, this._args.verbose);
138+
onServerKilled(this._server, signal, code, this._args.verbose);
138139
resolve();
139140
});
140141

141142
this._server.once("disconnect", (code, signal) => {
142-
onServerKilled(this._server, signal,code, this._args.verbose);
143+
onServerKilled(this._server, signal, code, this._args.verbose);
143144
resolve();
144145
});
145146

lib/device-manager.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ export declare class DeviceManager implements IDeviceManager {
66
constructor();
77
startDevice(args: INsCapabilities): Promise<IDevice>;
88
stopDevice(device: IDevice, args: INsCapabilities): Promise<any>;
9+
static getDevices(query: IDevice): Promise<IDevice[]>;
910
installApp(args: INsCapabilities): Promise<any>;
1011
uninstallApp(args: INsCapabilities): Promise<any>;
1112
static kill(device: IDevice): Promise<void>;
12-
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): any;
13+
static getInstalledApps(device: IDevice): Promise<string[]>;
14+
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): IDevice;
1315
static setDontKeepActivities(args: INsCapabilities, driver: any, value: any): Promise<void>;
1416
static executeShellCommand(driver: any, commandArgs: {
1517
command: string;

lib/device-manager.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
DeviceType,
1212
sortDescByApiLevelPredicate
1313
} from "mobile-devices-controller";
14+
import { isRegExp } from "util";
1415

1516
export class DeviceManager implements IDeviceManager {
1617
private static _emulators: Map<string, IDevice> = new Map();
@@ -25,7 +26,7 @@ export class DeviceManager implements IDeviceManager {
2526
const token = process.env["DEVICE_TOKEN"] || process.env.npm_config_deviceToken;
2627
device.token = token && token.replace("emulator-", "");
2728
device.name = process.env["DEVICE_NAME"] || device.name;
28-
29+
2930
DeviceManager.cleanUnsetProperties(device);
3031

3132
if (args.ignoreDeviceController) {
@@ -45,8 +46,7 @@ export class DeviceManager implements IDeviceManager {
4546
return device;
4647
}
4748

48-
const searchQuery = args.appiumCaps.udid ? { token: args.appiumCaps.udid } : device;
49-
49+
const searchQuery = args.appiumCaps.udid ? { token: args.appiumCaps.udid } : Object.assign(device);
5050
const foundDevices = (await DeviceController.getDevices(searchQuery))
5151
.sort((a, b) => sortDescByApiLevelPredicate(a, b));
5252

@@ -75,20 +75,22 @@ export class DeviceManager implements IDeviceManager {
7575

7676
if (foundDevices && foundDevices.length > 0) {
7777
let deviceStatus = args.reuseDevice ? Status.BOOTED : Status.SHUTDOWN;
78-
device = DeviceController.filter(foundDevices, { status: deviceStatus })[0];
78+
device = DeviceController.filter(foundDevices, { status: deviceStatus })
79+
.filter(d => d.type !== DeviceType.TV && d.type !== DeviceType.WATCH)[0];
7980

8081
// If there is no shutdown device
8182
if (!device || !device.status) {
8283
deviceStatus = args.reuseDevice ? Status.SHUTDOWN : Status.BOOTED;
83-
device = DeviceController.filter(foundDevices, { status: deviceStatus })[0];
84+
device = DeviceController.filter(foundDevices, { status: deviceStatus })
85+
.filter(d => d.type !== DeviceType.TV && d.type !== DeviceType.WATCH)[0];
8486
}
8587

8688
// If the device should not be reused we need to shutdown device and boot a clean instance
8789
let startDeviceOptions = args.startDeviceOptions || undefined;
8890
if (!args.reuseDevice && device.status !== Status.SHUTDOWN) {
8991
await DeviceController.kill(device);
9092
device.status = Status.SHUTDOWN;
91-
startDeviceOptions = device.type === DeviceType.EMULATOR ? "-wipe-data -no-snapshot-load -no-boot-anim -no-audio" : "";
93+
startDeviceOptions = device.type === DeviceType.EMULATOR ? "-wipe-data -no-snapshot-load -no-boot-anim -no-audio -snapshot clean_boot" : "";
9294
logInfo("Change appium config to fullReset: false if no restart of the device needed!");
9395
}
9496

@@ -131,6 +133,10 @@ export class DeviceManager implements IDeviceManager {
131133
}
132134
}
133135

136+
public static async getDevices(query: IDevice) {
137+
return await DeviceController.getDevices(query);
138+
}
139+
134140
public async installApp(args: INsCapabilities): Promise<any> {
135141
if (args.isIOS) {
136142
IOSController.installApp(args.device, args.appiumCaps.app);
@@ -149,7 +155,13 @@ export class DeviceManager implements IDeviceManager {
149155
}
150156

151157
public static async kill(device: IDevice) {
152-
await DeviceController.kill(device);
158+
if (device) {
159+
await DeviceController.kill(device);
160+
}
161+
}
162+
163+
public static async getInstalledApps(device: IDevice) {
164+
return await DeviceController.getInstalledApplication(device);
153165
}
154166

155167
public static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number) {

lib/interfaces/ns-capabilities-args.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ export interface INsCapabilitiesArgs {
3737
deviceManager?: IDeviceManager;
3838
imagesPath?: string;
3939
startDeviceOptions?: string;
40+
deviceTypeOrPlatform?: string;
4041
}

lib/interfaces/ns-capabilities-args.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ export interface INsCapabilitiesArgs {
3838
deviceManager?: IDeviceManager;
3939
imagesPath?: string;
4040
startDeviceOptions?: string;
41+
deviceTypeOrPlatform?: string
4142
}

lib/ns-capabilities.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ export declare class NsCapabilities implements INsCapabilities {
3838
deviceManager: IDeviceManager;
3939
exceptions: Array<string>;
4040
imagesPath: string;
41+
deviceTypeOrPlatform: string;
4142
constructor(_parser: INsCapabilitiesArgs);
4243
readonly isAndroid: any;
4344
readonly isIOS: boolean;
4445
automationName: AutomationName;
4546
setAutomationNameFromString(automationName: String): void;
4647
extend(args: INsCapabilities): this;
47-
validateArgs(): any;
48+
validateArgs(): Promise<void>;
4849
private isAndroidPlatform;
4950
shouldSetFullResetOption(): void;
5051
private setAutomationName;

0 commit comments

Comments
 (0)