How to use helpers.patternUnlock method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

unlock-helper-specs.js

Source:unlock-helper-specs.js Github

copy

Full Screen

...285 mocks.adb.expects('getApiLevel').returns(21);286 mocks.driver.expects('findElOrEls')287 .withExactArgs('id', 'com.android.systemui:id/lockPatternView', false)288 .returns(el);289 await helpers.patternUnlock(adb, driver, caps);290 mocks.helpers.verify();291 mocks.driver.verify();292 mocks.asyncbox.verify();293 mocks.adb.verify();294 });295 it('should be able to unlock device using pattern (API level < 21)', async function () {296 mocks.adb.expects('getApiLevel').returns(20);297 mocks.driver.expects('findElOrEls')298 .withExactArgs('id', 'com.android.keyguard:id/lockPatternView', false)299 .returns(el);300 await helpers.patternUnlock(adb, driver, caps);301 mocks.helpers.verify();302 mocks.driver.verify();303 mocks.asyncbox.verify();304 mocks.adb.verify();305 });306 }));...

Full Screen

Full Screen

unlock-helpers.js

Source:unlock-helpers.js Github

copy

Full Screen

1import logger from './logger';2import { sleep } from 'asyncbox';3import _ from 'lodash';4import { util } from 'appium-support';5const PIN_UNLOCK = 'pin';6const PASSWORD_UNLOCK = 'password';7const PATTERN_UNLOCK = 'pattern';8const FINGERPRINT_UNLOCK = 'fingerprint';9const UNLOCK_TYPES = [PIN_UNLOCK, PASSWORD_UNLOCK, PATTERN_UNLOCK, FINGERPRINT_UNLOCK];10const KEYCODE_NUMPAD_ENTER = 66;11const KEYCODE_POWER = 26;12const KEYCODE_WAKEUP = 224; // Can work over API Level 2013const UNLOCK_WAIT_TIME = 100;14const HIDE_KEYBOARD_WAIT_TIME = 100;15const INPUT_KEYS_WAIT_TIME = 100;16let helpers = {};17helpers.isValidUnlockType = function isValidUnlockType (type) {18 return UNLOCK_TYPES.indexOf(type) !== -1;19};20helpers.isValidKey = function isValidKey (type, key) {21 if (_.isUndefined(key)) {22 return false;23 }24 if (type === PIN_UNLOCK || type === FINGERPRINT_UNLOCK) {25 return /^[0-9]+$/.test(key.trim());26 }27 if (type === PATTERN_UNLOCK) {28 if (!/^[1-9]{2,9}$/.test(key.trim())) {29 return false;30 }31 return !(/([1-9]).*?\1/.test(key.trim()));32 }33 // Dont trim password key, you can use blank spaces in your android password34 // ¯\_(ツ)_/¯35 if (type === PASSWORD_UNLOCK) {36 return /.{4,}/g.test(key);37 }38 throw new Error(`Invalid unlock type ${type}`);39};40helpers.dismissKeyguard = async function dismissKeyguard (driver, adb) {41 logger.info('Waking up the device to unlock it');42 // Screen off once to force pre-inputted text field clean after wake-up43 // Just screen on if the screen defaults off44 await driver.pressKeyCode(KEYCODE_POWER);45 await driver.pressKeyCode(KEYCODE_WAKEUP);46 let isKeyboardShown = await driver.isKeyboardShown();47 if (isKeyboardShown) {48 await driver.hideKeyboard();49 // Waits a bit for the keyboard to hide50 await sleep(HIDE_KEYBOARD_WAIT_TIME);51 }52 // dismiss notifications53 logger.info('Dismiss notifications from unlock view');54 await adb.shell(['service', 'call', 'notification', '1']);55 await adb.back();56 if (await adb.getApiLevel() > 21) {57 logger.info('Trying to dismiss keyguard');58 await adb.shell(['wm', 'dismiss-keyguard']);59 return;60 }61 logger.info('Swiping up to dismiss keyguard');62 await helpers.swipeUp(driver);63};64helpers.swipeUp = async function swipeUp (driver) {65 let windowSize = await driver.getWindowSize();66 let x0 = parseInt(windowSize.x / 2, 10);67 let y0 = windowSize.y - 10;68 let yP = 100;69 let actions = [70 {action: 'press', options: {element: null, x: x0, y: y0}},71 {action: 'moveTo', options: {element: null, x: x0, y: yP}},72 {action: 'release'}73 ];74 await driver.performTouch(actions);75};76helpers.encodePassword = function encodePassword (key) {77 return key.replace(/\s/ig, '%s');78};79helpers.stringKeyToArr = function stringKeyToArr (key) {80 return key.trim().replace(/\s+/g, '').split(/\s*/);81};82helpers.fingerprintUnlock = async function fingerprintUnlock (adb, driver, capabilities) {83 if (await adb.getApiLevel() < 23) {84 throw new Error('Fingerprint unlock only works for Android 6+ emulators');85 }86 await adb.fingerprint(capabilities.unlockKey);87 await sleep(UNLOCK_WAIT_TIME);88};89helpers.pinUnlock = async function pinUnlock (adb, driver, capabilities) {90 logger.info(`Trying to unlock device using pin ${capabilities.unlockKey}`);91 await helpers.dismissKeyguard(driver, adb);92 let keys = helpers.stringKeyToArr(capabilities.unlockKey);93 if (await adb.getApiLevel() >= 21) {94 let els = await driver.findElOrEls('id', 'com.android.systemui:id/digit_text', true);95 if (_.isEmpty(els)) {96 throw new Error('Error finding unlock pin buttons!');97 }98 let pins = {};99 for (let el of els) {100 let text = await driver.getAttribute('text', util.unwrapElement(el));101 pins[text] = el;102 }103 for (let pin of keys) {104 let el = pins[pin];105 await driver.click(util.unwrapElement(el));106 }107 } else {108 for (let pin of keys) {109 let el = await driver.findElOrEls('id', `com.android.keyguard:id/key${pin}`, false);110 if (el === null) {111 throw new Error(`Error finding unlock pin '${pin}' button!`);112 }113 await driver.click(util.unwrapElement(el));114 }115 }116 // Some devices accept entering the code without pressing the Enter key117 // When I rushed commands without this wait before pressKeyCode, rarely UI2 sever crashed118 await sleep(UNLOCK_WAIT_TIME);119 if (await adb.isScreenLocked()) {120 await driver.pressKeyCode(KEYCODE_NUMPAD_ENTER);121 await sleep(UNLOCK_WAIT_TIME);122 }123};124helpers.passwordUnlock = async function passwordUnlock (adb, driver, capabilities) {125 logger.info(`Trying to unlock device using password ${capabilities.unlockKey}`);126 await helpers.dismissKeyguard(driver, adb);127 let key = capabilities.unlockKey;128 // Replace blank spaces with %s129 key = helpers.encodePassword(key);130 // Why adb ? It was less flaky131 await adb.shell(['input', 'text', key]);132 // Why sleeps ? Avoid some flakyness waiting for the input to receive the keys133 await sleep(INPUT_KEYS_WAIT_TIME);134 await adb.shell(['input', 'keyevent', KEYCODE_NUMPAD_ENTER]);135 // Waits a bit for the device to be unlocked136 await sleep(UNLOCK_WAIT_TIME);137};138helpers.getPatternKeyPosition = function getPatternKeyPosition (key, initPos, piece) {139 /*140 How the math works:141 We have 9 buttons divided in 3 columns and 3 rows inside the lockPatternView,142 every button has a position on the screen corresponding to the lockPatternView since143 it is the parent view right at the middle of each column or row.144 */145 const cols = 3;146 const pins = 9;147 let xPos = (key, x, piece) => {148 return Math.round(x + ((key % cols) || cols) * piece - piece / 2);149 };150 let yPos = (key, y, piece) => {151 return Math.round(y + (Math.ceil(((key % pins) || pins) / cols) * piece - piece / 2));152 };153 return {x: xPos(key, initPos.x, piece), y: yPos(key, initPos.y, piece)};154};155helpers.getPatternActions = function getPatternActions (keys, initPos, piece) {156 let actions = [];157 let lastPos;158 for (let key of keys) {159 let keyPos = helpers.getPatternKeyPosition(key, initPos, piece);160 if (key === keys[0]) {161 actions.push({action: 'press', options: {element: null, x: keyPos.x, y: keyPos.y}});162 lastPos = keyPos;163 continue;164 }165 let moveTo = {x: 0, y: 0};166 let diffX = keyPos.x - lastPos.x;167 if (diffX > 0) {168 moveTo.x = piece;169 if (Math.abs(diffX) > piece) {170 moveTo.x += piece;171 }172 } else if (diffX < 0) {173 moveTo.x = -1 * piece;174 if (Math.abs(diffX) > piece) {175 moveTo.x -= piece;176 }177 }178 let diffY = keyPos.y - lastPos.y;179 if (diffY > 0) {180 moveTo.y = piece;181 if (Math.abs(diffY) > piece) {182 moveTo.y += piece;183 }184 } else if (diffY < 0) {185 moveTo.y = -1 * piece;186 if (Math.abs(diffY) > piece) {187 moveTo.y -= piece;188 }189 }190 actions.push({action: 'moveTo', options: {element: null, x: moveTo.x + lastPos.x, y: moveTo.y + lastPos.y}});191 lastPos = keyPos;192 }193 actions.push({action: 'release'});194 return actions;195};196helpers.patternUnlock = async function patternUnlock (adb, driver, capabilities) {197 logger.info(`Trying to unlock device using pattern ${capabilities.unlockKey}`);198 await helpers.dismissKeyguard(driver, adb);199 let keys = helpers.stringKeyToArr(capabilities.unlockKey);200 /* We set the device pattern buttons as number of a regular phone201 * | • • • | | 1 2 3 |202 * | • • • | --> | 4 5 6 |203 * | • • • | | 7 8 9 |204 The pattern view buttons are not seeing by the uiautomator since they are205 included inside a FrameLayout, so we are going to try clicking on the buttons206 using the parent view bounds and math.207 */208 let apiLevel = await adb.getApiLevel();209 let el = await driver.findElOrEls('id',210 `com.android.${apiLevel >= 21 ? 'systemui' : 'keyguard'}:id/lockPatternView`,211 false212 );213 let initPos = await driver.getLocation(util.unwrapElement(el));214 let size = await driver.getSize(util.unwrapElement(el));215 // Get actions to perform216 let actions = helpers.getPatternActions(keys, initPos, size.width / 3);217 // Perform gesture218 await driver.performTouch(actions);219 // Waits a bit for the device to be unlocked220 await sleep(UNLOCK_WAIT_TIME);221};222helpers.PIN_UNLOCK = PIN_UNLOCK;223helpers.PASSWORD_UNLOCK = PASSWORD_UNLOCK;224helpers.PATTERN_UNLOCK = PATTERN_UNLOCK;225helpers.FINGERPRINT_UNLOCK = FINGERPRINT_UNLOCK;226export { PIN_UNLOCK, PASSWORD_UNLOCK, PATTERN_UNLOCK, FINGERPRINT_UNLOCK, helpers };...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1driver.init({2}).then(function() {3}).then(function(el) {4 return el.click();5}).then(function() {6}).then(function(el) {7 return el.click();8}).then(function() {9}).then(function(el) {10 return el.click();11}).then(function() {12}).then(function(el) {13 return el.click();14}).then(function() {15}).then(function(el) {16 return el.click();17}).then(function() {18}).then(function(el) {19 return el.click();20}).then(function() {21}).then(function(el) {22 return el.click();23}).then(function() {24}).then(function(el) {25 return el.click();26}).then(function() {27}).then(function(el) {28 return el.click();29}).then(function() {30}).then(function(el) {31 return el.click();32}).then(function() {33}).then(function(el) {34 return el.click();35}).then(function() {36}).then(function(el) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var driver = new AndroidDriver();2var pattern = [1, 2, 3, 4, 5, 6, 7, 8, 9];3driver.helpers.patternUnlock(pattern);4AndroidDriver.prototype.helpers = {5 patternUnlock: function(pattern) {6 }7}

