Skip to content

Commit 1133f24

Browse files
refactor: change emulator and simulator managers with mobile-devices-controller
* Implement devices controller * Update to new mobile-device-controller * Include mobile-devices-controller in device-controller
1 parent 0f725fa commit 1133f24

13 files changed

+134
-435
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export { Point } from "./lib/point";
55
export { SearchOptions } from "./lib/search-options";
66
export { Locator } from "./lib/locators";
77
export { Direction } from "./lib/direction";
8-
export { EmulatorManager } from "./lib/emulator-manager";
8+
export { DeviceController } from "./lib/device-controller";
99
export declare function startServer(port?: number): Promise<void>;
1010
export declare function stopServer(): Promise<void>;
1111
export declare function createDriver(): Promise<any>;

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export { Point } from "./lib/point";
1212
export { SearchOptions } from "./lib/search-options";
1313
export { Locator } from "./lib/locators";
1414
export { Direction } from "./lib/direction";
15-
export { EmulatorManager } from "./lib/emulator-manager";
15+
export { DeviceController } from "./lib/device-controller";
1616

1717
const nsCapabilities = new NsCapabilities();
1818
const appiumServer = new AppiumServer(nsCapabilities);

lib/appium-driver.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export class AppiumDriver {
365365
}
366366

367367
driverConfig = "https://" + sauceUser + ":" + sauceKey + "@ondemand.saucelabs.com:443/wd/hub";
368-
368+
369369

