Skip to content

Commit 759578a

Browse files
Update starting and killing of emulator
1 parent cd39cea commit 759578a

File tree

7 files changed

+105
-64
lines changed

7 files changed

+105
-64
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +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";
89
export declare function startServer(port?: number): Promise<void>;
910
export declare function stopServer(): Promise<void>;
1011
export declare function createDriver(): Promise<any>;

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +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";
1516

1617
const nsCapabilities = new NsCapabilities();
1718
const appiumServer = new AppiumServer(nsCapabilities);

lib/emulator-manager.d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
/// <reference types="node" />
2+
import * as child_process from "child_process";
13
import { INsCapabilities } from "./ins-capabilities";
24
export declare class EmulatorManager {
5+
private _id;
6+
private _args;
7+
private _emulatorProc;
8+
private _shouldKill;
39
private static _emulators;
410
private static _emulatorIds;
11+
constructor(_id: any, _args: any, _emulatorProc: child_process.ChildProcess, _shouldKill: any);
12+
readonly id: any;
13+
readonly emulatorProc: child_process.ChildProcess;
14+
readonly args: any;
15+
readonly shouldKill: any;
516
static startEmulator(args: INsCapabilities): Promise<boolean>;
6-
static stop(args: INsCapabilities): Promise<{}>;
17+
static stop(args: INsCapabilities): void;
718
private static waitUntilEmulatorBoot(deviceId, timeOut);
819
/**
920
*

lib/emulator-manager.ts

Lines changed: 81 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,28 @@ const EMULATOR = resolve(ANDROID_HOME, "emulator", "emulator");
77
const ADB = resolve(ANDROID_HOME, "platform-tools", "adb");
88

99
export class EmulatorManager {
10-
private static _emulators: Map<string, child_process.ChildProcess> = new Map();
10+
private static _emulators: Map<string, EmulatorManager> = new Map();
1111
private static _emulatorIds: Map<string, string> = new Map();
1212

13+
constructor(private _id, private _args, private _emulatorProc: child_process.ChildProcess, private _shouldKill) {
14+
}
15+
16+
get id() {
17+
return this._id;
18+
}
19+
20+
get emulatorProc() {
21+
return this._emulatorProc;
22+
}
23+
24+
get args() {
25+
return this._args;
26+
}
27+
28+
get shouldKill() {
29+
return this._shouldKill;
30+
}
31+
1332
public static async startEmulator(args: INsCapabilities) {
1433
if (!args.appiumCaps.avd || args.appiumCaps.avd === "") {
1534
log("No avd name provided! We will not start the emulator!", args.verbose);
@@ -26,11 +45,10 @@ export class EmulatorManager {
2645
detached: false
2746
});
2847

29-
const responce: boolean = await waitForOutput(emulator, new RegExp(args.appiumCaps.avd, "i"), /Error/, args.appiumCaps.lt || 180000, args.verbose);
30-
31-
if (responce) {
48+
const responce: boolean = await waitForOutput(emulator, new RegExp(args.appiumCaps.avd, "i"), new RegExp("Error", "i"), args.appiumCaps.lt || 180000, args.verbose);
49+
if (responce === true) {
3250
EmulatorManager.waitUntilEmulatorBoot(id, args.appiumCaps.lt || 180000);
33-
EmulatorManager._emulators.set(args.runType, emulator);
51+
EmulatorManager._emulators.set(args.runType, new EmulatorManager(id, args, emulator, true));
3452
} else {
3553
log("Emulator is probably already started!", args.verbose);
3654
}
@@ -41,40 +59,43 @@ export class EmulatorManager {
4159
public static stop(args: INsCapabilities) {
4260
if (EmulatorManager._emulators.has(args.runType)) {
4361

44-
const emulator = EmulatorManager._emulators.get(args.runType);
45-
return new Promise((resolve, reject) => {
46-
emulator.on("close", (code, signal) => {
47-
log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
48-
resolve();
49-
});
50-
51-
emulator.on("exit", (code, signal) => {
52-
log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
53-
resolve();
54-
});
55-
56-
emulator.on("error", (code, signal) => {
57-
log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
58-
resolve();
59-
});
60-
61-
emulator.on("disconnect", (code, signal) => {
62-
log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
63-
resolve();
64-
});
65-
66-
try {
67-
if (isWin) {
68-
shutdown(emulator, args.verbose);
69-
} else {
70-
shutdown(emulator, args.verbose);
71-
emulator.kill("SIGINT");
72-
//this._emulator.kill("SIGKILL");
73-
}
74-
} catch (error) {
75-
console.log(error);
76-
}
77-
});
62+
const emu = EmulatorManager._emulators.get(args.runType);
63+
executeCommand(ADB + " -s emulator-" + emu.id + " emu kill");
64+
65+
// return new Promise((resolve, reject) => {
66+
// emu.emulatorProc.on("close", (code, signal) => {
67+
// log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
68+
// resolve();
69+
// });
70+
71+
// emu.emulatorProc.on("exit", (code, signal) => {
72+
// log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
73+
// resolve();
74+
// });
75+
76+
// emu.emulatorProc.on("error", (code, signal) => {
77+
// log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
78+
// resolve();
79+
// });
80+
81+
// emu.emulatorProc.on("disconnect", (code, signal) => {
82+
// log(`Emulator terminated due signal: ${signal} and code: ${code}`, args.verbose);
83+
// resolve();
84+
// });
85+
86+
// try {
87+
// if (isWin) {
88+
// shutdown(emu.emulatorProc, args.verbose);
89+
// } else {
90+
// executeCommand(ADB + " -s emulator-" + emu.id + " emu kill");
91+
// shutdown(emu.emulatorProc, args.verbose);
92+
// emu.emulatorProc.kill("SIGINT");
93+
// //this._emulator.kill("SIGKILL");
94+
// }
95+
// } catch (error) {
96+
// console.log(error);
97+
// }
98+
// });
7899
}
79100

80101
}
@@ -104,22 +125,28 @@ export class EmulatorManager {
104125
* @param deviceId
105126
*/
106127
private static checkIfEmulatorIsRunning(deviceId) {
107-
let hasBooted = false;
108-
109-
let rowData = executeCommand(ADB + " -s " + deviceId + " shell dumpsys activity");
110-
let list = rowData.split("\\r\\n");
111-
112-
list.forEach(line => {
113-
if (line.includes("Recent #0")
114-
&& (line.includes("com.android.launcher")
115-
|| line.includes("com.google.android.googlequicksearchbox")
116-
|| line.includes("com.google.android.apps.nexuslauncher")
117-
|| line.includes(deviceId))) {
118-
hasBooted = true;
119-
}
120-
});
128+
let isBooted = executeCommand(ADB + " -s " + deviceId + " shell getprop sys.boot_completed").trim() === "1";
129+
if (isBooted) {
130+
isBooted = executeCommand(ADB + " -s " + deviceId + " shell getprop init.svc.bootanim").toLowerCase().trim() === "stopped";
131+
}
132+
133+
return isBooted;
134+
135+
// let hasBooted = false;
136+
// let rowData = executeCommand(ADB + " -s " + deviceId + " shell dumpsys activity");
137+
// let list = rowData.split("\\r\\n");
138+
139+
// list.forEach(line => {
140+
// if (line.includes("Recent #0")
141+
// && (line.includes("com.android.launcher")
142+
// || line.includes("com.google.android.googlequicksearchbox")
143+
// || line.includes("com.google.android.apps.nexuslauncher")
144+
// || line.includes(deviceId))) {
145+
// hasBooted = true;
146+
// }
147+
// });
121148

122-
return hasBooted;
149+
// return hasBooted;
123150
}
124151

125152
public static emulatorId(platformVersion) {

lib/image-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class ImageHelper {
88
}
99

1010
public thresholdType() {
11-
return ImageOptions.percent;
11+
return ImageOptions.pixel;
1212
}
1313

1414
public threshold() {

lib/utils.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,15 @@ export function waitForOutput(process, matcher, errorMatcher, timeout, verbose)
217217
process.stdout.on("data", function (data) {
218218
let line = "" + data;
219219
log(line, verbose);
220-
if (matcher.test(line)) {
221-
clearTimeout(abortWatch);
222-
resolve(true);
223-
}
224220
if (errorMatcher.test(line)) {
225221
clearTimeout(abortWatch);
226222
resolve(false);
227223
}
224+
225+
if (matcher.test(line)) {
226+
clearTimeout(abortWatch);
227+
resolve(true);
228+
}
228229
});
229230
});
230231
}

0 commit comments

Comments
 (0)