Full Screen

Using AI Code Generation

copy

Full Screen

1var driver = new wd.Builder().withCapabilities(caps).build();2helpers.patternUnlock(driver, 1, 2, 3, 4, 5, 6, 7, 8, 9);3var driver = new wd.Builder().withCapabilities(caps).build();4helpers.unlockScreen(driver);5var driver = new wd.Builder().withCapabilities(caps).build();6helpers.installApp(driver, 'path/to/app.apk');7var driver = new wd.Builder().withCapabilities(caps).build();8helpers.removeApp(driver, 'package-name');9var driver = new wd.Builder().withCapabilities(caps).build();10helpers.isAppInstalled(driver, 'package-name');11var driver = new wd.Builder().withCapabilities(caps).build();12helpers.backgroundApp(driver, 5);13var driver = new wd.Builder().withCapabilities(caps).build();14helpers.startActivity(driver, 'package-name', 'activity-name');15var driver = new wd.Builder().withCapabilities(caps).build();16helpers.toggleAirplaneMode(driver);17var driver = new wd.Builder().withCapabilities(caps).build();18helpers.toggleData(driver);19var driver = new wd.Builder().withCapabilities(caps).build();20helpers.toggleWiFi(driver);21var driver = new wd.Builder().withCapabilities(caps).build();22helpers.toggleLocationServices(driver);23var driver = new wd.Builder().withCapabilities(caps).build();24helpers.getDeviceTime(driver);25var driver = new wd.Builder().withCapabilities(caps).build();26helpers.getDeviceTime(driver);

