Skip to content

Tsenov/extend device support #123

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 3 commits into from
Jun 4, 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
5 changes: 5 additions & 0 deletions lib/appium-driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export declare class AppiumDriver {
*/
readonly storageByDeviceName: string;
static createAppiumDriver(port: number, args: INsCapabilities): Promise<AppiumDriver>;
private static applyDeviceAdditionsSettings(args, sessionIfno);
/**
*
* @param xPath
Expand Down Expand Up @@ -203,5 +204,9 @@ export declare class AppiumDriver {
* @param waitForElement
*/
findElementByAccessibilityIdIfExists(id: string, waitForElement?: number): Promise<UIElement>;
executeShellCommand(commandAndargs: {
command: string;
"args": Array<any>;
}): Promise<any>;
setDontKeepActivities(value: boolean): Promise<void>;
}
52 changes: 44 additions & 8 deletions lib/appium-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
DeviceController,
IDevice,
DeviceType,
AndroidController
AndroidController,
IOSController
} from "mobile-devices-controller";
import {
addExt,
Expand Down Expand Up @@ -179,11 +180,15 @@ export class AppiumDriver {

const driver = await wd.promiseChainRemote(driverConfig);
AppiumDriver.configureLogging(driver, args.verbose);

let hasStarted = false;
let retries = 10;
while (retries > 0 && !hasStarted) {
try {
await driver.init(args.appiumCaps);
const sessionIfno = await driver.init(args.appiumCaps);
log(sessionIfno, args.verbose);
AppiumDriver.applyDeviceAdditionsSettings(args, sessionIfno);

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

private static applyDeviceAdditionsSettings(args: INsCapabilities, sessionIfno: any) {
if (!args.device.config || !args.device.config.density || !args.device.config.offset) {
args.device.config = {};
const density: number = sessionIfno[1].deviceScreenDensity / 100;
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 @@ -600,15 +628,15 @@ export class AppiumDriver {
}

private async convertArrayToUIElements(array, searchM, args) {
let i = 0;
const arrayOfUIElements = new Array<UIElement>();
if (!array || array === null) {
return arrayOfUIElements;
}
array.forEach(async element => {
arrayOfUIElements.push(new UIElement(await element, this._driver, this._wd, this._webio, this._args, searchM, args, i));
i++;
});

for (let index = 0; index < array.length; index++) {
const element = array[index];
arrayOfUIElements.push(new UIElement(await element, this._driver, this._wd, this._webio, this._args, searchM, args, index));
}

return arrayOfUIElements;
}
Expand Down Expand Up @@ -705,9 +733,17 @@ export class AppiumDriver {
}
}

public async executeShellCommand(commandAndargs: { command: string, "args": Array<any> }) {
const output = await this._driver.execute("mobile: shell", commandAndargs);
return output;
}
public async setDontKeepActivities(value: boolean) {
if (this._args.isAndroid) {
AndroidController.setDontKeepActivities(value, this._args.device);
const status = value ? 1 : 0;
const output = await this.executeShellCommand({ command: "settings", args: ['put', 'global', 'always_finish_activities', status] });
//check if set
const check = await this.executeShellCommand({ command: "settings", args: ['get', 'global', 'always_finish_activities'] })
console.info(`always_finish_activities: ${check}`)
} else {
// Do nothing for iOS ...
}
Expand Down
3 changes: 2 additions & 1 deletion lib/appium-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export class AppiumServer {
}

private startAppiumServer(logLevel: string, isSauceLab: boolean) {
const startingServerArgs = isSauceLab ? [ "--log-level", logLevel] : ["-p", this.port.toString(), "--log-level", logLevel];
const startingServerArgs: Array<string> = isSauceLab ? ["--log-level", logLevel] : ["-p", this.port.toString(), "--log-level", logLevel];
startingServerArgs.push("--relaxed-security");
this._server = child_process.spawn(this._appium, startingServerArgs, {
shell: true,
detached: false
Expand Down
1 change: 0 additions & 1 deletion lib/frame-comparer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { mkdirSync } from "fs";
import { resolve, getStorageByDeviceName, getReportPath } from "./utils";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDevice } from "mobile-devices-controller";
import * as frComparer from "frame-comparer";
import { IRectangle } from "..";
import { ImageHelper } from "./image-helper";
Expand Down
2 changes: 1 addition & 1 deletion lib/image-helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export declare class ImageHelper {
blockOutAreas: IRectangle[];
imageOutputLimit(): ImageOptions;
thresholdType(): ImageOptions;
threshold(thresholdType: any): 10 | 0.01;
threshold(thresholdType: any): 0.01 | 10;
delta(): number;
static cropImageDefault(_args: INsCapabilities): {
x: number;
Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/ns-capabilities.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IDevice } from "mobile-devices-controller";
export declare enum AutomationName {
UiAutomator2 = "UIAutomator2",
Appium = "Appium",
XCUITest = "XCUITest"
XCUITest = "XCUITest",
}
export interface INsCapabilities {
projectDir: string;
Expand Down
8 changes: 4 additions & 4 deletions lib/ns-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ export class NsCapabilities implements INsCapabilities {

private setAutomationName() {
if (this.appiumCaps["automationName"]) {
switch (this.appiumCaps["automationName"]) {
case AutomationName.UiAutomator2.toString():
switch (this.appiumCaps["automationName"].toLowerCase()) {
case AutomationName.UiAutomator2.toString().toLowerCase():
this._automationName = AutomationName.UiAutomator2; break;
case AutomationName.Appium.toString():
case AutomationName.Appium.toString().toLowerCase():
this._automationName = AutomationName.Appium; break;
case AutomationName.XCUITest.toString():
case AutomationName.XCUITest.toString().toLowerCase():
this._automationName = AutomationName.XCUITest; break;
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ function getAppName(args: INsCapabilities) {
}

export function getAppPath(caps: INsCapabilities) {
let basePath = caps.appPath;
if (isFile(basePath)) {
let basePath = caps.appiumCaps.app || caps.appPath;
if (fs.existsSync(basePath) && ((basePath.endsWith(".apk") || basePath.endsWith(".app") || basePath.endsWith(".ipa")))) {
return basePath;
}

Expand Down
100 changes: 49 additions & 51 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
{
"name": "nativescript-dev-appium",
"version": "3.3.0",
"description": "A NativeScript plugin to help integrate and run Appium tests",
"author": "NativeScript",
"license": "MIT",
"main": "./index.js",
"directories": {
"lib": "./lib"
},
"repository": {
"type": "git",
"url": "https://github.com/NativeScript/nativescript-dev-appium.git"
},
"keywords": [
"nativescript",
"appium",
"test"
],
"maintainers": [
{
"name": "SvetoslavTsenov",
"email": "[email protected]"
"name": "nativescript-dev-appium",
"version": "3.3.0",
"description": "A NativeScript plugin to help integrate and run Appium tests",
"author": "NativeScript",
"license": "MIT",
"main": "./index.js",
"directories": {
"lib": "./lib"
},
"repository": {
"type": "git",
"url": "https://github.com/NativeScript/nativescript-dev-appium.git"
},
"keywords": [
"nativescript",
"appium",
"test"
],
"maintainers": [{
"name": "SvetoslavTsenov",
"email": "[email protected]"
}],
"dependencies": {
"app-root-path": "~2.0.1",
"blink-diff": "~1.0.13",
"chai": "~4.1.0",
"chai-as-promised": "~7.1.0",
"frame-comparer": "^1.0.3",
"glob": "7.1.0",
"mobile-devices-controller": "~2.5.0",
"mocha": "~5.1.0",
"mocha-junit-reporter": "~1.17.0",
"mocha-multi": "~1.0.0",
"wd": "~1.8.0",
"webdriverio": "~4.12.0",
"yargs": "~8.0.2"
},
"devDependencies": {
"@types/chai": "^4.1.3",
"@types/mocha": "^5.2.0",
"@types/node": "^8.10.13",
"@types/webdriverio": "~4.8.4",
"typescript": "^2.8.0"
},
"scripts": {
"postinstall": "node ./postinstall.js",
"prepare": "tsc",
"watch": "tsc --watch"
}
],
"dependencies": {
"app-root-path": "^2.0.1",
"blink-diff": "^1.0.13",
"chai": "^4.1.0",
"chai-as-promised": "^7.1.0",
"frame-comparer": "^1.0.3",
"glob": "7.1.0",
"mobile-devices-controller": "^2.5.0",
"mocha": "^5.1.0",
"mocha-junit-reporter": "^1.17.0",
"mocha-multi": "^1.0.0",
"wd": "^1.6.0",
"webdriverio": "^4.12.0",
"yargs": "^8.0.2"
},
"devDependencies": {
"@types/chai": "^4.1.3",
"@types/mocha": "^5.2.0",
"@types/node": "^8.10.13",
"@types/webdriverio": "~4.8.4",
"typescript": "^2.8.0"
},
"scripts": {
"postinstall": "node ./postinstall.js",
"prepare": "tsc",
"watch": "tsc --watch"
}
}
}
8 changes: 4 additions & 4 deletions postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function getDevDependencies() {
// There is need to explicitly install them to the project.
const typeScriptDevDependencies = [
//{ name: "tslib", version: "^1.7.1" },
{ name: "@types/chai", version: "^4.0.2" },
{ name: "@types/mocha", version: "^2.2.41" },
{ name: "@types/chai", version: "~4.1.3" },
{ name: "@types/mocha", version: "~5.2.1" },
{ name: "@types/node", version: "^7.0.5" },
];

Expand All @@ -100,7 +100,7 @@ function configureDevDependencies(packageJson) {
if (devDependenciesToInstall.length) {
console.info("Installing new devDependencies ...");
// Execute `npm install` after everything else
setTimeout(function () {
setTimeout(function() {
executeNpmInstall(appRootPath);
}, 300);
}
Expand Down Expand Up @@ -136,4 +136,4 @@ if (basename(appRootPath) !== "nativescript-dev-appium") {
console.info("JavaScript project - not adding sample config and test ...");
}
}
}
}