Skip to content

refactor: remove ServiceContext #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/appium-driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export declare class AppiumDriver {
private _logPath;
private _storageByDeviceName;
private _storageByPlatform;
private constructor(_driver, _wd, _webio, _driverConfig, _args);
private constructor();
readonly imageHelper: ImageHelper;
defaultWaitTime: number;
readonly capabilities: any;
Expand Down
6 changes: 2 additions & 4 deletions lib/appium-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { UIElement } from "./ui-element";
import { Direction } from "./direction";
import { Locator } from "./locators";
import { Platform } from "mobile-devices-controller";
import { ServiceContext } from "./service/service-context";
import {
addExt,
log,
Expand Down Expand Up @@ -470,9 +469,8 @@ export class AppiumDriver {

// It looks we need it for XCTest (iOS 10+ automation)
if (args.appiumCaps.platformVersion >= 10) {
let freePort = await findFreePort(10, (parseInt(args.appiumCaps["wdaLocalPort"])) || 8400, args);
console.log(" args.appiumCaps['wdaLocalPort']", freePort)
args.appiumCaps["wdaLocalPort"] = freePort;
console.log(`args.appiumCaps['wdaLocalPort']: ${args.wdaPort}`);
args.appiumCaps["wdaLocalPort"] = args.wdaPort;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/appium-server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export declare class AppiumServer {
readonly server: child_process.ChildProcess;
hasStarted: boolean;
start(port: any, deviceManager?: IDeviceManager): Promise<boolean>;
private startAppiumServer(logLevel);
stop(): Promise<{}>;
private resolveAppiumDependency();
}
24 changes: 14 additions & 10 deletions lib/appium-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDeviceManager } from "./interfaces/device-manager";
import { DeviceManger } from "./device-controller";
import { ServiceContext } from "../lib/service/service-context";

export class AppiumServer {
private _server: child_process.ChildProcess;
Expand Down Expand Up @@ -56,7 +55,7 @@ export class AppiumServer {
this._hasStarted = hasStarted;
}

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

let response: boolean = false;
this.startAppiumServer(logLevel);

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._args));

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

return response;
}

private startAppiumServer(logLevel) {
this._server = child_process.spawn(this._appium, ["-p", this.port.toString(), "--log-level", logLevel], {
shell: true,
detached: false
});
}