Full Screen

Using AI Code Generation

copy

Full Screen

1var driver = wd.promiseChainRemote('localhost', 4723);2driver.init({3}).then(function () {4 return driver.sleep(5000);5}).then(function () {6 return driver.execute("mobile: patternUnlock", [{pattern: [0, 1, 2, 5, 8, 7, 6, 3, 4]}]);7});8The patternUnlock method will fail if the pattern is invalid. For example, the following array represents a pattern that starts at point 0, goes to point 1, then point 2, then point 5, then point 8, then point 7, then point 6, then point 3, then point 4, then point 0, and then point 1 (which is not a valid pattern):9The patternUnlock method will fail if the pattern is not long enough. For example, the following array represents a pattern that starts at point 0, goes to point 1, then point 2 (which is not a valid pattern because it is too short):

Full Screen

Using AI Code Generation

copy

Full Screen

1const Appium = require('appium');2const AndroidDriver = Appium.AndroidDriver;3const androidDriver = new AndroidDriver();4const helpers = androidDriver.helpers;5const pattern = [1, 2, 3, 4, 5, 6, 7, 8, 9];6helpers.patternUnlock(pattern).then(function () {7 console.log("Pattern unlocked successfully");8}).catch(function (err) {9 console.log("Error unlocking pattern: " + err);10});

Full Screen

Using AI Code Generation

copy

Full Screen

1var helpers = require('appium-android-driver').androidHelpers;2helpers.patternUnlock(driver, '1234');3var helpers = require('appium-android-driver').androidHelpers;4helpers.unlockScreen(driver);5var helpers = require('appium-android-driver').androidHelpers;6helpers.getApiLevel(driver);7var helpers = require('appium-android-driver').androidHelpers;8helpers.getDevicePixelRatio(driver);9var helpers = require('appium-android-driver').androidHelpers;10helpers.getDisplayDensity(driver);11var helpers = require('appium-android-driver').androidHelpers;12helpers.getDisplaySize(driver);13var helpers = require('appium-android-driver').androidHelpers;14helpers.getDisplayRotation(driver);15var helpers = require('appium-android-driver').androidHelpers;16helpers.getDisplayRotation(driver);17var helpers = require('appium-android-driver').androidHelpers;18helpers.getDeviceTime(driver);19var helpers = require('appium-android-driver').androidHelpers;20helpers.getDeviceInfo(driver);21var helpers = require('appium-android-driver').androidHelpers;22helpers.getDisplayDensity(driver);23var helpers = require('appium-android-driver').android

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Android Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful