forked from khanhdodang/nativescript-dev-appium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
160 lines (145 loc) · 6.28 KB
/
index.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
import { AppiumServer } from "./lib/appium-server";
import { AppiumDriver } from "./lib/appium-driver";
import { NsCapabilities } from "./lib/ns-capabilities";
import { IDeviceManager } from "./lib/interfaces/device-manager";
import * as frameComparerHelper from "./lib/frame-comparer";
import { FrameComparer } from "./lib/frame-comparer";
import { DeviceManager } from "./lib/device-manager";
import { DeviceController } from "mobile-devices-controller";
import { logInfo, logError } from "./lib/utils";
import { INsCapabilities } from "./lib/interfaces/ns-capabilities";
import { INsCapabilitiesArgs } from "./lib/interfaces/ns-capabilities-args";
import * as parser from "./lib/parser"
export { AppiumDriver } from "./lib/appium-driver";
export { AppiumServer } from "./lib/appium-server";
export { ElementHelper } from "./lib/element-helper";
export { UIElement } from "./lib/ui-element";
export { Point } from "./lib/point";
export { SearchOptions } from "./lib/search-options";
export { Locator } from "./lib/locators";
export { Direction } from "./lib/direction";
export { DeviceManager } from "./lib/device-manager";
export { FrameComparer } from "./lib/frame-comparer";
export { IRectangle } from "./lib/interfaces/rectangle";
export { IDeviceManager } from "./lib/interfaces/device-manager";
export { LogType } from "./lib/log-types";
export { INsCapabilities } from "./lib/interfaces/ns-capabilities";
export { INsCapabilitiesArgs } from "./lib/interfaces/ns-capabilities-args";
export { logInfo, logError, logWarn } from "./lib/utils";
export { ITestReporter } from "./lib/interfaces/test-reporter";
export { screencapture } from "./lib/helpers/screenshot-manager";
export { LogImageType } from "./lib/enums/log-image-type";
export { ImageHelper, IImageCompareOptions } from "./lib/image-helper";
export { DeviceOrientation } from "./lib/enums/device-orientation";
export const nsCapabilities: INsCapabilities = new NsCapabilities(parser);
const appiumServer = new AppiumServer(nsCapabilities);
let frameComparer: FrameComparer;
let appiumDriver = null;
if (nsCapabilities.startSession) {
startServer(nsCapabilities.port).then(s => {
createDriver().then((d: AppiumDriver) => {
logInfo("Session has started successfully!");
d.sessionId().then(session => {
logInfo(`Session id: ${session}`);
logInfo(`Appium server port: ${appiumServer.port}`);
}).catch(error => {
logError('Something went wrong! Appium driver failed to start. Check appium config file.');
logError(error);
});
}).catch(error => {
logError('Something went wrong! Appium driver failed to start. Check appium config file.');
logError(error);
});
}).catch(error => {
logError('Something went wrong! Appium server failed to start. Check appium config file!');
logError(error);
});
}
export async function startServer(port?: number, deviceManager?: IDeviceManager) {
await appiumServer.start(port || nsCapabilities.port, deviceManager);
await attachToExitProcessHookup(appiumServer.server, "appium");
return appiumServer;
}
export async function stopServer() {
if (appiumDriver && appiumDriver.isAlive) {
await appiumDriver.quit();
}
if (appiumServer && appiumServer.server && !appiumServer.server.killed) {
await appiumServer.stop();
}
if (nsCapabilities.cleanApp && !nsCapabilities.ignoreDeviceController) {
await DeviceController.uninstallApplication(nsCapabilities.device, nsCapabilities.appPath);
logInfo("Application from device is uninstalled.")
}
};
export async function createDriver(args?: INsCapabilitiesArgs) {
if (args) {
nsCapabilities.extend(args);
}
if (!nsCapabilities.port) {
nsCapabilities.port = appiumServer.port;
}
if (nsCapabilities.attachToDebug) {
if (!appiumDriver) {
appiumDriver = await AppiumDriver.createAppiumDriver(nsCapabilities);
}
return appiumDriver;
}
if (!appiumServer.server && (!nsCapabilities.isSauceLab || !nsCapabilities.kobiton)) {
logInfo("Server is not available! To start appium server programmaticаlly use startServer()!");
}
if (!nsCapabilities.appiumCapsLocation) {
throw new Error("Provided path to appium capabilities is not correct!");
}
if (!nsCapabilities.runType && !nsCapabilities.appiumCaps) {
throw new Error("--runType is missing! Make sure it is provided correctly! It is used to parse the configuration for appium driver!");
}
if (appiumDriver !== null && appiumDriver.isAlive) {
return appiumDriver;
} else if (appiumDriver === null) {
appiumDriver = await AppiumDriver.createAppiumDriver(nsCapabilities);
} else if (appiumDriver !== null && !appiumDriver.isAlive) {
await appiumDriver.init();
}
// Make sure to turn off "Don't keep activities"
// in case of previous execution failure.
await DeviceManager.setDontKeepActivities(nsCapabilities, appiumDriver.driver, false);
return appiumDriver;
}
/**
* Provide instance of FrameComparer in order to compare frames/ images from video
* Please read carefully README.md before using it.
* @throws exception in order the dependecies are not installed properly.
*/
export function loadFrameComparer() {
if (!frameComparer) {
frameComparer = frameComparerHelper.loadFrameComparer(nsCapabilities);
}
return frameComparer;
}
const killProcesses = async (code) => {
console.log(`About to exit with code: ${code}`);
if (appiumServer) {
await stopServer();
}
process.removeAllListeners();
try {
//if (isWin() && process) {
// process.exit(0);
//}
} catch (error) { }
}
const attachToExitProcessHookup = (processToAttach, processName) => {
const signals = ['exit', 'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'];
if (!processToAttach) {
return;
}
signals.forEach(function (sig) {
processToAttach.once(sig, async function () {
await killProcesses(sig);
console.log(`Exited from ${processName}`);
processToAttach.removeListener(sig, killProcesses);
});
});
}