-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathappium-server.ts
218 lines (184 loc) · 8.01 KB
/
appium-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import * as child_process from "child_process";
import {
log,
resolve,
waitForOutput,
shutdown,
fileExists,
isWin,
executeCommand,
findFreePort,
getRegexResultsAsArray
} from "./utils";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDeviceManager } from "./interfaces/device-manager";
import { DeviceManger } from "./device-controller";
import { AndroidController } from "mobile-devices-controller";
export class AppiumServer {
private _server: child_process.ChildProcess;
private _appium;
private _port: number;
private _runType: string;
private _hasStarted: boolean;
private _deviceManager: IDeviceManager;
constructor(private _args: INsCapabilities) {
this._runType = this._args.runType;
this._hasStarted = false;
this.resolveAppiumDependency();
}
get port() {
return this._port;
}
set port(port: number) {
this._port = port;
}
set runType(runType: string) {
this._runType = runType;
}
get runType() {
return this._runType;
}
get server() {
return this._server;
}
get hasStarted() {
return this._hasStarted;
}
set hasStarted(hasStarted) {
this._hasStarted = hasStarted;
}
public async start(port, deviceManager: IDeviceManager = new DeviceManger()) {
await this.prepareDevice(deviceManager);
await this.prepareApp();
log("Starting server...", this._args.verbose);
const logLevel = this._args.verbose === true ? "debug" : "info";
this.port = this._args.port || port;
let retry = false;
this.startAppiumServer(logLevel, this._args.isSauceLab);
let response = await waitForOutput(this._server, /listener started/, /Error: listen/, 60000, this._args.verbose);
let retries = 11;
while (retries > 0 && !response) {
retries--;
this.port += 10;
this.port = (await findFreePort(100, this.port));
this.startAppiumServer(logLevel, this._args.isSauceLab);
response = await waitForOutput(this._server, /listener started/, /Error: listen/, 60000, true);
}
return response;
}
private startAppiumServer(logLevel: string, isSauceLab: boolean) {
const startingServerArgs: Array<string> = isSauceLab ? ["--log-level", logLevel] : ["-p", this.port.toString(), "--log-level", logLevel];
startingServerArgs.push("--relaxed-security");
this._server = child_process.spawn(this._appium, startingServerArgs, {
shell: true,
detached: false
});
}
public async stop() {
await this._deviceManager.stopDevice(this._args);
return new Promise((resolve, reject) => {
this._server.on("close", (code, signal) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
resolve();
});
this._server.on("exit", (code, signal) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
resolve();
});
this._server.on("error", (code, signal) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
resolve();
});
this._server.on("disconnect", (code, signal) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
resolve();
});
log("Stopping server...", this._args.verbose);
try {
if (isWin()) {
shutdown(this._server, this._args.verbose);
this._server.kill("SIGINT");
this._server.kill("SIGINT");
this._server = null;
} else {
this._server.kill("SIGINT");
this._server.kill("SIGINT");
this._server.kill("SIGKILL");
shutdown(this._server, this._args.verbose);
}
} catch (error) {
console.log(error);
}
});
}
private async prepareDevice(deviceManager: IDeviceManager) {
this._deviceManager = deviceManager;
if (!this._args.device) {
const device = await this._deviceManager.startDevice(this._args);
this._args.device = device;
}
}
private async prepareApp() {
const appPackage = this._args.isAndroid ? "appPackage" : "bundleId";
const appFullPath = this._args.appiumCaps.app;
if (!this._args.ignoreDeviceController) {
if (appFullPath && !this._args.appiumCaps[appPackage]) {
console.log(`Trying to resolve automatically ${appPackage}!`);
this._args.appiumCaps[appPackage] = this._deviceManager.getPackageId(this._args.device, appFullPath);
console.log(`Setting capabilities ${this._args.runType}{ "${appPackage}" : "${this._args.appiumCaps[appPackage]}" }!`);
}
const appActivityProp = "appActivity";
if (this._args.isAndroid && appFullPath && !this._args.appiumCaps[appActivityProp]) {
console.log(`Trying to resolve automatically ${appActivityProp}!`);
this._args.appiumCaps[appActivityProp] = AndroidController.getLaunchableActivity(appFullPath);
console.log(`Setting capabilities ${this._args.runType}{ "${appActivityProp} : "${this._args.appiumCaps[appActivityProp]}" }!`);
}
if (!this._args.appiumCaps[appPackage]) {
throw new Error(`Please, provide ${appPackage} in ${this._args.appiumCapsLocation} file!`);
}
if (this._args.isAndroid && !this._args.appiumCaps[appActivityProp]) {
throw new Error(`Please, provide ${appActivityProp} in ${this._args.appiumCapsLocation} file!`);
}
const groupings = getRegexResultsAsArray(/(\w+)/gi, this._args.appiumCaps[appPackage]);
this._args.appName = groupings[groupings.length - 1];
console.log(`Setting application name as ${this._args.appName}`);
if (!this._args.devMode && !this._args.ignoreDeviceController) {
await this._deviceManager.uninstallApp(this._args);
} else {
this._args.appiumCaps.app = "";
}
}
if (this._args.appiumCaps[appPackage]) {
const groupings = getRegexResultsAsArray(/(\w+)/gi, this._args.appiumCaps[appPackage]);
this._args.appName = groupings[groupings.length - 1];
}
}
// Resolve appium dependency
private resolveAppiumDependency() {
const projectDir = this._args.projectDir;
const pluginBinary = this._args.pluginBinary;
const projectBinary = this._args.projectBinary;
const pluginRoot = this._args.pluginRoot;
let appium = process.platform === "win32" ? "appium.cmd" : "appium";
const pluginAppiumBinary = resolve(pluginBinary, appium);
const projectAppiumBinary = resolve(projectBinary, appium);
if (fileExists(pluginAppiumBinary)) {
log("Using plugin-local Appium binary.", this._args.verbose);
appium = pluginAppiumBinary;
} else if (fileExists(projectAppiumBinary)) {
log("Using project-local Appium binary.", this._args.verbose);
appium = projectAppiumBinary;
} else {
//const result = executeCommand("npm list -g");
//if (result.includes("appium")) {
log("Using global Appium binary.", true);
log('Please, make sure it is installed globally.', true);
//} else if (result.includes("appium")) {
// const msg = "Appium not found. Please install appium before runnig tests!";
// log(msg, this._args.verbose);
// new Error(msg);
// }
}
this._appium = appium;
}
}