Best JavaScript code snippet using appium-android-driver
android-helper-specs.js
Source:android-helper-specs.js
...591 const opts = {tmpDir: '/tmp_dir', appPackage: 'pkg'};592 mocks.adb.expects('rimraf').withExactArgs(`${REMOTE_TEMP_PATH}/strings.json`).once();593 mocks.adb.expects('pullApk').withExactArgs(opts.appPackage, opts.tmpDir)594 .throws(`adb: error: remote object ${opts.appPackage} does not exist`);595 (await helpers.pushStrings('en', adb, opts)).should.be.deep.equal({});596 mocks.adb.verify();597 mocks.fs.verify();598 });599 it('should extracts string.xml and converts it to string.json and pushes it', async function () {600 const opts = {app: 'app', tmpDir: '/tmp_dir', appPackage: 'pkg'};601 mocks.adb.expects('rimraf').withExactArgs(`${REMOTE_TEMP_PATH}/strings.json`).once();602 mocks.fs.expects('exists').withExactArgs(opts.app).returns(true);603 mocks.fs.expects('rimraf').once();604 mocks.adb.expects('extractStringsFromApk').withArgs(opts.app, 'en')605 .returns({apkStrings: {id: 'string'}, localPath: 'local_path'});606 mocks.adb.expects('push').withExactArgs('local_path', REMOTE_TEMP_PATH).once();607 (await helpers.pushStrings('en', adb, opts)).should.be.deep.equal({id: 'string'});608 mocks.adb.verify();609 });610 it('should delete remote strings.json if app is not present', async function () {611 const opts = {app: 'app', tmpDir: '/tmp_dir', appPackage: 'pkg'};612 mocks.adb.expects('rimraf').withExactArgs(`${REMOTE_TEMP_PATH}/strings.json`).once();613 mocks.fs.expects('exists').withExactArgs(opts.app).returns(false);614 (await helpers.pushStrings('en', adb, opts)).should.be.deep.equal({});615 mocks.adb.verify();616 mocks.fs.verify();617 });618 it('should push an empty json object if app does not have strings.xml', async function () {619 const opts = {app: 'app', tmpDir: '/tmp_dir', appPackage: 'pkg'};620 mocks.adb.expects('rimraf').withExactArgs(`${REMOTE_TEMP_PATH}/strings.json`).once();621 mocks.fs.expects('exists').withExactArgs(opts.app).returns(true);622 mocks.fs.expects('rimraf').once();623 mocks.adb.expects('extractStringsFromApk').throws();624 mocks.adb.expects('shell').withExactArgs('echo', [`'{}' > ${REMOTE_TEMP_PATH}/strings.json`]);625 (await helpers.pushStrings('en', adb, opts)).should.be.deep.equal({});626 mocks.adb.verify();627 mocks.fs.verify();628 });629 }));630 describe('unlock', withMocks({adb, helpers, unlocker}, (mocks) => {631 it('should return if screen is already unlocked', async function () {632 mocks.adb.expects('isScreenLocked').withExactArgs().once()633 .returns(false);634 mocks.adb.expects('getApiLevel').never();635 mocks.adb.expects('startApp').never();636 await helpers.unlock(helpers, adb, {});637 mocks.adb.verify();638 });639 it('should start unlock app', async function () {...
ah1.js
Source:ah1.js
1import _ from 'lodash';2import path from 'path';3import { exec } from 'teen_process';4import { retry, retryInterval } from 'asyncbox';5import logger from './logger';6import { fs } from 'appium-support';7import { path as unicodeIMEPath } from 'appium-android-ime';8import { path as settingsApkPath } from 'io.appium.settings';9import { path as unlockApkPath } from 'appium-unlock';10import Bootstrap from 'appium-android-bootstrap';11import B from 'bluebird';12import ADB from 'appium-adb';13import { default as unlocker, PIN_UNLOCK, PASSWORD_UNLOCK,14 PATTERN_UNLOCK, FINGERPRINT_UNLOCK } from './unlock-helpers';15const PACKAGE_INSTALL_TIMEOUT = 90000; // milliseconds16const CHROME_BROWSER_PACKAGE_ACTIVITY = {17 chrome: {18 pkg: 'com.android.chrome',19 activity: 'com.google.android.apps.chrome.Main',20 },21 chromium: {22 pkg: 'org.chromium.chrome.shell',23 activity: '.ChromeShellActivity',24 },25 chromebeta: {26 pkg: 'com.chrome.beta',27 activity: 'com.google.android.apps.chrome.Main',28 },29 browser: {30 pkg: 'com.android.browser',31 activity: 'com.android.browser.BrowserActivity',32 },33 'chromium-browser': {34 pkg: 'org.chromium.chrome',35 activity: 'com.google.android.apps.chrome.Main',36 },37 'chromium-webview': {38 pkg: 'org.chromium.webview_shell',39 activity: 'org.chromium.webview_shell.WebViewBrowserActivity',40 },41 default: {42 pkg: 'com.android.chrome',43 activity: 'com.google.android.apps.chrome.Main',44 },45};46const SETTINGS_HELPER_PKG_ID = 'io.appium.settings';47const SETTINGS_HELPER_PKG_ACTIVITY = ".Settings";48const UNLOCK_HELPER_PKG_ID = 'io.appium.unlock';49const UNLOCK_HELPER_PKG_ACTIVITY = ".Unlock";50const UNICODE_IME_PKG_ID = 'io.appium.android.ime';51let helpers = {};52helpers.createBaseADB = async function (opts = {}) {53 // filter out any unwanted options sent in54 // this list should be updated as ADB takes more arguments55 const {56 javaVersion,57 adbPort,58 suppressKillServer,59 remoteAdbHost,60 clearDeviceLogsOnStart,61 adbExecTimeout,62 } = opts;63 return await ADB.createADB({64 javaVersion,65 adbPort,66 suppressKillServer,67 remoteAdbHost,68 clearDeviceLogsOnStart,69 adbExecTimeout,70 });71};72helpers.parseJavaVersion = function (stderr) {73 let lines = stderr.split("\n");74 for (let line of lines) {75 if (new RegExp(/(java|openjdk) version/).test(line)) {76 return line.split(" ")[2].replace(/"/g, '');77 }78 }79 return null;80};81helpers.getJavaVersion = async function (logVersion = true) {82 let {stderr} = await exec('java', ['-version']);83 let javaVer = helpers.parseJavaVersion(stderr);84 if (javaVer === null) {85 throw new Error("Could not get the Java version. Is Java installed?");86 }87 if (logVersion) {88 logger.info(`Java version is: ${javaVer}`);89 }90 return javaVer;91};92helpers.prepareEmulator = async function (adb, opts) {93 let {avd, avdArgs, language, locale, avdLaunchTimeout,94 avdReadyTimeout} = opts;95 if (!avd) {96 throw new Error("Cannot launch AVD without AVD name");97 }98 let avdName = avd.replace('@', '');99 let runningAVD = await adb.getRunningAVD(avdName);100 if (runningAVD !== null) {101 if (avdArgs && avdArgs.toLowerCase().indexOf("-wipe-data") > -1) {102 logger.debug(`Killing '${avdName}' because it needs to be wiped at start.`);103 await adb.killEmulator(avdName);104 } else {105 logger.debug("Not launching AVD because it is already running.");106 return;107 }108 }109 avdArgs = this.prepareAVDArgs(opts, adb, avdArgs);110 await adb.launchAVD(avd, avdArgs, language, locale, avdLaunchTimeout,111 avdReadyTimeout);112};113helpers.prepareAVDArgs = function (opts, adb, avdArgs) {114 let args = avdArgs ? [avdArgs] : [];115 if (!_.isUndefined(opts.networkSpeed)) {116 let networkSpeed = this.ensureNetworkSpeed(adb, opts.networkSpeed);117 args.push('-netspeed', networkSpeed);118 }119 if (opts.isHeadless) {120 args.push('-no-window');121 }122 return args.join(' ');123};124helpers.ensureNetworkSpeed = function (adb, networkSpeed) {125 if (_.values(adb.NETWORK_SPEED).indexOf(networkSpeed) !== -1) {126 return networkSpeed;127 }128 logger.warn(`Wrong network speed param ${networkSpeed}, using default: full. Supported values: ${_.values(adb.NETWORK_SPEED)}`);129 return adb.NETWORK_SPEED.FULL;130};131helpers.ensureDeviceLocale = async function (adb, language, country) {132 if (!_.isString(language) && !_.isString(country)) {133 logger.warn(`setDeviceLanguageCountry requires language or country.`);134 logger.warn(`Got language: '${language}' and country: '${country}'`);135 return;136 }137 await adb.setDeviceLanguageCountry(language, country);138 if (!await adb.ensureCurrentLocale(language, country)) {139 throw new Error(`Failed to set language: ${language} and country: ${country}`);140 }141};142helpers.getDeviceInfoFromCaps = async function (opts = {}) {143 // we can create a throwaway ADB instance here, so there is no dependency144 // on instantiating on earlier (at this point, we have no udid)145 // we can only use this ADB object for commands that would not be confused146 // if multiple devices are connected147 const adb = await helpers.createBaseADB(opts);148 let udid = opts.udid;149 let emPort = null;150 // a specific avd name was given. try to initialize with that151 if (opts.avd) {152 await helpers.prepareEmulator(adb, opts);153 udid = adb.curDeviceId;154 emPort = adb.emulatorPort;155 } else {156 // no avd given. lets try whatever's plugged in devices/emulators157 logger.info("Retrieving device list");158 let devices = await adb.getDevicesWithRetry();159 // udid was given, lets try to init with that device160 if (udid) {161 if (!_.includes(_.map(devices, 'udid'), udid)) {162 logger.errorAndThrow(`Device ${udid} was not in the list ` +163 `of connected devices`);164 }165 emPort = adb.getPortFromEmulatorString(udid);166 } else if (opts.platformVersion) {167 opts.platformVersion = `${opts.platformVersion}`.trim();168 // a platform version was given. lets try to find a device with the same os169 logger.info(`Looking for a device with Android '${opts.platformVersion}'`);170 // in case we fail to find something, give the user a useful log that has171 // the device udids and os versions so they know what's available172 let availDevicesStr = [];173 // first try started devices/emulators174 for (let device of devices) {175 // direct adb calls to the specific device176 await adb.setDeviceId(device.udid);177 let deviceOS = await adb.getPlatformVersion();178 // build up our info string of available devices as we iterate179 availDevicesStr.push(`${device.udid} (${deviceOS})`);180 // we do a begins with check for implied wildcard matching181 // eg: 4 matches 4.1, 4.0, 4.1.3-samsung, etc182 if (deviceOS.indexOf(opts.platformVersion) === 0) {183 udid = device.udid;184 break;185 }186 }187 // we couldn't find anything! quit188 if (!udid) {189 logger.errorAndThrow(`Unable to find an active device or emulator ` +190 `with OS ${opts.platformVersion}. The following ` +191 `are available: ` + availDevicesStr.join(', '));192 }193 emPort = adb.getPortFromEmulatorString(udid);194 } else {195 // a udid was not given, grab the first device we see196 udid = devices[0].udid;197 emPort = adb.getPortFromEmulatorString(udid);198 }199 }200 logger.info(`Using device: ${udid}`);201 return {udid, emPort};202};203// returns a new adb instance with deviceId set204helpers.createADB = async function (opts = {}) {205 const {udid, emPort} = opts;206 const adb = await helpers.createBaseADB(opts);207 adb.setDeviceId(udid);208 if (emPort) {209 adb.setEmulatorPort(emPort);210 }211 return adb;212};213helpers.validatePackageActivityNames = function (opts) {214 for (const key of ['appPackage', 'appActivity', 'appWaitPackage', 'appWaitActivity']) {215 const name = opts[key];216 if (!name) {217 continue;218 }219 const match = /([^\w.*,])+/.exec(name);220 if (!match) {221 continue;222 }223 logger.warn(`Capability '${key}' is expected to only include latin letters, digits, underscore, dot, comma and asterisk characters.`);224 logger.warn(`Current value '${name}' has non-matching character at index ${match.index}: '${name.substring(0, match.index + 1)}'`);225 }226};227helpers.getLaunchInfo = async function (adb, opts) {228 let {app, appPackage, appActivity, appWaitPackage, appWaitActivity} = opts;229 if (!app) {230 logger.warn("No app sent in, not parsing package/activity");231 return;232 }233 this.validatePackageActivityNames(opts);234 if (appPackage && appActivity) {235 return;236 }237 logger.debug("Parsing package and activity from app manifest");238 let {apkPackage, apkActivity} =239 await adb.packageAndLaunchActivityFromManifest(app);240 if (apkPackage && !appPackage) {241 appPackage = apkPackage;242 }243 if (!appWaitPackage) {244 appWaitPackage = appPackage;245 }246 if (apkActivity && !appActivity) {247 appActivity = apkActivity;248 }249 if (!appWaitActivity) {250 appWaitActivity = appActivity;251 }252 logger.debug(`Parsed package and activity are: ${apkPackage}/${apkActivity}`);253 return {appPackage, appWaitPackage, appActivity, appWaitActivity};254};255helpers.resetApp = async function (adb, opts = {}) {256 const {257 app,258 appPackage,259 fastReset,260 fullReset,261 androidInstallTimeout = PACKAGE_INSTALL_TIMEOUT,262 autoGrantPermissions,263 allowTestPackages264 } = opts;265 if (!appPackage) {266 throw new Error("'appPackage' option is required");267 }268 const isInstalled = await adb.isAppInstalled(appPackage);269 if (isInstalled) {270 try {271 await adb.forceStop(appPackage);272 } catch (ign) {}273 // fullReset has priority over fastReset274 if (!fullReset && fastReset) {275 const output = await adb.clear(appPackage);276 if (_.isString(output) && output.toLowerCase().includes('failed')) {277 throw new Error(`Cannot clear the application data of '${appPackage}'. Original error: ${output}`);278 }279 // executing `shell pm clear` resets previously assigned application permissions as well280 if (autoGrantPermissions) {281 try {282 await adb.grantAllPermissions(appPackage);283 } catch (error) {284 logger.error(`Unable to grant permissions requested. Original error: ${error.message}`);285 }286 }287 logger.debug(`Performed fast reset on the installed '${appPackage}' application (stop and clear)`);288 return;289 }290 }291 if (!app) {292 throw new Error("'app' option is required for reinstall");293 }294 logger.debug(`Running full reset on '${appPackage}' (reinstall)`);295 if (isInstalled) {296 await adb.uninstallApk(appPackage);297 }298 await adb.install(app, {299 grantPermissions: autoGrantPermissions,300 timeout: androidInstallTimeout,301 allowTestPackages,302 });303};304helpers.installApk = async function (adb, opts = {}) {305 const {306 app,307 appPackage,308 fastReset,309 fullReset,310 androidInstallTimeout = PACKAGE_INSTALL_TIMEOUT,311 autoGrantPermissions,312 allowTestPackages313 } = opts;314 if (!app || !appPackage) {315 throw new Error("'app' and 'appPackage' options are required");316 }317 if (fullReset) {318 await this.resetApp(adb, opts);319 return;320 }321 // There is no need to reset the newly installed app322 const shouldPerformFastReset = fastReset && await adb.isAppInstalled(appPackage);323 await adb.installOrUpgrade(app, appPackage, {324 grantPermissions: autoGrantPermissions,325 timeout: androidInstallTimeout,326 allowTestPackages,327 });328 if (shouldPerformFastReset) {329 logger.info(`Performing fast reset on '${appPackage}'`);330 await this.resetApp(adb, opts);331 }332};333/**334 * Installs an array of apks335 * @param {ADB} adb Instance of Appium ADB object336 * @param {Object} opts Opts defined in driver.js337 */338helpers.installOtherApks = async function (otherApps, adb, opts) {339 let {340 androidInstallTimeout = PACKAGE_INSTALL_TIMEOUT,341 autoGrantPermissions,342 allowTestPackages343 } = opts;344 // Install all of the APK's asynchronously345 await B.all(otherApps.map((otherApp) => {346 logger.debug(`Installing app: ${otherApp}`);347 return adb.installOrUpgrade(otherApp, null, {348 grantPermissions: autoGrantPermissions,349 timeout: androidInstallTimeout,350 allowTestPackages,351 });352 }));353};354helpers.initUnicodeKeyboard = async function (adb) {355 logger.debug('Enabling Unicode keyboard support');356 logger.debug("Pushing unicode ime to device...");357 try {358 await adb.install(unicodeIMEPath, {replace: false});359 } catch (err) {360 logger.info(`Performing full reinstall of ${UNICODE_IME_PKG_ID} as a possible fix for: ${err.message}`);361 await adb.uninstallApk(UNICODE_IME_PKG_ID);362 await adb.install(unicodeIMEPath, {replace: false});363 }364 // get the default IME so we can return back to it later if we want365 let defaultIME = await adb.defaultIME();366 logger.debug(`Unsetting previous IME ${defaultIME}`);367 const appiumIME = `${UNICODE_IME_PKG_ID}/.UnicodeIME`;368 logger.debug(`Setting IME to '${appiumIME}'`);369 await adb.enableIME(appiumIME);370 await adb.setIME(appiumIME);371 return defaultIME;372};373helpers.setMockLocationApp = async function (adb, app) {374 try {375 if (await adb.getApiLevel() < 23) {376 await adb.shell(['settings', 'put', 'secure', 'mock_location', '1']);377 } else {378 await adb.shell(['appops', 'set', app, 'android:mock_location', 'allow']);379 }380 } catch (err) {381 logger.warn(`Unable to set mock location for app '${app}': ${err.message}`);382 }383};384helpers.installHelperApp = async function (adb, apkPath, packageId, appName) {385 try {386 await adb.installOrUpgrade(apkPath, packageId, {grantPermissions: true});387 } catch (err) {388 logger.warn(`Ignored error while installing Appium ${appName} helper: ` +389 `'${err.message}'. Manually uninstalling the application ` +390 `with package id '${packageId}' may help. Expect some Appium ` +391 `features may not work as expected unless this problem is ` +392 `fixed.`);393 }394};395helpers.pushSettingsApp = async function (adb, throwError = false) {396 logger.debug("Pushing settings apk to device...");397 await helpers.installHelperApp(adb, settingsApkPath, SETTINGS_HELPER_PKG_ID, 'Settings');398 // Reinstall will stop the settings helper process anyway, so399 // there is no need to continue if the application is still running400 if (await adb.processExists(SETTINGS_HELPER_PKG_ID)) {401 logger.debug(`${SETTINGS_HELPER_PKG_ID} is already running. ` +402 `There is no need to reset its permissions.`);403 return;404 }405 // lauch io.appium.settings app due to settings failing to be set406 // if the app is not launched prior to start the session on android 7+407 // see https://github.com/appium/appium/issues/8957408 try {409 await adb.startApp({410 pkg: SETTINGS_HELPER_PKG_ID,411 activity: SETTINGS_HELPER_PKG_ACTIVITY,412 action: "android.intent.action.MAIN",413 category: "android.intent.category.LAUNCHER",414 flags: "0x10200000",415 stopApp: false,416 });417 } catch (err) {418 logger.warn(`Failed to launch settings app: ${err.message}`);419 if (throwError) {420 throw err;421 }422 }423};424helpers.pushUnlock = async function (adb) {425 logger.debug("Pushing unlock helper app to device...");426 await helpers.installHelperApp(adb, unlockApkPath, UNLOCK_HELPER_PKG_ID, 'Unlock');427};428/**429 * Extracts string.xml and converts it to string.json and pushes430 * it to /data/local/tmp/string.json on for use of bootstrap431 * If app is not present to extract string.xml it deletes remote strings.json432 * If app does not have strings.xml we push an empty json object to remote433 *434 * @param {?string} language - Language abbreviation, for example 'fr'. The default language435 * is used if this argument is not defined.436 * @param {Object} adb - The adb mofdule instance.437 * @param {Object} opts - Driver options dictionary.438 * @returns {Object} The dictionary, where string resourtces identifiers are keys439 * along with their corresponding values for the given language or an empty object440 * if no matching resources were extracted.441 */442helpers.pushStrings = async function (language, adb, opts) {443 const remoteDir = '/data/local/tmp';444 const stringsJson = 'strings.json';445 const remoteFile = `${remoteDir}/${stringsJson}`;446 // clean up remote string.json if present447 await adb.rimraf(remoteFile);448 if (_.isEmpty(opts.appPackage) || !(await fs.exists(opts.app))) {449 return {};450 }451 const stringsTmpDir = path.resolve(opts.tmpDir, opts.appPackage);452 try {453 logger.debug('Extracting strings from apk', opts.app, language, stringsTmpDir);454 const {apkStrings, localPath} = await adb.extractStringsFromApk(opts.app, language, stringsTmpDir);455 await adb.push(localPath, remoteDir);456 return apkStrings;457 } catch (err) {458 logger.warn(`Could not get strings, continuing anyway. Original error: ${err.message}`);459 await adb.shell('echo', [`'{}' > ${remoteFile}`]);460 } finally {461 await fs.rimraf(stringsTmpDir);462 }463 return {};464};465helpers.unlockWithUIAutomation = async function (driver, adb, unlockCapabilities) {466 let unlockType = unlockCapabilities.unlockType;467 if (!unlocker.isValidUnlockType(unlockType)) {468 throw new Error(`Invalid unlock type ${unlockType}`);469 }470 let unlockKey = unlockCapabilities.unlockKey;471 if (!unlocker.isValidKey(unlockType, unlockKey)) {472 throw new Error(`Missing unlockKey ${unlockKey} capability for unlockType ${unlockType}`);473 }474 const unlockMethod = {475 [PIN_UNLOCK]: unlocker.pinUnlock,476 [PASSWORD_UNLOCK]: unlocker.passwordUnlock,477 [PATTERN_UNLOCK]: unlocker.patternUnlock,478 [FINGERPRINT_UNLOCK]: unlocker.fingerprintUnlock479 }[unlockType];480 await unlockMethod(adb, driver, unlockCapabilities);481};482helpers.unlockWithHelperApp = async function (adb) {483 logger.info("Unlocking screen");484 try {485 await adb.forceStop(UNLOCK_HELPER_PKG_ID);486 } catch (e) {487 // Sometimes we can see the below error, but we can ignore it.488 // [W3C] Encountered internal error running command: Error: Error executing adbExec. Original error: 'Command 'adb -P 5037 -s emulator-5554 shell am force-stop io.appium.unlock' timed out after 20000ms'; Stderr: ''; Code: 'null'489 logger.warn(`An error in unlockWithHelperApp: ${e.message}`);490 }491 let startOpts = {492 pkg: UNLOCK_HELPER_PKG_ID,493 activity: UNLOCK_HELPER_PKG_ACTIVITY,494 action: "android.intent.action.MAIN",495 category: "android.intent.category.LAUNCHER",496 flags: "0x10200000",497 stopApp: false,498 retry: false,499 waitDuration: 1000500 };501 // Unlock succeed with a couple of retries.502 let firstRun = true;503 await retry(3, async function () {504 // To reduce a time to call adb.isScreenLocked() since `adb shell dumpsys window` is easy to hang adb commands505 if (firstRun) {506 firstRun = false;507 } else {508 try {509 if (!(await adb.isScreenLocked())) {510 return;511 }512 } catch (e) {513 logger.warn(`Error in isScreenLocked: ${e.message}`);514 logger.warn("\"adb shell dumpsys window\" command has timed out.");515 logger.warn("The reason of this timeout is the delayed adb response. Resetting adb server can improve it.");516 }517 }518 logger.info(`Launching ${UNLOCK_HELPER_PKG_ID}`);519 // The command takes too much time so we should not call the command over twice continuously.520 await adb.startApp(startOpts);521 });522};523helpers.unlock = async function (driver, adb, capabilities) {524 if (!(await adb.isScreenLocked())) {525 logger.info("Screen already unlocked, doing nothing");526 return;527 }528 logger.debug("Screen is locked, trying to unlock");529 if (_.isUndefined(capabilities.unlockType)) {530 logger.warn("Using app unlock, this is going to be deprecated!");531 await helpers.unlockWithHelperApp(adb);532 } else {533 await helpers.unlockWithUIAutomation(driver, adb, {unlockType: capabilities.unlockType, unlockKey: capabilities.unlockKey});534 await helpers.verifyUnlock(adb);535 }536};537helpers.verifyUnlock = async function (adb) {538 await retryInterval(2, 1000, async () => {539 if (await adb.isScreenLocked()) {540 throw new Error("Screen did not unlock successfully, retrying");541 }542 logger.debug("Screen unlocked successfully");543 });544};545helpers.initDevice = async function (adb, opts) {546 await adb.waitForDevice();547 // pushSettingsApp required before calling ensureDeviceLocale for API Level 24+548 await helpers.pushSettingsApp(adb);549 if (!opts.avd) {550 await helpers.setMockLocationApp(adb, SETTINGS_HELPER_PKG_ID);551 }552 await helpers.ensureDeviceLocale(adb, opts.language, opts.locale);553 await adb.startLogcat();554 let defaultIME;555 if (opts.unicodeKeyboard) {556 defaultIME = await helpers.initUnicodeKeyboard(adb);557 }558 if (_.isUndefined(opts.unlockType)) {559 await helpers.pushUnlock(adb);560 }561 return defaultIME;562};563helpers.removeNullProperties = function (obj) {564 for (let key of _.keys(obj)) {565 if (_.isNull(obj[key]) || _.isUndefined(obj[key])) {566 delete obj[key];567 }568 }569};570helpers.truncateDecimals = function (number, digits) {571 let multiplier = Math.pow(10, digits),572 adjustedNum = number * multiplier,573 truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);574 return truncatedNum / multiplier;575};576helpers.isChromeBrowser = function (browser) {577 return _.includes(Object.keys(CHROME_BROWSER_PACKAGE_ACTIVITY), (browser || '').toLowerCase());578};579helpers.getChromePkg = function (browser) {580 return CHROME_BROWSER_PACKAGE_ACTIVITY[browser.toLowerCase()] ||581 CHROME_BROWSER_PACKAGE_ACTIVITY.default;582};583helpers.removeAllSessionWebSocketHandlers = async function (server, sessionId) {584 if (!server || !_.isFunction(server.getWebSocketHandlers)) {585 return;586 }587 const activeHandlers = await server.getWebSocketHandlers(sessionId);588 for (const pathname of _.keys(activeHandlers)) {589 await server.removeWebSocketHandler(pathname);590 }591};592/**593 * Takes a desired capability and tries to JSON.parse it as an array,594 * and either returns the parsed array or a singleton array.595 *596 * @param {any} cap A desired capability597 */598helpers.parseArray = function (cap) {599 let parsedCaps;600 try {601 parsedCaps = JSON.parse(cap);602 } catch (ign) { }603 if (_.isArray(parsedCaps)) {604 return parsedCaps;605 } else if (_.isString(cap)) {606 return [cap];607 }608 throw new Error(`must provide a string or JSON Array; received ${cap}`);609};610helpers.validateDesiredCaps = function (caps) {611 // make sure that the capabilities have one of `app`, `appPackage` or `browser`612 if ((!caps.browserName || !this.isChromeBrowser(caps.browserName)) && !caps.app && !caps.appPackage) {613 logger.errorAndThrow('The desired capabilities must include either an app, appPackage or browserName');614 }615 if (caps.browserName) {616 if (caps.app) {617 // warn if the capabilities have both `app` and `browser, although this is common with selenium grid618 logger.warn('The desired capabilities should generally not include both an app and a browserName');619 }620 if (caps.appPackage) {621 logger.errorAndThrow(`The desired capabilities must include either 'appPackage' or 'browserName'`);622 }623 }624 return true;625};626helpers.bootstrap = Bootstrap;627helpers.unlocker = unlocker;...
android-helpers.js
Source:android-helpers.js
1import _ from 'lodash';2import path from 'path';3import { exec } from 'teen_process';4import { retry, retryInterval } from 'asyncbox';5import logger from './logger';6import { fs } from 'appium-support';7import { path as unicodeIMEPath } from 'appium-android-ime';8import { path as settingsApkPath } from 'io.appium.settings';9import { path as unlockApkPath } from 'appium-unlock';10import Bootstrap from 'appium-android-bootstrap';11import B from 'bluebird';12import ADB from 'appium-adb';13import { default as unlocker, PIN_UNLOCK, PASSWORD_UNLOCK, PATTERN_UNLOCK, FINGERPRINT_UNLOCK } from './unlock-helpers';14const PACKAGE_INSTALL_TIMEOUT = 90000; // milliseconds15const CHROME_BROWSER_PACKAGE_ACTIVITY = {16 chrome: {17 pkg: 'com.android.chrome',18 activity: 'com.google.android.apps.chrome.Main',19 },20 chromium: {21 pkg: 'org.chromium.chrome.shell',22 activity: '.ChromeShellActivity',23 },24 chromebeta: {25 pkg: 'com.chrome.beta',26 activity: 'com.google.android.apps.chrome.Main',27 },28 browser: {29 pkg: 'com.android.browser',30 activity: 'com.android.browser.BrowserActivity',31 },32 'chromium-browser': {33 pkg: 'org.chromium.chrome',34 activity: 'com.google.android.apps.chrome.Main',35 },36 'chromium-webview': {37 pkg: 'org.chromium.webview_shell',38 activity: 'org.chromium.webview_shell.WebViewBrowserActivity',39 },40 default: {41 pkg: 'com.android.chrome',42 activity: 'com.google.android.apps.chrome.Main',43 },44};45const SETTINGS_HELPER_PKG_ID = 'io.appium.settings';46const SETTINGS_HELPER_PKG_ACTIVITY = ".Settings";47const UNLOCK_HELPER_PKG_ID = 'io.appium.unlock';48const UNLOCK_HELPER_PKG_ACTIVITY = ".Unlock";49let helpers = {};50helpers.parseJavaVersion = function (stderr) {51 let lines = stderr.split("\n");52 for (let line of lines) {53 if (new RegExp(/(java|openjdk) version/).test(line)) {54 return line.split(" ")[2].replace(/"/g, '');55 }56 }57 return null;58};59helpers.getJavaVersion = async function () {60 logger.debug("Getting Java version");61 let {stderr} = await exec('java', ['-version']);62 let javaVer = helpers.parseJavaVersion(stderr);63 if (javaVer === null) {64 throw new Error("Could not get the Java version. Is Java installed?");65 }66 logger.info(`Java version is: ${javaVer}`);67 return javaVer;68};69helpers.prepareEmulator = async function (adb, opts) {70 let {avd, avdArgs, language, locale, avdLaunchTimeout,71 avdReadyTimeout} = opts;72 if (!avd) {73 throw new Error("Cannot launch AVD without AVD name");74 }75 let avdName = avd.replace('@', '');76 let runningAVD = await adb.getRunningAVD(avdName);77 if (runningAVD !== null) {78 if (avdArgs && avdArgs.toLowerCase().indexOf("-wipe-data") > -1) {79 logger.debug(`Killing '${avdName}' because it needs to be wiped at start.`);80 await adb.killEmulator(avdName);81 } else {82 logger.debug("Not launching AVD because it is already running.");83 return;84 }85 }86 avdArgs = this.prepareAVDArgs(opts, adb, avdArgs);87 await adb.launchAVD(avd, avdArgs, language, locale, avdLaunchTimeout,88 avdReadyTimeout);89};90helpers.prepareAVDArgs = function (opts, adb, avdArgs) {91 let args = avdArgs ? [avdArgs] : [];92 if (!_.isUndefined(opts.networkSpeed)) {93 let networkSpeed = this.ensureNetworkSpeed(adb, opts.networkSpeed);94 args.push('-netspeed', networkSpeed);95 }96 if (opts.isHeadless) {97 args.push('-no-window');98 }99 return args.join(' ');100};101helpers.ensureNetworkSpeed = function (adb, networkSpeed) {102 if (_.values(adb.NETWORK_SPEED).indexOf(networkSpeed) !== -1) {103 return networkSpeed;104 }105 logger.warn(`Wrong network speed param ${networkSpeed}, using default: full. Supported values: ${_.values(adb.NETWORK_SPEED)}`);106 return adb.NETWORK_SPEED.FULL;107};108helpers.ensureDeviceLocale = async function (adb, language, country) {109 if (!_.isString(language) && !_.isString(country)) {110 logger.warn(`setDeviceLanguageCountry requires language or country.`);111 logger.warn(`Got language: '${language}' and country: '${country}'`);112 return;113 }114 await adb.setDeviceLanguageCountry(language, country);115 if (!await adb.ensureCurrentLocale(language, country)) {116 throw new Error(`Failed to set language: ${language} and country: ${country}`);117 }118};119helpers.getDeviceInfoFromCaps = async function (opts = {}) {120 // we can create a throwaway ADB instance here, so there is no dependency121 // on instantiating on earlier (at this point, we have no udid)122 // we can only use this ADB object for commands that would not be confused123 // if multiple devices are connected124 let adb = await ADB.createADB({125 javaVersion: opts.javaVersion,126 adbPort: opts.adbPort,127 remoteAdbHost: opts.remoteAdbHost,128 suppressKillServer: opts.suppressKillServer,129 clearDeviceLogsOnStart: opts.clearDeviceLogsOnStart,130 });131 let udid = opts.udid;132 let emPort = null;133 // a specific avd name was given. try to initialize with that134 if (opts.avd) {135 await helpers.prepareEmulator(adb, opts);136 udid = adb.curDeviceId;137 emPort = adb.emulatorPort;138 } else {139 // no avd given. lets try whatever's plugged in devices/emulators140 logger.info("Retrieving device list");141 let devices = await adb.getDevicesWithRetry();142 // udid was given, lets try to init with that device143 if (udid) {144 if (!_.includes(_.map(devices, 'udid'), udid)) {145 logger.errorAndThrow(`Device ${udid} was not in the list ` +146 `of connected devices`);147 }148 emPort = adb.getPortFromEmulatorString(udid);149 } else if (opts.platformVersion) {150 opts.platformVersion = `${opts.platformVersion}`.trim();151 // a platform version was given. lets try to find a device with the same os152 logger.info(`Looking for a device with Android '${opts.platformVersion}'`);153 // in case we fail to find something, give the user a useful log that has154 // the device udids and os versions so they know what's available155 let availDevicesStr = [];156 // first try started devices/emulators157 for (let device of devices) {158 // direct adb calls to the specific device159 await adb.setDeviceId(device.udid);160 let deviceOS = await adb.getPlatformVersion();161 // build up our info string of available devices as we iterate162 availDevicesStr.push(`${device.udid} (${deviceOS})`);163 // we do a begins with check for implied wildcard matching164 // eg: 4 matches 4.1, 4.0, 4.1.3-samsung, etc165 if (deviceOS.indexOf(opts.platformVersion) === 0) {166 udid = device.udid;167 break;168 }169 }170 // we couldn't find anything! quit171 if (!udid) {172 logger.errorAndThrow(`Unable to find an active device or emulator ` +173 `with OS ${opts.platformVersion}. The following ` +174 `are available: ` + availDevicesStr.join(', '));175 }176 emPort = adb.getPortFromEmulatorString(udid);177 } else {178 // a udid was not given, grab the first device we see179 udid = devices[0].udid;180 emPort = adb.getPortFromEmulatorString(udid);181 }182 }183 logger.info(`Using device: ${udid}`);184 return {udid, emPort};185};186// returns a new adb instance with deviceId set187helpers.createADB = async function (javaVersion, udid, emPort, adbPort, suppressKillServer, remoteAdbHost, clearDeviceLogsOnStart) {188 let adb = await ADB.createADB({189 javaVersion,190 adbPort,191 suppressKillServer,192 remoteAdbHost,193 clearDeviceLogsOnStart,194 });195 adb.setDeviceId(udid);196 if (emPort) {197 adb.setEmulatorPort(emPort);198 }199 return adb;200};201helpers.getLaunchInfo = async function (adb, opts) {202 let {app, appPackage, appActivity, appWaitPackage, appWaitActivity} = opts;203 if (!app) {204 logger.warn("No app sent in, not parsing package/activity");205 return;206 }207 if (appPackage && appActivity) {208 return;209 }210 logger.debug("Parsing package and activity from app manifest");211 let {apkPackage, apkActivity} =212 await adb.packageAndLaunchActivityFromManifest(app);213 if (apkPackage && !appPackage) {214 appPackage = apkPackage;215 }216 if (!appWaitPackage) {217 appWaitPackage = appPackage;218 }219 if (apkActivity && !appActivity) {220 appActivity = apkActivity;221 }222 if (!appWaitActivity) {223 appWaitActivity = appActivity;224 }225 logger.debug(`Parsed package and activity are: ${apkPackage}/${apkActivity}`);226 return {appPackage, appWaitPackage, appActivity, appWaitActivity};227};228helpers.resetApp = async function (adb, opts = {}) {229 const {app, appPackage, fastReset, fullReset,230 androidInstallTimeout = PACKAGE_INSTALL_TIMEOUT,231 autoGrantPermissions} = opts;232 if (!appPackage) {233 throw new Error("'appPackage' option is required");234 }235 const isInstalled = await adb.isAppInstalled(appPackage);236 if (isInstalled) {237 try {238 await adb.forceStop(appPackage);239 } catch (ign) {}240 // fullReset has priority over fastReset241 if (!fullReset && fastReset) {242 const output = await adb.clear(appPackage);243 if (_.isString(output) && output.toLowerCase().includes('failed')) {244 throw new Error(`Cannot clear the application data of '${appPackage}'. Original error: ${output}`);245 }246 // executing `shell pm clear` resets previously assigned application permissions as well247 if (autoGrantPermissions) {248 try {249 await adb.grantAllPermissions(appPackage);250 } catch (error) {251 logger.error(`Unable to grant permissions requested. Original error: ${error.message}`);252 }253 }254 logger.debug(`Performed fast reset on the installed '${appPackage}' application (stop and clear)`);255 return;256 }257 }258 if (!app) {259 throw new Error("'app' option is required for reinstall");260 }261 logger.debug(`Running full reset on '${appPackage}' (reinstall)`);262 if (isInstalled) {263 await adb.uninstallApk(appPackage);264 }265 await adb.install(app, {266 grantPermissions: autoGrantPermissions,267 timeout: androidInstallTimeout268 });269};270helpers.installApk = async function (adb, opts = {}) {271 const {app, appPackage, fastReset, fullReset,272 androidInstallTimeout = PACKAGE_INSTALL_TIMEOUT,273 autoGrantPermissions} = opts;274 if (!app || !appPackage) {275 throw new Error("'app' and 'appPackage' options are required");276 }277 if (fullReset) {278 await this.resetApp(adb, opts);279 return;280 }281 // There is no need to reset the newly installed app282 const shouldPerformFastReset = fastReset && await adb.isAppInstalled(appPackage);283 await adb.installOrUpgrade(app, appPackage, {284 grantPermissions: autoGrantPermissions,285 timeout: androidInstallTimeout286 });287 if (shouldPerformFastReset) {288 logger.info(`Performing fast reset on '${appPackage}'`);289 await this.resetApp(adb, opts);290 }291};292/**293 * Installs an array of apks294 * @param {ADB} adb Instance of Appium ADB object295 * @param {Object} opts Opts defined in driver.js296 */297helpers.installOtherApks = async function (otherApps, adb, opts) {298 let {299 androidInstallTimeout = PACKAGE_INSTALL_TIMEOUT,300 autoGrantPermissions301 } = opts;302 // Install all of the APK's asynchronously303 await B.all(otherApps.map((otherApp) => {304 logger.debug(`Installing app: ${otherApp}`);305 return adb.installOrUpgrade(otherApp, null, {306 grantPermissions: autoGrantPermissions,307 timeout: androidInstallTimeout,308 });309 }));310};311helpers.initUnicodeKeyboard = async function (adb) {312 logger.debug('Enabling Unicode keyboard support');313 logger.debug("Pushing unicode ime to device...");314 // await adb.install(unicodeIMEPath, {replace: false});315 // get the default IME so we can return back to it later if we want316 let defaultIME = await adb.defaultIME();317 logger.debug(`Unsetting previous IME ${defaultIME}`);318 const appiumIME = 'io.appium.android.ime/.UnicodeIME';319 logger.debug(`Setting IME to '${appiumIME}'`);320 await adb.enableIME(appiumIME);321 await adb.setIME(appiumIME);322 return defaultIME;323};324helpers.setMockLocationApp = async function (adb, app) {325 try {326 if (await adb.getApiLevel() < 23) {327 await adb.shell(['settings', 'put', 'secure', 'mock_location', '1']);328 } else {329 await adb.shell(['appops', 'set', app, 'android:mock_location', 'allow']);330 }331 } catch (err) {332 logger.warn(`Unable to set mock location for app '${app}': ${err.message}`);333 }334};335helpers.installHelperApp = async function (adb, apkPath, packageId, appName) {336 try {337 await adb.installOrUpgrade(apkPath, packageId, {grantPermissions: true});338 } catch (err) {339 logger.warn(`Ignored error while installing Appium ${appName} helper: ` +340 `'${err.message}'. Manually uninstalling the application ` +341 `with package id '${packageId}' may help. Expect some Appium ` +342 `features may not work as expected unless this problem is ` +343 `fixed.`);344 }345};346helpers.pushSettingsApp = async function (adb, throwError = false) {347 logger.debug("Pushing settings apk to device...");348 await helpers.installHelperApp(adb, settingsApkPath, SETTINGS_HELPER_PKG_ID, 'Settings');349 // Reinstall will stop the settings helper process anyway, so350 // there is no need to continue if the application is still running351 if (await adb.processExists(SETTINGS_HELPER_PKG_ID)) {352 logger.debug(`${SETTINGS_HELPER_PKG_ID} is already running. ` +353 `There is no need to reset its permissions.`);354 return;355 }356 // lauch io.appium.settings app due to settings failing to be set357 // if the app is not launched prior to start the session on android 7+358 // see https://github.com/appium/appium/issues/8957359 try {360 await adb.startApp({361 pkg: SETTINGS_HELPER_PKG_ID,362 activity: SETTINGS_HELPER_PKG_ACTIVITY,363 action: "android.intent.action.MAIN",364 category: "android.intent.category.LAUNCHER",365 flags: "0x10200000",366 stopApp: false,367 });368 } catch (err) {369 logger.warn(`Failed to launch settings app: ${err.message}`);370 if (throwError) {371 throw err;372 }373 }374};375helpers.pushUnlock = async function (adb) {376 logger.debug("Pushing unlock helper app to device...");377 await helpers.installHelperApp(adb, unlockApkPath, UNLOCK_HELPER_PKG_ID, 'Unlock');378};379/**380 * Extracts string.xml and converts it to string.json and pushes381 * it to /data/local/tmp/string.json on for use of bootstrap382 * If app is not present to extract string.xml it deletes remote strings.json383 * If app does not have strings.xml we push an empty json object to remote384 *385 * @param {?string} language - Language abbreviation, for example 'fr'. The default language386 * is used if this argument is not defined.387 * @param {Object} adb - The adb mofdule instance.388 * @param {Object} opts - Driver options dictionary.389 * @returns {Object} The dictionary, where string resourtces identifiers are keys390 * along with their corresponding values for the given language or an empty object391 * if no matching resources were extracted.392 */393helpers.pushStrings = async function (language, adb, opts) {394 const remoteDir = '/data/local/tmp';395 const stringsJson = 'strings.json';396 const remoteFile = `${remoteDir}/${stringsJson}`;397 // clean up remote string.json if present398 await adb.rimraf(remoteFile);399 if (_.isEmpty(opts.appPackage) || !(await fs.exists(opts.app))) {400 return {};401 }402 const stringsTmpDir = path.resolve(opts.tmpDir, opts.appPackage);403 try {404 logger.debug('Extracting strings from apk', opts.app, language, stringsTmpDir);405 const {apkStrings, localPath} = await adb.extractStringsFromApk(opts.app, language, stringsTmpDir);406 await adb.push(localPath, remoteDir);407 return apkStrings;408 } catch (err) {409 logger.warn(`Could not get strings, continuing anyway. Original error: ${err.message}`);410 await adb.shell('echo', [`'{}' > ${remoteFile}`]);411 } finally {412 await fs.rimraf(stringsTmpDir);413 }414 return {};415};416helpers.unlockWithUIAutomation = async function (driver, adb, unlockCapabilities) {417 let unlockType = unlockCapabilities.unlockType;418 if (!unlocker.isValidUnlockType(unlockType)) {419 throw new Error(`Invalid unlock type ${unlockType}`);420 }421 let unlockKey = unlockCapabilities.unlockKey;422 if (!unlocker.isValidKey(unlockType, unlockKey)) {423 throw new Error(`Missing unlockKey ${unlockKey} capability for unlockType ${unlockType}`);424 }425 const unlockMethod = {426 [PIN_UNLOCK]: unlocker.pinUnlock,427 [PASSWORD_UNLOCK]: unlocker.passwordUnlock,428 [PATTERN_UNLOCK]: unlocker.patternUnlock,429 [FINGERPRINT_UNLOCK]: unlocker.fingerprintUnlock430 }[unlockType];431 await unlockMethod(adb, driver, unlockCapabilities);432};433helpers.unlockWithHelperApp = async function (adb) {434 logger.info("Unlocking screen");435 try {436 await adb.forceStop(UNLOCK_HELPER_PKG_ID);437 } catch (e) {438 // Sometimes we can see the below error, but we can ignore it.439 // [W3C] Encountered internal error running command: Error: Error executing adbExec. Original error: 'Command 'adb -P 5037 -s emulator-5554 shell am force-stop io.appium.unlock' timed out after 20000ms'; Stderr: ''; Code: 'null'440 logger.warn(`An error in unlockWithHelperApp: ${e.message}`);441 }442 let startOpts = {443 pkg: UNLOCK_HELPER_PKG_ID,444 activity: UNLOCK_HELPER_PKG_ACTIVITY,445 action: "android.intent.action.MAIN",446 category: "android.intent.category.LAUNCHER",447 flags: "0x10200000",448 stopApp: false,449 retry: false,450 waitDuration: 1000451 };452 // Unlock succeed with a couple of retries.453 let firstRun = true;454 await retry(3, async function () {455 // To reduce a time to call adb.isScreenLocked() since `adb shell dumpsys window` is easy to hang adb commands456 if (firstRun) {457 firstRun = false;458 } else {459 try {460 if (!(await adb.isScreenLocked())) {461 return;462 }463 } catch (e) {464 logger.warn(`Error in isScreenLocked: ${e.message}`);465 logger.warn("\"adb shell dumpsys window\" command has timed out.");466 logger.warn("The reason of this timeout is the delayed adb response. Resetting adb server can improve it.");467 }468 }469 logger.info(`Launching ${UNLOCK_HELPER_PKG_ID}`);470 // The command takes too much time so we should not call the command over twice continuously.471 await adb.startApp(startOpts);472 });473};474helpers.unlock = async function (driver, adb, capabilities) {475 if (!(await adb.isScreenLocked())) {476 logger.info("Screen already unlocked, doing nothing");477 return;478 }479 logger.debug("Screen is locked, trying to unlock");480 if (_.isUndefined(capabilities.unlockType)) {481 logger.warn("Using app unlock, this is going to be deprecated!");482 await helpers.unlockWithHelperApp(adb);483 } else {484 await helpers.unlockWithUIAutomation(driver, adb, {unlockType: capabilities.unlockType, unlockKey: capabilities.unlockKey});485 await helpers.verifyUnlock(adb);486 }487};488helpers.verifyUnlock = async function (adb) {489 await retryInterval(2, 1000, async () => {490 if (await adb.isScreenLocked()) {491 throw new Error("Screen did not unlock successfully, retrying");492 }493 logger.debug("Screen unlocked successfully");494 });495};496helpers.initDevice = async function (adb, opts) {497 await adb.waitForDevice();498 if (!opts.avd) {499 // pushSettingsApp required before calling ensureDeviceLocale for API Level 24+500 // await helpers.pushSettingsApp(adb);501 await helpers.setMockLocationApp(adb, SETTINGS_HELPER_PKG_ID);502 }503 await helpers.ensureDeviceLocale(adb, opts.language, opts.locale);504 await adb.startLogcat();505 let defaultIME;506 if (opts.unicodeKeyboard) {507 defaultIME = await helpers.initUnicodeKeyboard(adb);508 }509 if (_.isUndefined(opts.unlockType)) {510 //await helpers.pushUnlock(adb);511 }512 return defaultIME;513};514helpers.removeNullProperties = function (obj) {515 for (let key of _.keys(obj)) {516 if (_.isNull(obj[key]) || _.isUndefined(obj[key])) {517 delete obj[key];518 }519 }520};521helpers.truncateDecimals = function (number, digits) {522 let multiplier = Math.pow(10, digits),523 adjustedNum = number * multiplier,524 truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);525 return truncatedNum / multiplier;526};527helpers.isChromeBrowser = function (browser) {528 return _.includes(Object.keys(CHROME_BROWSER_PACKAGE_ACTIVITY), (browser || '').toLowerCase());529};530helpers.getChromePkg = function (browser) {531 return CHROME_BROWSER_PACKAGE_ACTIVITY[browser.toLowerCase()] ||532 CHROME_BROWSER_PACKAGE_ACTIVITY.default;533};534helpers.removeAllSessionWebSocketHandlers = async function (server, sessionId) {535 if (!server || !_.isFunction(server.getWebSocketHandlers)) {536 return;537 }538 const activeHandlers = await server.getWebSocketHandlers(sessionId);539 for (const pathname of _.keys(activeHandlers)) {540 await server.removeWebSocketHandler(pathname);541 }542};543/**544 * Takes a desired capability and tries to JSON.parse it as an array,545 * and either returns the parsed array or a singleton array.546 *547 * @param {any} cap A desired capability548 */549helpers.parseArray = function (cap) {550 let parsedCaps;551 try {552 parsedCaps = JSON.parse(cap);553 } catch (ign) { }554 if (_.isArray(parsedCaps)) {555 return parsedCaps;556 } else if (_.isString(cap)) {557 return [cap];558 }559 throw new Error(`must provide a string or JSON Array; received ${cap}`);560};561helpers.bootstrap = Bootstrap;562helpers.unlocker = unlocker;...
driver.js
Source:driver.js
...174 logger.debug('Initializing application under test');175 // set the localized strings for the current language from the apk176 // TODO: incorporate changes from appium#5308 which fix a race cond-177 // ition bug in old appium and need to be replicated here178 this.apkStrings[this.opts.language] = await helpers.pushStrings(179 this.opts.language, this.adb, this.opts);180 if (!this.opts.skipUninstall) {181 await this.adb.uninstallApk(this.opts.appPackage);182 }183 if (!this.opts.noSign) {184 let signed = await this.adb.checkApkCert(this.opts.app, this.opts.appPackage);185 if (!signed) {186 logger.debug('Application not signed. Signing.');187 await this.adb.sign(this.opts.app, this.opts.appPackage);188 }189 }190 await helpers.installApk(this.adb, this.opts);191 // get Selendroid on the device too192 await this.selendroid.installModifiedServer();...
general.js
Source:general.js
1import _ from 'lodash';2import log from '../logger';3import { androidHelpers } from 'appium-android-driver';4import { errors } from 'appium-base-driver';5let extensions = {},6 commands = {},7 helpers = {};8commands.getPageSource = async function () {9 return await this.uiautomator2.jwproxy.command('/source', 'GET', {});10};11// Need to override this for correct unicode support12commands.doSendKeys = async function (params) {13 await this.uiautomator2.jwproxy.command('/keys', 'POST', params);14};15// uiautomator2 doesn't support metastate for keyevents16commands.keyevent = async function (keycode, metastate) {17 log.debug(`Ignoring metastate ${metastate}`);18 await this.adb.keyevent(keycode);19};20// Use ADB since we don't have UiAutomator21commands.back = async function () {22 await this.adb.keyevent(4);23};24commands.getStrings = async function (language) {25 if (!language) {26 language = await this.adb.getDeviceLanguage();27 log.info(`No language specified, returning strings for: ${language}`);28 }29 if (this.apkStrings[language]) {30 // Return cached strings31 return this.apkStrings[language];32 }33 // TODO: This is mutating the current language, but it's how appium currently works34 this.apkStrings[language] = await androidHelpers.pushStrings(language, this.adb, this.opts);35 await this.uiautomator2.jwproxy.command(`/appium/app/strings`, 'POST', {});36 return this.apkStrings[language];37};38// memoized in constructor39commands.getWindowSize = async function () {40 return await this.uiautomator2.jwproxy.command('/window/current/size', 'GET', {});41};42// For W3C43commands.getWindowRect = async function () {44 const {width, height} = await this.getWindowSize();45 return {46 width,47 height,48 x: 0,49 y: 0,50 };51};52extensions.executeMobile = async function (mobileCommand, opts = {}) {53 const mobileCommandsMapping = {54 shell: 'mobileShell',55 scrollBackTo: 'mobileScrollBackTo',56 viewportScreenshot: 'mobileViewportScreenshot',57 deepLink: 'mobileDeepLink',58 startLogsBroadcast: 'mobileStartLogsBroadcast',59 stopLogsBroadcast: 'mobileStopLogsBroadcast',60 acceptAlert: 'mobileAcceptAlert',61 dismissAlert: 'mobileDismissAlert',62 batteryInfo: 'mobileGetBatteryInfo',63 };64 if (!_.has(mobileCommandsMapping, mobileCommand)) {65 throw new errors.UnknownCommandError(`Unknown mobile command "${mobileCommand}". ` +66 `Only ${_.keys(mobileCommandsMapping)} commands are supported.`);67 }68 return await this[mobileCommandsMapping[mobileCommand]](opts);69};70commands.mobileScrollBackTo = async function (opts) {71 const {elementId, elementToId} = opts;72 return await this.uiautomator2.jwproxy.command(`/appium/element/${elementId}/scroll_to/${elementToId}`, 'POST', {});73};74commands.mobileViewportScreenshot = async function () {75 return await this.getViewportScreenshot();76};77commands.setUrl = async function (url) {78 await this.adb.startUri(url, this.opts.appPackage);79};80commands.mobileDeepLink = async function (opts = {}) {81 const {url, package:pkg} = opts;82 return await this.adb.startUri(url, pkg);83};84commands.openNotifications = async function () {85 return await this.uiautomator2.jwproxy.command('/appium/device/open_notifications', 'POST', {});86};87commands.updateSettings = async function (settings) {88 return await this.uiautomator2.jwproxy.command('/appium/settings', 'POST', {settings});89};90commands.getSettings = async function () {91 return await this.uiautomator2.jwproxy.command('/appium/settings', 'GET');92};93/**94 * Overriding appium-android-driver's wrapBootstrapDisconnect,95 * unlike in appium-android-driver avoiding adb restarting as it intern96 * kills UiAutomator2 server running in the device.97 **/98helpers.wrapBootstrapDisconnect = async function (wrapped) {99 await wrapped();100};101// Stop proxying to any Chromedriver and redirect to uiautomator2102helpers.suspendChromedriverProxy = function () {103 this.chromedriver = null;104 this.proxyReqRes = this.uiautomator2.proxyReqRes.bind(this.uiautomator2);105 this.jwpProxyActive = true;106};107Object.assign(extensions, commands, helpers);...
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desired = {4};5var driver = wd.promiseChainRemote("localhost", 4723);6 .init(desired)7 .elementById('com.example.android.contactmanager:id/addContactButton')8 .click()9 .then(function() {10 return driver.elementById('com.example.android.contactmanager:id/contactNameEditText');11 })12 .then(function(el) {13 return driver.pushStrings(el, 'myname');14 })15 .then(function() {16 return driver.elementById('com.example.android.contactmanager:id/contactPhoneEditText');17 })18 .then(function(el) {19 return driver.pushStrings(el, '1234567890');20 })21 .then(function() {22 return driver.elementById('com.example.android.contactmanager:id/contactEmailEditText');23 })24 .then(function(el) {25 return driver.pushStrings(el, '
Using AI Code Generation
1helpers.pushStrings('Hello World');2helpers.pushStrings('Hello World', 'Hello World');3helpers.pushStrings(['Hello World', 'Hello World']);4helpers.pushStrings(['Hello World', 'Hello World'], 'Hello World');5helpers.pushStrings(['Hello World', 'Hello World'], ['Hello World', 'Hello World']);6helpers.pushStrings('Hello World', ['Hello World', 'Hello World']);7helpers.pushStrings('Hello World', 'Hello World', 'Hello World');8helpers.pushStrings(['Hello World', 'Hello World'], 'Hello World', 'Hello World');9helpers.pushStrings(['Hello World', 'Hello World'], ['Hello World', 'Hello World'], 'Hello World');10helpers.pushStrings(['Hello World', 'Hello World'], ['Hello World', 'Hello World'], ['Hello World', 'Hello World']);11helpers.pushStrings('Hello World', ['Hello World', 'Hello World'], 'Hello World');12helpers.pushStrings('Hello World', ['Hello World', 'Hello World'], ['Hello World', 'Hello World']);13helpers.pushStrings('Hello World', 'Hello World', ['Hello World', 'Hello World']);14helpers.pushStrings('Hello World', 'Hello World', ['Hello World',
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var caps = require('./caps.js');4var helpers = require('./helpers.js');5var driver = wd.promiseChainRemote('localhost', 4723);6driver.init(caps.android19).then(function () {7 .elementByClassName('android.widget.EditText')8 .sendKeys('Hello World');9}).then(function () {10 .elementByClassName('android.widget.EditText')11 .text().should.become('Hello World');12}).fin(function () { return driver.quit(); }).done();13exports.android19 = {14};15var wd = require('wd');16exports.pushStrings = function (driver, strings) {17 .execute('mobile: pushStrings', {strings: strings});18};19{20 {21 }22}23var wd = require('wd');24var assert = require('assert');25var caps = require('./caps.js');26var helpers = require('./helpers.js');27var driver = wd.promiseChainRemote('localhost', 4723);28driver.init(caps.android19).then(function () {29 return helpers.pushStrings(driver, require('./strings.json'));30}).then(function () {31 .elementByClassName('android.widget.EditText')32 .sendKeys('@string/greeting');33}).then(function () {34 .elementByClassName('android.widget.EditText')35 .text().should.become('Hello World');36}).fin(function () { return driver.quit(); }).done();37exports.android19 = {
Using AI Code Generation
1var wd = require("wd")2var assert = require('assert');3var fs = require('fs');4var path = require('path');5var helpers = require('appium-android-driver').androidHelpers;6var desiredCaps = {
Using AI Code Generation
1var driver = require('appium-android-driver');2var helpers = require('appium-android-driver').androidHelpers;3var d = new driver.AndroidDriver();4helpers.pushStrings(d, 'en', 'strings.xml');5helpers.pushStrings(d, 'fr', 'strings.xml');6helpers.pushStrings(d, 'de', 'strings.xml');7var driver = require('appium-android-driver');8var helpers = require('appium-android-driver').androidHelpers;9var d = new driver.AndroidDriver();10helpers.pushStrings(d, 'en', 'strings.xml');11helpers.pushStrings(d, 'fr', 'strings.xml');12helpers.pushStrings(d, 'de', 'strings.xml');13var driver = require('appium-android-driver');14var helpers = require('appium-android-driver').androidHelpers;15var d = new driver.AndroidDriver();16helpers.pushStrings(d, 'en', 'strings.xml');17helpers.pushStrings(d, 'fr', 'strings.xml');18helpers.pushStrings(d, 'de', 'strings.xml');19var driver = require('appium-android-driver');20var helpers = require('appium-android-driver').androidHelpers;21var d = new driver.AndroidDriver();22helpers.pushStrings(d, 'en', 'strings.xml');23helpers.pushStrings(d, 'fr', 'strings.xml');24helpers.pushStrings(d, 'de', 'strings.xml');
Using AI Code Generation
1var helpers = require('appium-android-driver').helpers;2helpers.pushStrings('en', 'strings.xml', 'com.example.app', 'appiumtest');3var helpers = require('appium-android-driver').helpers;4helpers.pushStrings('en', 'strings.xml', 'com.example.app', 'appiumtest');5var helpers = require('appium-android-driver').helpers;6helpers.pushStrings('en', 'strings.xml', 'com.example.app', 'appiumtest');7var helpers = require('appium-android-driver').helpers;8helpers.pushStrings('en', 'strings.xml', 'com.example.app', 'appiumtest');9var helpers = require('appium-android-driver').helpers;10helpers.pushStrings('en', 'strings.xml', 'com.example.app', 'appiumtest');11var helpers = require('appium-android-driver').helpers;12helpers.pushStrings('en', 'strings.xml', 'com.example.app', 'appiumtest');13var helpers = require('appium-android-driver').helpers;14helpers.pushStrings('en', 'strings.xml', 'com.example.app', 'appiumtest');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!