Skip to content
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
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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 const nsCapabilities: INsCapabilities = new NsCapabilities(parser);

const appiumServer = new AppiumServer(nsCapabilities);
Expand Down
26 changes: 16 additions & 10 deletions lib/appium-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ export class AppiumServer {
response = await waitForOutput(this._server, /listener started/, /Error: listen/, 60000, true);
}

this.hasStarted = response;
return response;
} else if (!this._args.attachToDebug) {
return true;
}

return false;

}

private startAppiumServer(logLevel: string, isSauceLab: boolean) {
Expand All @@ -116,25 +116,30 @@ export class AppiumServer {
}

public async stop() {
await this._args.deviceManager.stopDevice(this._args);
const onServerKilled = (server, signal, code, verbose) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, verbose);
server && server.removeAllListeners();
}

await this._args.deviceManager.stopDevice(this._args.device, 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);
this._server.once("close", (code, signal) => {
onServerKilled(this._server, signal,code, this._args.verbose);
resolve();
});

this._server.on("exit", (code, signal) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
this._server.once("exit", (code, signal) => {
onServerKilled(this._server, signal,code, this._args.verbose);
resolve();
});

this._server.on("error", (code, signal) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
this._server.once("error", (code, signal) => {
onServerKilled(this._server, signal,code, this._args.verbose);
resolve();
});

this._server.on("disconnect", (code, signal) => {
log(`Appium terminated due signal: ${signal} and code: ${code}`, this._args.verbose);
this._server.once("disconnect", (code, signal) => {
onServerKilled(this._server, signal,code, this._args.verbose);
resolve();
});

Expand All @@ -149,6 +154,7 @@ export class AppiumServer {
this._server.kill("SIGINT");
this._server.kill("SIGINT");
this._server.kill("SIGKILL");
process.kill(this._server.pid, "SIGKILL");
shutdown(this._server, this._args.verbose);
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion lib/device-controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export declare class DeviceManger implements IDeviceManager {
private static _emulators;
constructor();
startDevice(args: INsCapabilities): Promise<IDevice>;
stopDevice(args: INsCapabilities): Promise<any>;
stopDevice(device: IDevice, args: INsCapabilities): Promise<any>;
installApp(args: INsCapabilities): Promise<any>;
uninstallApp(args: INsCapabilities): Promise<any>;
static kill(device: IDevice): Promise<void>;
Expand Down
4 changes: 2 additions & 2 deletions lib/device-manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export declare class DeviceManager implements IDeviceManager {
private static _emulators;
constructor();
startDevice(args: INsCapabilities): Promise<IDevice>;
stopDevice(args: INsCapabilities): Promise<any>;
stopDevice(device: IDevice, args: INsCapabilities): Promise<any>;
installApp(args: INsCapabilities): Promise<any>;
uninstallApp(args: INsCapabilities): Promise<any>;
static kill(device: IDevice): Promise<void>;
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): any;
static getDefaultDevice(args: INsCapabilities, deviceName?: string, token?: string, type?: DeviceType, platformVersion?: number): IDevice;
static setDontKeepActivities(args: INsCapabilities, driver: any, value: any): Promise<void>;
static executeShellCommand(driver: any, commandArgs: {
command: string;
Expand Down
16 changes: 9 additions & 7 deletions lib/device-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
AndroidController,
Platform,
Status,
DeviceType
DeviceType,
sortDescByApiLevelPredicate
} from "mobile-devices-controller";

export class DeviceManager implements IDeviceManager {
Expand All @@ -19,6 +20,7 @@ export class DeviceManager implements IDeviceManager {

public async startDevice(args: INsCapabilities): Promise<IDevice> {
args.appiumCaps.platformName = args.appiumCaps.platformName.toLowerCase();
const shouldFullyResetDevice = !args.appiumCaps.udid;
let device: IDevice = DeviceManager.getDefaultDevice(args);
const token = process.env["DEVICE_TOKEN"] || process.env.npm_config_deviceToken;
device.token = token && token.replace("emulator-", "");
Expand All @@ -41,7 +43,9 @@ export class DeviceManager implements IDeviceManager {

const searchQuery = args.appiumCaps.udid ? { token: args.appiumCaps.udid } : device;

const foundDevices = await DeviceController.getDevices(searchQuery);
const foundDevices = (await DeviceController.getDevices(searchQuery))
.sort((a, b) => sortDescByApiLevelPredicate(a, b));

if (!foundDevices || foundDevices.length === 0) {
logError("We couldn't find any devices of type: ", searchQuery);
logError("We will try to proceed to appium!");
Expand Down Expand Up @@ -88,7 +92,7 @@ export class DeviceManager implements IDeviceManager {
logInfo("Device is connected:", device)
}
if (device.status === Status.SHUTDOWN) {
await DeviceController.startDevice(device, startDeviceOptions);
await DeviceController.startDevice(device, startDeviceOptions, shouldFullyResetDevice);
try {
delete device.process;
} catch (error) { }
Expand All @@ -110,12 +114,10 @@ export class DeviceManager implements IDeviceManager {
return device;
}

public async stopDevice(args: INsCapabilities): Promise<any> {
if (DeviceManager._emulators.has(args.runType)
&& !args.reuseDevice
public async stopDevice(device: IDevice, args: INsCapabilities): Promise<any> {
if (!args.reuseDevice
&& !args.isSauceLab
&& !args.ignoreDeviceController) {
const device = DeviceManager._emulators.get(args.runType);
await DeviceManager.kill(device);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/device-manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { INsCapabilities } from "../interfaces/ns-capabilities";
import { IDevice } from "mobile-devices-controller";
export interface IDeviceManager {
startDevice(args: INsCapabilities): Promise<IDevice>;
stopDevice(args: INsCapabilities): Promise<IDevice>;
stopDevice(device: IDevice, args: INsCapabilities): Promise<IDevice>;
installApp(args: INsCapabilities): Promise<void>;
uninstallApp(args: INsCapabilities): Promise<void>;
getPackageId(device: any, appPath: any): string;
Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/device-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IDevice } from "mobile-devices-controller";

export interface IDeviceManager {
startDevice(args: INsCapabilities): Promise<IDevice>
stopDevice(args: INsCapabilities): Promise<IDevice>
stopDevice(device: IDevice, args: INsCapabilities): Promise<IDevice>
installApp(args: INsCapabilities): Promise<void>;
uninstallApp(args: INsCapabilities): Promise<void>;
getPackageId(device, appPath): string;
Expand Down
4 changes: 2 additions & 2 deletions lib/interfaces/ns-capabilities.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { INsCapabilitiesArgs } from "./ns-capabilities-args";
export interface INsCapabilities extends INsCapabilitiesArgs {
validateArgs(): void;
extend(args: INsCapabilitiesArgs): INsCapabilities;
validateArgs?: () => {};
extend?: (args: INsCapabilitiesArgs) => {};
}
6 changes: 3 additions & 3 deletions lib/interfaces/ns-capabilities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { INsCapabilitiesArgs } from "./ns-capabilities-args";

export interface INsCapabilities extends INsCapabilitiesArgs{
validateArgs(): void;
extend(args: INsCapabilitiesArgs): INsCapabilities;
export interface INsCapabilities extends INsCapabilitiesArgs {
validateArgs?: () => {};
extend?: (args: INsCapabilitiesArgs) => {};
}

94 changes: 32 additions & 62 deletions lib/ns-capabilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,51 @@ import { IDevice } from "mobile-devices-controller";
import { IDeviceManager } from "./interfaces/device-manager";
export declare class NsCapabilities implements INsCapabilities {
private _parser;
private _projectDir;
private _projectBinary;
private _pluginRoot;
private _pluginBinary;
private _port;
private _verbose;
private _appiumCapsLocation;
private _appiumCaps;
private _testFolder;
private _storage;
private _testReports;
private _reuseDevice;
private _devMode;
private _runType;
private _isSauceLab;
private _wdaLocalPort;
private _appName;
private _appPath;
private _path;
private _emulatorOptions;
private _sessionId;
private _capabilitiesName;
private _ignoreDeviceController;
private _relaxedSecurity;
private _cleanApp;
private _attachToDebug;
private _startSession;
private _isValidated;
private _automationName;
private _device;
private _deviceManager;
private _exceptions;
private _imagesPath;
constructor(_parser: INsCapabilitiesArgs);
readonly path: string;
readonly projectDir: string;
readonly projectBinary: string;
readonly pluginRoot: string;
readonly pluginBinary: string;
projectDir: string;
projectBinary: string;
pluginRoot: string;
pluginBinary: string;
port: number;
verbose: boolean;
readonly appiumCapsLocation: string;
appiumCapsLocation: string;
appiumCaps: any;
readonly testFolder: string;
readonly storage: string;
readonly testReports: any;
readonly reuseDevice: boolean;
readonly devMode: boolean;
readonly runType: string;
readonly isAndroid: any;
readonly isIOS: boolean;
readonly isSauceLab: boolean;
automationName: AutomationName;
readonly appPath: string;
testFolder: string;
storage: string;
testReports: any;
reuseDevice: boolean;
devMode: boolean;
runType: string;
isSauceLab: boolean;
wdaLocalPort: number;
appName: string;
appPath: string;
path: string;
emulatorOptions: string;
sessionId: string;
capabilitiesName: string;
ignoreDeviceController: boolean;
readonly wdaLocalPort: number;
relaxedSecurity: boolean;
cleanApp: boolean;
attachToDebug: boolean;
startSession: boolean;
isValidated: boolean;
device: IDevice;
readonly emulatorOptions: string;
readonly relaxedSecurity: boolean;
readonly cleanApp: boolean;
readonly attachToDebug: boolean;
sessionId: string;
readonly startSession: boolean;
deviceManager: IDeviceManager;
readonly isValidated: boolean;
readonly imagesPath: string;
exceptions: Array<string>;
imagesPath: string;
constructor(_parser: INsCapabilitiesArgs);
readonly isAndroid: any;
readonly isIOS: boolean;
automationName: AutomationName;
setAutomationNameFromString(automationName: String): void;
extend(args: INsCapabilities): this;
validateArgs(): void;
validateArgs(): any;
private isAndroidPlatform;
private shouldSetFullResetOption;
shouldSetFullResetOption(): void;
private setAutomationName;
tryGetAndroidApiLevel(): number;
private resolveApplication;
private checkMandatoryCapabiliies;
private checkMandatoryCapabilities;
private throwExceptions;
}
Loading