Skip to content

Tsenov/relaxed security #126

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 4 commits into from
Jun 11, 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 index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export { Point } from "./lib/point";
export { SearchOptions } from "./lib/search-options";
export { Locator } from "./lib/locators";
export { Direction } from "./lib/direction";
export { DeviceManger } from "./lib/device-controller";
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";
Expand Down
9 changes: 5 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IDeviceManager } from "./lib/interfaces/device-manager";
import { shutdown, findFreePort } from "./lib/utils";
import * as frameComparerHelper from "./lib/frame-comparer";
import { FrameComparer } from "./lib/frame-comparer";
import { DeviceManager } from "./lib/device-manager";

export { AppiumDriver } from "./lib/appium-driver";
export { AppiumServer } from "./lib/appium-server";
Expand All @@ -15,7 +16,7 @@ export { Point } from "./lib/point";
export { SearchOptions } from "./lib/search-options";
export { Locator } from "./lib/locators";
export { Direction } from "./lib/direction";
export { DeviceManger } from "./lib/device-controller";
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";
Expand All @@ -25,7 +26,7 @@ const appiumServer = new AppiumServer(nsCapabilities);
let frameComparer: FrameComparer;
let appiumDriver = null;

const attachToExitProcessHoockup = (processToExitFrom, processName ) => {
const attachToExitProcessHoockup = (processToExitFrom, processName) => {
const signals = ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'];
signals.forEach(function (sig) {
Expand Down Expand Up @@ -71,7 +72,7 @@ export async function createDriver() {

// Make sure to turn off "Don't keep activities"
// in case of previous execution failure.
await appiumDriver.setDontKeepActivities(false);
await DeviceManager.setDontKeepActivities(nsCapabilities, appiumDriver, false);

return appiumDriver;
}
Expand All @@ -98,4 +99,4 @@ const killProcesses = async (code) => {

process.once("exit", async (code) => await killProcesses(code));

attachToExitProcessHoockup(process,"main process");
attachToExitProcessHoockup(process, "main process");
5 changes: 0 additions & 5 deletions lib/appium-driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export declare class AppiumDriver {
*/
readonly storageByDeviceName: string;
static createAppiumDriver(port: number, args: INsCapabilities): Promise<AppiumDriver>;
private static applyDeviceAdditionsSettings;
/**
*
* @param xPath
Expand Down Expand Up @@ -204,9 +203,5 @@ export declare class AppiumDriver {
* @param waitForElement
*/
findElementByAccessibilityIdIfExists(id: string, waitForElement?: number): Promise<UIElement>;
static executeShellCommand(driver: any, commandAndargs: {
command: string;
"args": Array<any>;
}): Promise<any>;
setDontKeepActivities(value: boolean): Promise<void>;
}
42 changes: 3 additions & 39 deletions lib/appium-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { ImageHelper } from "./image-helper";
import { ImageOptions } from "./image-options"
import { unlinkSync, writeFileSync } from "fs";
import * as webdriverio from "webdriverio";
import { DeviceManager } from "../lib/device-manager";

export class AppiumDriver {
private static pngFileExt = '.png';
Expand Down Expand Up @@ -187,7 +188,7 @@ export class AppiumDriver {
try {
const sessionIfno = await driver.init(args.appiumCaps);
log(sessionIfno, args.verbose);
await AppiumDriver.applyDeviceAdditionsSettings(driver, args, sessionIfno);
await DeviceManager.applyDeviceAdditionsSettings(driver, args, sessionIfno);

hasStarted = true;
} catch (error) {
Expand All @@ -209,34 +210,6 @@ export class AppiumDriver {
return new AppiumDriver(driver, wd, webio, driverConfig, args);
}

private static async applyDeviceAdditionsSettings(driver, args: INsCapabilities, sessionIfno: any) {
if (!args.device.config || !args.device.config.density || !args.device.config.offset) {
args.device.config = {};
let density: number = sessionIfno[1].deviceScreenDensity / 100;
console.log(`Get density from appium session: ${density}`);
if (!density) {
density = await AppiumDriver.executeShellCommand(driver, { command: "wm", args: ["density"] });
console.log(`Device density recieved from adb shell command ${density}`);
}
args.device.config['density'] = density;

if (args.appiumCaps.platformName.toLowerCase() === "android") {
args.device.config['offsetPixels'] = AndroidController.calculateScreenOffset(density);
} else {
IOSController.getDevicesScreenInfo().forEach((v, k, m) => {
if (args.device.name.includes(k)) {
args.device.config = {
density: args.device.config['density'] || v.density,
offsetPixels: v.actionBarHeight
};
}
});
}

console.log(`Device setting:`, args.device.config);
}
}

/**
*
* @param xPath
Expand Down Expand Up @@ -738,18 +711,9 @@ export class AppiumDriver {
}
}

public static async executeShellCommand(driver, commandAndargs: { command: string, "args": Array<any> }) {
const output = await driver.execute("mobile: shell", commandAndargs);
return output;
}

public async setDontKeepActivities(value: boolean) {
if (this._args.isAndroid) {
const status = value ? 1 : 0;
const output = await AppiumDriver.executeShellCommand(this._driver, { command: "settings", args: ['put', 'global', 'always_finish_activities', status] });
//check if set
const check = await AppiumDriver.executeShellCommand(this._driver, { command: "settings", args: ['get', 'global', 'always_finish_activities'] })
console.info(`always_finish_activities: ${check}`)
const output = await DeviceManager.setDontKeepActivities(this._args, this._driver, value);
} else {
// Do nothing for iOS ...
}
Expand Down
8 changes: 5 additions & 3 deletions lib/appium-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "./utils";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDeviceManager } from "./interfaces/device-manager";
import { DeviceManger } from "./device-controller";
import { DeviceManager } from "./device-manager";
import { AndroidController } from "mobile-devices-controller";

export class AppiumServer {
Expand Down Expand Up @@ -57,7 +57,7 @@ export class AppiumServer {
this._hasStarted = hasStarted;
}

public async start(port, deviceManager: IDeviceManager = new DeviceManger()) {
public async start(port, deviceManager: IDeviceManager = new DeviceManager()) {
await this.prepareDevice(deviceManager);
await this.prepareApp();

Expand Down Expand Up @@ -85,7 +85,9 @@ export class AppiumServer {

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");
if(this._args.isAndroid && this._args.ignoreDeviceController && !this._args.isSauceLab){
this._args.relaxedSecurity ? startingServerArgs.push("--relaxed-security") : console.log("'relaxedSecurity' is not enabled!\nTo enabled it use '--relaxedSecurity'!");
}
this._server = child_process.spawn(this._appium, startingServerArgs, {
shell: true,
detached: false
Expand Down
7 changes: 7 additions & 0 deletions lib/device-controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ export declare class DeviceManger implements IDeviceManager {
static kill(device: IDevice): Promise<void>;
private static getDefaultDevice;
getPackageId(device: IDevice, appPath: string): string;
static setDontKeepActivities(nsArgs: INsCapabilities, driver: any, value: boolean): Promise<void>;
static executeShellCommand(driver: any, commandAndargs: {
command: string;
"args": Array<any>;
includeStderr?: boolean;
}): Promise<any>;
static getDensity(nsArgs: any, driver: any): Promise<void>;
}
21 changes: 21 additions & 0 deletions lib/device-manager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDeviceManager } from "./interfaces/device-manager";
import { IDevice } from "mobile-devices-controller";
export declare class DeviceManager implements IDeviceManager {
private static _emulators;
constructor();
startDevice(args: INsCapabilities): Promise<IDevice>;
stopDevice(args: INsCapabilities): Promise<any>;
installApp(args: INsCapabilities): Promise<any>;
uninstallApp(args: INsCapabilities): Promise<any>;
static kill(device: IDevice): Promise<void>;
private static getDefaultDevice;
static setDontKeepActivities(args: INsCapabilities, driver: any, value: any): Promise<void>;
static executeShellCommand(driver: IDevice, commandAndargs: {
command: string;
"args": Array<any>;
}): Promise<any>;
static getDensity(args: INsCapabilities, driver: any): Promise<void>;
static applyDeviceAdditionsSettings(driver: any, args: INsCapabilities, sessionIfno: any): Promise<void>;
getPackageId(device: IDevice, appPath: string): string;
}
86 changes: 78 additions & 8 deletions lib/device-controller.ts → lib/device-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import {
DeviceType
} from "mobile-devices-controller";

export class DeviceManger implements IDeviceManager {
export class DeviceManager implements IDeviceManager {
private static _emulators: Map<string, IDevice> = new Map();

constructor() {
}

public async startDevice(args: INsCapabilities): Promise<IDevice> {
args.appiumCaps.platformName = args.appiumCaps.platformName.toLowerCase();
let device: IDevice = DeviceManger.getDefaultDevice(args);
let device: IDevice = DeviceManager.getDefaultDevice(args);
if (process.env["DEVICE_TOKEN"]) {
device.token = process.env["DEVICE_TOKEN"];
device.name = process.env["DEVICE_NAME"] || device.name;
Expand All @@ -41,8 +41,8 @@ export class DeviceManger implements IDeviceManager {

// When isSauceLab specified we simply do nothing;
if (args.isSauceLab || args.ignoreDeviceController) {
args.ignoreDeviceController = true;
DeviceManger._emulators.set(args.runType, device);
args.ignoreDeviceController = true;
DeviceManager._emulators.set(args.runType, device);
return device;
}

Expand Down Expand Up @@ -89,18 +89,18 @@ export class DeviceManger implements IDeviceManager {
}
}

DeviceManger._emulators.set(args.runType, device);
DeviceManager._emulators.set(args.runType, device);

return device;
}

public async stopDevice(args: INsCapabilities): Promise<any> {
if (DeviceManger._emulators.has(args.runType)
if (DeviceManager._emulators.has(args.runType)
&& !args.reuseDevice
&& !args.isSauceLab
&& !args.ignoreDeviceController) {
const device = DeviceManger._emulators.get(args.runType);
await DeviceManger.kill(device);
const device = DeviceManager._emulators.get(args.runType);
await DeviceManager.kill(device);
}
}

Expand Down Expand Up @@ -133,6 +133,76 @@ export class DeviceManger implements IDeviceManager {
return device;
}

public static async setDontKeepActivities(args: INsCapabilities, driver, value) {
if (args.isAndroid) {
if (!args.ignoreDeviceController) {
AndroidController.setDontKeepActivities(value, args.device);
} else if (args.relaxedSecurity) {
const status = value ? 1 : 0;
const output = await DeviceManager.executeShellCommand(driver, { command: "settings", args: ['put', 'global', 'always_finish_activities', status] });
//check if set
const check = await DeviceManager.executeShellCommand(driver, { command: "settings", args: ['get', 'global', 'always_finish_activities'] });
console.info(`always_finish_activities: ${check}`);
}
} else {
// Do nothing for iOS ...
}
}

public static async executeShellCommand(driver: IDevice, commandAndargs: { command: string, "args": Array<any> }) {
if (driver.platform.toLowerCase() === Platform.ANDROID) {
const output = await driver.execute("mobile: shell", commandAndargs);
return output;
}
return undefined;
}

public static async getDensity(args: INsCapabilities, driver) {
args.device.config = args.device.config || {};
if (args.appiumCaps.platformName.toLowerCase() === "android") {
if (!args.ignoreDeviceController) {
args.device.config.density = await AndroidController.getPhysicalDensity(args.device);
}

if (args.relaxedSecurity) {
args.device.config.density = await DeviceManager.executeShellCommand(driver, { command: "wm", args: ["density"] });
console.log(`Device density recieved from adb shell command ${args.device.config.density}`);
}

if (args.device.config.density) {
args.device.config['offsetPixels'] = AndroidController.calculateScreenOffset(args.device.config.density);
}
} else {
IOSController.getDevicesScreenInfo().forEach((v, k, m) => {
if (args.device.name.includes(k)) {
args.device.config = {
density: args.device.config['density'] || v.density,
offsetPixels: v.actionBarHeight
};
}
});
}
}

public static async applyDeviceAdditionsSettings(driver, args: INsCapabilities, sessionIfno: any) {
if (!args.device.config || !args.device.config.offsetPixels) {
args.device.config = {};
let density: number = sessionIfno[1].deviceScreenDensity ? sessionIfno[1].deviceScreenDensity / 100 : undefined;

if (density) {
console.log(`Get density from appium session: ${density}`);
args.device.config['density'] = density;
args.device.config['offsetPixels'] = AndroidController.calculateScreenOffset(args.device.config.density);
}

if (!density) {
await DeviceManager.getDensity(args, driver);
}

density ? console.log(`Device setting:`, args.device.config) : console.log(`Could not resolve device density. Please provide offset in appium config`);
}
}

public getPackageId(device: IDevice, appPath: string): string {
const appActivity = (device.type === DeviceType.EMULATOR || device.platform === Platform.ANDROID) ? AndroidController.getPackageId(appPath) : IOSController.getIOSPackageId(device.type, appPath);
return appActivity;
Expand Down
1 change: 1 addition & 0 deletions lib/interfaces/ns-capabilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface INsCapabilities {
wdaLocalPort: number;
path: string;
automationName: AutomationName;
relaxedSecurity: boolean;
}
1 change: 1 addition & 0 deletions lib/interfaces/ns-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export interface INsCapabilities {
wdaLocalPort: number;
path: string;
automationName: AutomationName;
relaxedSecurity: boolean
}
2 changes: 2 additions & 0 deletions lib/ns-capabilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export declare class NsCapabilities implements INsCapabilities {
private _device;
private _ignoreDeviceController;
private _wdaLocalPort;
private _relaxedSecurity;
private exceptions;
constructor();
readonly path: string;
Expand Down Expand Up @@ -53,6 +54,7 @@ export declare class NsCapabilities implements INsCapabilities {
readonly wdaLocalPort: number;
device: IDevice;
readonly emulatorOptions: string;
readonly relaxedSecurity: boolean;
private isAndroidPlatform;
private setAutomationName;
tryGetAndroidApiLevel(): number;
Expand Down
3 changes: 3 additions & 0 deletions lib/ns-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class NsCapabilities implements INsCapabilities {
private _device: IDevice;
private _ignoreDeviceController: boolean;
private _wdaLocalPort: number;
private _relaxedSecurity: boolean;
private exceptions: Array<string> = new Array();

constructor() {
Expand All @@ -54,6 +55,7 @@ export class NsCapabilities implements INsCapabilities {
this._ignoreDeviceController = parser.ignoreDeviceController;
this._wdaLocalPort = parser.wdaLocalPort;
this._path = parser.path;
this._relaxedSecurity = parser.relaxedSecurity;
this.setAutomationName();
this.resolveApplication();
this.checkMandatoryCapabiliies();
Expand Down Expand Up @@ -88,6 +90,7 @@ export class NsCapabilities implements INsCapabilities {
get device() { return this._device; }
set device(device: IDevice) { this._device = device; }
get emulatorOptions() { return (this._emulatorOptions || "-wipe-data -gpu on") }
get relaxedSecurity() { return this._relaxedSecurity }

private isAndroidPlatform() { return this._appiumCaps.platformName.toLowerCase().includes("android"); }

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, devMode: any, ignoreDeviceController: any, wdaLocalPort: any, path: any;
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, devMode: any, ignoreDeviceController: any, wdaLocalPort: any, path: any, relaxedSecurity: any;
Loading