Skip to content

Commit d40d454

Browse files
refactor: remove ServiceContext (#92)
1 parent 10b1342 commit d40d454

15 files changed

+41
-169
lines changed

Diff for: lib/appium-driver.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export declare class AppiumDriver {
2525
private _logPath;
2626
private _storageByDeviceName;
2727
private _storageByPlatform;
28-
private constructor(_driver, _wd, _webio, _driverConfig, _args);
28+
private constructor();
2929
readonly imageHelper: ImageHelper;
3030
defaultWaitTime: number;
3131
readonly capabilities: any;

Diff for: lib/appium-driver.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { UIElement } from "./ui-element";
1111
import { Direction } from "./direction";
1212
import { Locator } from "./locators";
1313
import { Platform } from "mobile-devices-controller";
14-
import { ServiceContext } from "./service/service-context";
1514
import {
1615
addExt,
1716
log,
@@ -470,9 +469,8 @@ export class AppiumDriver {
470469

471470
// It looks we need it for XCTest (iOS 10+ automation)
472471
if (args.appiumCaps.platformVersion >= 10) {
473-
let freePort = await findFreePort(10, (parseInt(args.appiumCaps["wdaLocalPort"])) || 8400, args);
474-
console.log(" args.appiumCaps['wdaLocalPort']", freePort)
475-
args.appiumCaps["wdaLocalPort"] = freePort;
472+
console.log(`args.appiumCaps['wdaLocalPort']: ${args.wdaPort}`);
473+
args.appiumCaps["wdaLocalPort"] = args.wdaPort;
476474
}
477475
}
478476
}

Diff for: lib/appium-server.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export declare class AppiumServer {
1616
readonly server: child_process.ChildProcess;
1717
hasStarted: boolean;
1818
start(port: any, deviceManager?: IDeviceManager): Promise<boolean>;
19+
private startAppiumServer(logLevel);
1920
stop(): Promise<{}>;
2021
private resolveAppiumDependency();
2122
}

Diff for: lib/appium-server.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
import { INsCapabilities } from "./interfaces/ns-capabilities";
1313
import { IDeviceManager } from "./interfaces/device-manager";
1414
import { DeviceManger } from "./device-controller";
15-
import { ServiceContext } from "../lib/service/service-context";
1615

1716
export class AppiumServer {
1817
private _server: child_process.ChildProcess;
@@ -56,7 +55,7 @@ export class AppiumServer {
5655
this._hasStarted = hasStarted;
5756
}
5857

59-
public async start(port, deviceManager: IDeviceManager = new DeviceManger(this._args.deviceControllerServerPort)) {
58+
public async start(port, deviceManager: IDeviceManager = new DeviceManger()) {
6059
this._deviceManager = deviceManager;
6160
if (!this._args.device) {
6261
const device = await this._deviceManager.startDevice(this._args);
@@ -70,25 +69,30 @@ export class AppiumServer {
7069
this.port = port || this._args.port;
7170
let retry = false;
7271

73-
let response: boolean = false;
72+
this.startAppiumServer(logLevel);
73+
74+
let response = await waitForOutput(this._server, /listener started/, /Error: listen/, 60000, this._args.verbose);
75+
7476
let retries = 11;
7577
while (retries > 0 && !response) {
7678
retries--;
79+
this.port += 10;
7780
this.port = (await findFreePort(100, this.port, this._args));
7881

79-
this._server = child_process.spawn(this._appium, ["-p", this.port.toString(), "--log-level", logLevel], {
80-
shell: true,
81-
detached: false
82-
});
82+
this.startAppiumServer(logLevel);
8383
response = await waitForOutput(this._server, /listener started/, /Error: listen/, 60000, this._args.verbose);
84-
if (!response) {
85-
this.port += 10;
86-
}
8784
}
8885

8986
return response;
9087
}
9188

89+
private startAppiumServer(logLevel) {
90+
this._server = child_process.spawn(this._appium, ["-p", this.port.toString(), "--log-level", logLevel], {
91+
shell: true,
92+
detached: false
93+
});
94+
}
95+
9296
public async stop() {
9397
await this._deviceManager.stopDevice(this._args);
9498
return new Promise((resolve, reject) => {

Diff for: lib/device-controller.d.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { INsCapabilities } from "./interfaces/ns-capabilities";
22
import { IDeviceManager } from "./interfaces/device-manager";
3-
import { ServiceContext } from "./service/service-context";
43
import { IDevice } from "mobile-devices-controller";
54
export declare class DeviceManger implements IDeviceManager {
6-
private _serveiceContext;
75
private static _emulators;
8-
constructor(port: any, _serveiceContext?: ServiceContext);
6+
constructor();
97
startDevice(args: INsCapabilities): Promise<IDevice>;
108
stopDevice(args: INsCapabilities): Promise<any>;
119
installApp(args: INsCapabilities): Promise<any>;

Diff for: lib/device-controller.ts

+3-39
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import {
44
log,
55
isWin,
66
shutdown,
7-
executeCommand,
8-
findFreePort
7+
executeCommand
98
} from "./utils";
109
import * as child_process from "child_process";
1110
import { INsCapabilities } from "./interfaces/ns-capabilities";
1211
import { IDeviceManager } from "./interfaces/device-manager";
13-
import { ServiceContext } from "./service/service-context";
1412

1513
import {
1614
IDevice,
@@ -26,10 +24,7 @@ import {
2624
export class DeviceManger implements IDeviceManager {
2725
private static _emulators: Map<string, IDevice> = new Map();
2826

29-
constructor(port, private _serveiceContext: ServiceContext = undefined) {
30-
if (!this._serveiceContext) {
31-
this._serveiceContext = ServiceContext.createServer(port);
32-
}
27+
constructor() {
3328
}
3429

3530
public async startDevice(args: INsCapabilities): Promise<IDevice> {
@@ -46,20 +41,6 @@ export class DeviceManger implements IDeviceManager {
4641
return device;
4742
}
4843

49-
// Using serve to manage deivces.
50-
if (args.useDeviceControllerServer) {
51-
const d = await this._serveiceContext.subscribe(args.appiumCaps.deviceName, args.appiumCaps.platformName.toLowerCase(), args.appiumCaps.platformVersion, args.appiumCaps.app);
52-
delete d['__v'];
53-
delete d['_id']
54-
if (!d || !(d as IDevice)) {
55-
console.error("", d);
56-
throw new Error("Missing device: " + d);
57-
}
58-
console.log(`Device:`, d);
59-
60-
return <any>d;
61-
}
62-
6344
const allDevices = (await DeviceController.getDevices({ platform: args.appiumCaps.platformName }));
6445
if (!allDevices || allDevices === null || allDevices.length === 0) {
6546
console.log("We couldn't find any devices. We will try to proceed to appium! Maybe avd manager is missing")
@@ -111,27 +92,10 @@ export class DeviceManger implements IDeviceManager {
11192
public async stopDevice(args: INsCapabilities): Promise<any> {
11293
if (process.env["DEVICE_TOKEN"]) {
11394
return;
114-
}
115-
if (args.useDeviceControllerServer) {
116-
const d = await this._serveiceContext.unsubscribe(args.device.token);
117-
if (!d) {
118-
console.error("", d);
119-
throw new Error("Missing device: " + d);
120-
}
121-
122-
try {
123-
await this._serveiceContext.releasePort(args.port);
124-
if (args.appiumCaps.wdaLocalPort) {
125-
await this._serveiceContext.releasePort(args.appiumCaps.wdaLocalPort);
126-
}
127-
} catch (error) {
128-
console.log(error);
129-
}
13095
} else if (DeviceManger._emulators.has(args.runType)
13196
&& !args.reuseDevice
13297
&& !args.isSauceLab
133-
&& !args.ignoreDeviceController
134-
&& !args.useDeviceControllerServer) {
98+
&& !args.ignoreDeviceController) {
13599
const device = DeviceManger._emulators.get(args.runType);
136100
await DeviceManger.kill(device);
137101
}

Diff for: lib/interfaces/ns-capabilities.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ export interface INsCapabilities {
2020
reuseDevice: boolean;
2121
device: IDevice;
2222
ignoreDeviceController: boolean;
23-
useDeviceControllerServer: boolean;
24-
deviceControllerServerPort: number;
23+
wdaLocalPort: number;
2524
}

Diff for: lib/interfaces/ns-capabilities.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ export interface INsCapabilities {
2121
reuseDevice: boolean;
2222
device: IDevice;
2323
ignoreDeviceController: boolean;
24-
useDeviceControllerServer: boolean;
25-
deviceControllerServerPort: number;
24+
wdaLocalPort: number;
2625
}

Diff for: lib/ns-capabilities.d.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export declare class NsCapabilities implements INsCapabilities {
2121
private _emulatorOptions;
2222
private _device;
2323
private _ignoreDeviceController;
24-
private _useDeviceControllerServer;
25-
private _deviceControllerServerPort;
24+
private _wdaLocalPort;
2625
private exceptions;
2726
constructor();
2827
readonly projectDir: any;
@@ -43,8 +42,7 @@ export declare class NsCapabilities implements INsCapabilities {
4342
readonly isSauceLab: any;
4443
appPath: string;
4544
readonly ignoreDeviceController: boolean;
46-
readonly useDeviceControllerServer: boolean;
47-
readonly deviceControllerServerPort: number;
45+
readonly wdaLocalPort: number;
4846
device: IDevice;
4947
readonly emulatorOptions: string;
5048
private isAndroidPlatform();

Diff for: lib/ns-capabilities.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ export class NsCapabilities implements INsCapabilities {
2525
private _emulatorOptions: string;
2626
private _device: IDevice;
2727
private _ignoreDeviceController: boolean;
28-
private _useDeviceControllerServer: boolean;
29-
private _deviceControllerServerPort: number;
28+
private _wdaLocalPort: number;
3029
private exceptions: Array<string> = new Array();
3130

3231
constructor() {
@@ -48,8 +47,7 @@ export class NsCapabilities implements INsCapabilities {
4847
this._isIOS = !this._isAndroid;
4948
this._isSauceLab = parser.isSauceLab;
5049
this._ignoreDeviceController = parser.ignoreDeviceController;
51-
this._useDeviceControllerServer = parser.useDeviceControllerServer;
52-
this._deviceControllerServerPort = parser.deviceControllerServerPort;
50+
this._wdaLocalPort = parser.wdaLocalPort;
5351
this.resolveAppPath();
5452
this.checkMandatoryCapabiliies();
5553
this.throwExceptions();
@@ -73,8 +71,7 @@ export class NsCapabilities implements INsCapabilities {
7371
get isSauceLab() { return this._isSauceLab; }
7472
get appPath() { return this._appPath; }
7573
get ignoreDeviceController() { return this._ignoreDeviceController; }
76-
get useDeviceControllerServer() { return this._useDeviceControllerServer; }
77-
get deviceControllerServerPort() { return this._deviceControllerServerPort; }
74+
get wdaLocalPort() { return this._wdaLocalPort; }
7875
set appPath(appPath: string) { this._appPath = appPath; }
7976
get device() { return this._device; }
8077
set device(device: IDevice) { this._device = device; }

Diff for: lib/parser.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export declare const capabilitiesName = "appium.capabilities.json";
2-
export declare const projectDir: any, projectBinary: any, pluginRoot: any, pluginBinary: any, port: any, verbose: any, appiumCapsLocation: any, testFolder: any, runType: any, isSauceLab: any, appPath: any, storage: any, testReports: any, reuseDevice: any, ignoreDeviceController: any, useDeviceControllerServer: any, deviceControllerServerPort: number;
2+
export declare const projectDir: any, projectBinary: any, pluginRoot: any, pluginBinary: any, port: any, verbose: any, appiumCapsLocation: any, testFolder: any, runType: any, isSauceLab: any, appPath: any, storage: any, testReports: any, reuseDevice: any, ignoreDeviceController: any, wdaLocalPort: any;

Diff for: lib/parser.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ const config = (() => {
1010
.option("testFolder", { describe: "e2e test folder name", default: "e2e", type: "string" })
1111
.option("appiumCapsLocation", { describe: "Capabilities", type: "string" })
1212
.option("sauceLab", { describe: "SauceLab", default: false, type: "boolean" })
13-
.option("port", { alias: "p", describe: "Execution port", type: "string" })
13+
.option("port", { alias: "p", describe: "Appium port", type: "string" })
14+
.option("wdaLocalPort", { alias: "p", describe: "WDA port", type: "string" })
1415
.option("verbose", { alias: "v", describe: "Log actions", type: "boolean" })
1516
.option("path", { describe: "path", default: process.cwd(), type: "string" })
1617
.option("appPath", { describe: "application path", type: "string" })
1718
.option("storage", { describe: "Storage for images folder.", type: "string" })
1819
.option("testReports", { describe: "Test reporting folder", type: "string" })
19-
.option("reuseDevice", { describe: "Reusing device if available.", type: "boolean", defualt: false })
20+
.option("reuseDevice", { describe: "Reusing device if available.", type: "boolean", defualt: true })
2021
.option("ignoreDeviceController", { alias: "i-ns-device-controller", describe: "Use default appium options for running emulatos/ simulators.", type: "boolean", defualt: false })
2122
.option("useDeviceControllerServer", {
2223
alias: "use-ns-device-controller-server",
@@ -47,7 +48,8 @@ const config = (() => {
4748
projectBinary: projectBinary,
4849
pluginRoot: pluginRoot,
4950
pluginBinary: pluginBinary,
50-
port: options.port || process.env.npm_config_port,
51+
port: options.port || process.env.npm_config_port || process.env["APPIUM_PORT"],
52+
wdaLocalPort: options.wdaLocalPort || process.env["WDA_LOCAL_PORT"],
5153
testFolder: options.testFolder || process.env.npm_config_testFolder || "e2e",
5254
runType: options.runType || process.env.npm_config_runType,
5355
appiumCapsLocation: options.appiumCapsLocation || join(projectDir, options.testFolder, "config", capabilitiesName),
@@ -57,9 +59,7 @@ const config = (() => {
5759
storage: options.storage || process.env.npm_config_STORAGE || process.env.STORAGE,
5860
testReports: options.testReports || process.env.npm_config_TEST_REPORTS || process.env.TEST_REPORTS,
5961
reuseDevice: options.reuseDevice || process.env.npm_config_REUSE_DEVICE || process.env.REUSE_DEVICE,
60-
ignoreDeviceController: options.ignoreDeviceController,
61-
useDeviceControllerServer: options.useDeviceControllerServer || process.env['USE_DEVICE_CONTROLLER_SERVER'],
62-
deviceControllerServerPort: parseInt(options.deviceControllerServerPort) || parseInt(process.env['DEVICE_CONTROLLER_SERVER_PORT']) || 8700
62+
ignoreDeviceController: options.ignoreDeviceController
6363
};
6464

6565
return config;
@@ -81,6 +81,5 @@ export const {
8181
testReports,
8282
reuseDevice,
8383
ignoreDeviceController,
84-
useDeviceControllerServer,
85-
deviceControllerServerPort
84+
wdaLocalPort
8685
} = config;

Diff for: lib/service/service-context.d.ts

-15
This file was deleted.

Diff for: lib/service/service-context.ts

-66
This file was deleted.

0 commit comments

Comments
 (0)