370370
args.appiumCaps.app = "sauce-storage:" + args.appPath;
371371
console.log("Using Sauce Labs. The application path is changed to: " + args.appPath);
@@ -400,7 +400,10 @@ export class AppiumDriver {
400400
console.log("Killing driver");
401401
try {
402402
await this._driver.quit();
403+
await this._driver.quit();
404+
await this._webio.quit();
403405
} catch (error) {
406+
console.log("", error);
404407
}
405408
this._isAlive = false;
406409
console.log("Driver is dead");

lib/appium-server.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as child_process from "child_process";
22
import { log, resolve, waitForOutput, shutdown, fileExists, isWin, executeCommand } from "./utils";
33
import { INsCapabilities } from "./ins-capabilities";
4-
import { SimulatorManager } from "./simulator-manager";
5-
import { EmulatorManager } from "./emulator-manager";
4+
import { DeviceController } from "./device-controller";
65

76
export class AppiumServer {
87
private _server: child_process.ChildProcess;
@@ -46,16 +45,9 @@ export class AppiumServer {
4645
}
4746

4847
public async start() {
49-
50-
if (this._args.appiumCaps.platformName.toLowerCase() === "android") {
51-
await EmulatorManager.startEmulator(this._args);
52-
}
53-
54-
if (!this._args.isSauceLab && this._args.appiumCaps.platformName.toLowerCase().includes("ios")) {
55-
await SimulatorManager.startDevice(this._args);
56-
}
57-
48+
const device = await DeviceController.startDevice(this._args)
5849
log("Starting server...", this._args.verbose);
50+
this._args.appiumCaps["udid"] = device.token;
5951
const logLevel = this._args.verbose === true ? "debug" : "info";
6052
this._server = child_process.spawn(this._appium, ["-p", this.port.toString(), "--log-level", logLevel], {
6153
shell: true,
@@ -68,15 +60,7 @@ export class AppiumServer {
6860
}
6961

7062
public async stop() {
71-
72-
if (this._args.appiumCaps.platformName.toLowerCase() === "android") {
73-
await EmulatorManager.stop(this._args);
74-
}
75-
76-
if (!this._args.isSauceLab && !this._args.reuseDevice && this._args.appiumCaps.platformName.toLowerCase().includes("ios")) {
77-
await SimulatorManager.stop(this._args);
78-
}
79-
63+
await DeviceController.stop(this._args);
8064
return new Promise((resolve, reject) => {
8165
this._server.on("close", (code, signal) => {
8266
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
@@ -102,10 +86,12 @@ export class AppiumServer {
10286
try {
10387
if (isWin) {
10488
shutdown(this._server, this._args.verbose);
105-
this._server.kill("SIGINT");
89+
this._server.kill("SIGINT");
90+
this._server.kill("SIGINT");
10691
} else {
10792
this._server.kill("SIGINT");
10893
this._server.kill("SIGINT");
94+
this._server.kill("SIGKILL");
10995
shutdown(this._server, this._args.verbose);
11096
}
11197
} catch (error) {

lib/device-controller.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { INsCapabilities } from "./ins-capabilities";
2+
import { IDevice } from "mobile-devices-controller";
3+
export declare class DeviceController {
4+
private static _emulators;
5+
static startDevice(args: INsCapabilities): Promise<IDevice>;
6+
static stop(args: INsCapabilities): Promise<void>;
7+
static kill(device: IDevice): Promise<void>;
8+
private static device(runType);
9+
private static getDevicesByStatus(devices, status);
10+
}

lib/device-controller.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { waitForOutput, resolve, log, isWin, shutdown, executeCommand } from "./utils";
2+
import * as child_process from "child_process";
3+
import { INsCapabilities } from "./ins-capabilities";
4+
5+
import {
6+
DeviceManager,
7+
AndroidManager,
8+
IOSManager,
9+
IDevice,
10+
Platform,
11+
Status
12+
} from "mobile-devices-controller";
13+
14+
15+
export class DeviceController {
16+
private static _emulators: Map<string, IDevice> = new Map();
17+
18+
public static async startDevice(args: INsCapabilities) {
19+
const devices = (await DeviceManager.getAllDevices(args.appiumCaps.platformName.toLowerCase(), args.appiumCaps.deviceName));
20+
if (!devices || devices === null || devices.size) {
21+
console.log("Available devices:\n", devices);
22+
throw new Error(`No such device ${args.appiumCaps.deviceName}!!!\n Check availabe emulators!!!`);
23+
}
24+
25+
let device: IDevice;
26+
// Should find new device
27+
if (!args.reuseDevice) {
28+
device = DeviceController.getDevicesByStatus(devices, Status.SHUTDOWN);
29+
}
30+
31+
// If there is no shutdown device
32+
if (!device || device === null) {
33+
device = DeviceController.getDevicesByStatus(devices, Status.BOOTED);
34+
}
35+
36+
// If there is no booted device
37+
if (!device || device === null) {
38+
device = DeviceController.getDevicesByStatus(devices, Status.FREE);
39+
}
40+
41+
// In case reuse device is true but there weren't any booted devices. We need to fall back and boot new one.
42+
if (!device || device === null && args.reuseDevice) {
43+
device = DeviceController.getDevicesByStatus(devices, Status.SHUTDOWN);
44+
}
45+
46+
if (device.status === Status.SHUTDOWN) {
47+
await DeviceManager.startDevice(device);
48+
console.log("Started device: ", device);
49+
} else {
50+
console.log("Device is alredy started", device);
51+
if (!args.reuseDevice) {
52+
DeviceController.kill(device);
53+
await DeviceManager.startDevice(device);
54+
}
55+
}
56+
57+
DeviceController._emulators.set(args.runType, device);
58+
59+
return device;
60+
}
61+
62+
public static async stop(args: INsCapabilities) {
63+
if (DeviceController._emulators.has(args.runType) && !args.reuseDevice) {
64+
const device = DeviceController._emulators.get(args.runType);
65+
await DeviceManager.kill(device);
66+
}
67+
}
68+
69+
public static async kill(device: IDevice) {
70+
await DeviceManager.kill(device);
71+
}
72+
73+
private static device(runType) {
74+
return DeviceController._emulators.get(runType);
75+
}
76+
77+
private static getDevicesByStatus(devices: Array<IDevice>, status) {
78+
let device: IDevice;
79+
const shutdownDeivces = devices.filter(dev => {
80+
return dev.status === status;
81+
});
82+
83+
if (shutdownDeivces && shutdownDeivces !== null && shutdownDeivces.length > 0) {
84+
device = shutdownDeivces[0];
85+
}
86+
87+
return device;
88+
}
89+
}

lib/emulator-manager.d.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/emulator-manager.ts

Lines changed: 0 additions & 131 deletions
This file was deleted.

lib/simulator-manager.d.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)