public async stop() {
await this._deviceManager.stopDevice(this._args);
return new Promise((resolve, reject) => {
Expand Down
4 changes: 1 addition & 3 deletions lib/device-controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDeviceManager } from "./interfaces/device-manager";
import { ServiceContext } from "./service/service-context";
import { IDevice } from "mobile-devices-controller";
export declare class DeviceManger implements IDeviceManager {
private _serveiceContext;
private static _emulators;
constructor(port: any, _serveiceContext?: ServiceContext);
constructor();
startDevice(args: INsCapabilities): Promise<IDevice>;
stopDevice(args: INsCapabilities): Promise<any>;
installApp(args: INsCapabilities): Promise<any>;
Expand Down
42 changes: 3 additions & 39 deletions lib/device-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {
log,
isWin,
shutdown,
executeCommand,
findFreePort
executeCommand
} from "./utils";
import * as child_process from "child_process";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDeviceManager } from "./interfaces/device-manager";
import { ServiceContext } from "./service/service-context";

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

constructor(port, private _serveiceContext: ServiceContext = undefined) {
if (!this._serveiceContext) {
this._serveiceContext = ServiceContext.createServer(port);
}
constructor() {
}

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

// Using serve to manage deivces.
if (args.useDeviceControllerServer) {
const d = await this._serveiceContext.subscribe(args.appiumCaps.deviceName, args.appiumCaps.platformName.toLowerCase(), args.appiumCaps.platformVersion, args.appiumCaps.app);
delete d['__v'];
delete d['_id']
if (!d || !(d as IDevice)) {
console.error("", d);
throw new Error("Missing device: " + d);
}
console.log(`Device:`, d);

return <any>d;
}

const allDevices = (await DeviceController.getDevices({ platform: args.appiumCaps.platformName }));
if (!allDevices || allDevices === null || allDevices.length === 0) {
console.log("We couldn't find any devices. We will try to proceed to appium! Maybe avd manager is missing")
Expand Down Expand Up @@ -111,27 +92,10 @@ export class DeviceManger implements IDeviceManager {
public async stopDevice(args: INsCapabilities): Promise<any> {
if (process.env["DEVICE_TOKEN"]) {
return;
}
if (args.useDeviceControllerServer) {
const d = await this._serveiceContext.unsubscribe(args.device.token);
if (!d) {
console.error("", d);
throw new Error("Missing device: " + d);
}

try {
await this._serveiceContext.releasePort(args.port);
if (args.appiumCaps.wdaLocalPort) {
await this._serveiceContext.releasePort(args.appiumCaps.wdaLocalPort);
}
} catch (error) {
console.log(error);
}
} else if (DeviceManger._emulators.has(args.runType)
&& !args.reuseDevice
&& !args.isSauceLab
&& !args.ignoreDeviceController
&& !args.useDeviceControllerServer) {
&& !args.ignoreDeviceController) {
const device = DeviceManger._emulators.get(args.runType);
await DeviceManger.kill(device);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/interfaces/ns-capabilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ export interface INsCapabilities {
reuseDevice: boolean;
device: IDevice;
ignoreDeviceController: boolean;
useDeviceControllerServer: boolean;
deviceControllerServerPort: number;
wdaLocalPort: number;
}
3 changes: 1 addition & 2 deletions lib/interfaces/ns-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ export interface INsCapabilities {
reuseDevice: boolean;
device: IDevice;
ignoreDeviceController: boolean;
useDeviceControllerServer: boolean;
deviceControllerServerPort: number;
wdaLocalPort: number;
}
6 changes: 2 additions & 4 deletions lib/ns-capabilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export declare class NsCapabilities implements INsCapabilities {
private _emulatorOptions;
private _device;
private _ignoreDeviceController;
private _useDeviceControllerServer;
private _deviceControllerServerPort;
private _wdaLocalPort;
private exceptions;
constructor();
readonly projectDir: any;
Expand All @@ -43,8 +42,7 @@ export declare class NsCapabilities implements INsCapabilities {
readonly isSauceLab: any;
appPath: string;
readonly ignoreDeviceController: boolean;
readonly useDeviceControllerServer: boolean;
readonly deviceControllerServerPort: number;
readonly wdaLocalPort: number;
device: IDevice;
readonly emulatorOptions: string;
private isAndroidPlatform();
Expand Down
9 changes: 3 additions & 6 deletions lib/ns-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export class NsCapabilities implements INsCapabilities {
private _emulatorOptions: string;
private _device: IDevice;
private _ignoreDeviceController: boolean;
private _useDeviceControllerServer: boolean;
private _deviceControllerServerPort: number;
private _wdaLocalPort: number;
private exceptions: Array<string> = new Array();

constructor() {
Expand All @@ -48,8 +47,7 @@ export class NsCapabilities implements INsCapabilities {
this._isIOS = !this._isAndroid;
this._isSauceLab = parser.isSauceLab;
this._ignoreDeviceController = parser.ignoreDeviceController;
this._useDeviceControllerServer = parser.useDeviceControllerServer;
this._deviceControllerServerPort = parser.deviceControllerServerPort;
this._wdaLocalPort = parser.wdaLocalPort;
this.resolveAppPath();
this.checkMandatoryCapabiliies();
this.throwExceptions();
Expand All @@ -73,8 +71,7 @@ export class NsCapabilities implements INsCapabilities {
get isSauceLab() { return this._isSauceLab; }
get appPath() { return this._appPath; }
get ignoreDeviceController() { return this._ignoreDeviceController; }
get useDeviceControllerServer() { return this._useDeviceControllerServer; }
get deviceControllerServerPort() { return this._deviceControllerServerPort; }
get wdaLocalPort() { return this._wdaLocalPort; }
set appPath(appPath: string) { this._appPath = appPath; }
get device() { return this._device; }
set device(device: IDevice) { this._device = device; }
Expand Down
2 changes: 1 addition & 1 deletion lib/parser.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export declare const capabilitiesName = "appium.capabilities.json";
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;
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;
15 changes: 7 additions & 8 deletions lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const config = (() => {
.option("testFolder", { describe: "e2e test folder name", default: "e2e", type: "string" })
.option("appiumCapsLocation", { describe: "Capabilities", type: "string" })
.option("sauceLab", { describe: "SauceLab", default: false, type: "boolean" })
.option("port", { alias: "p", describe: "Execution port", type: "string" })
.option("port", { alias: "p", describe: "Appium port", type: "string" })
.option("wdaLocalPort", { alias: "p", describe: "WDA port", type: "string" })
.option("verbose", { alias: "v", describe: "Log actions", type: "boolean" })
.option("path", { describe: "path", default: process.cwd(), type: "string" })
.option("appPath", { describe: "application path", type: "string" })
.option("storage", { describe: "Storage for images folder.", type: "string" })
.option("testReports", { describe: "Test reporting folder", type: "string" })
.option("reuseDevice", { describe: "Reusing device if available.", type: "boolean", defualt: false })
.option("reuseDevice", { describe: "Reusing device if available.", type: "boolean", defualt: true })
.option("ignoreDeviceController", { alias: "i-ns-device-controller", describe: "Use default appium options for running emulatos/ simulators.", type: "boolean", defualt: false })
.option("useDeviceControllerServer", {
alias: "use-ns-device-controller-server",
Expand Down Expand Up @@ -47,7 +48,8 @@ const config = (() => {
projectBinary: projectBinary,
pluginRoot: pluginRoot,
pluginBinary: pluginBinary,
port: options.port || process.env.npm_config_port,
port: options.port || process.env.npm_config_port || process.env["APPIUM_PORT"],
wdaLocalPort: options.wdaLocalPort || process.env["WDA_LOCAL_PORT"],
testFolder: options.testFolder || process.env.npm_config_testFolder || "e2e",
runType: options.runType || process.env.npm_config_runType,
appiumCapsLocation: options.appiumCapsLocation || join(projectDir, options.testFolder, "config", capabilitiesName),
Expand All @@ -57,9 +59,7 @@ const config = (() => {
storage: options.storage || process.env.npm_config_STORAGE || process.env.STORAGE,
testReports: options.testReports || process.env.npm_config_TEST_REPORTS || process.env.TEST_REPORTS,
reuseDevice: options.reuseDevice || process.env.npm_config_REUSE_DEVICE || process.env.REUSE_DEVICE,
ignoreDeviceController: options.ignoreDeviceController,
useDeviceControllerServer: options.useDeviceControllerServer || process.env['USE_DEVICE_CONTROLLER_SERVER'],
deviceControllerServerPort: parseInt(options.deviceControllerServerPort) || parseInt(process.env['DEVICE_CONTROLLER_SERVER_PORT']) || 8700
ignoreDeviceController: options.ignoreDeviceController
};

return config;
Expand All @@ -81,6 +81,5 @@ export const {
testReports,
reuseDevice,
ignoreDeviceController,
useDeviceControllerServer,
deviceControllerServerPort
wdaLocalPort
} = config;
15 changes: 0 additions & 15 deletions lib/service/service-context.d.ts

This file was deleted.

66 changes: 0 additions & 66 deletions lib/service/service-context.ts

This file was deleted.

Loading