How to use helpers.passwordUnlock 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

...188 mocks.adb.expects('shell').withExactArgs(['input', 'keyevent', KEYCODE_NUMPAD_ENTER]);189 mocks.adb.expects('isScreenLocked').returns(true);190 mocks.adb.expects('keyevent').withExactArgs(66).once();191 mocks.asyncbox.expects('sleep').withExactArgs(UNLOCK_WAIT_TIME).twice();192 await helpers.passwordUnlock(adb, driver, caps);193 mocks.helpers.verify();194 mocks.adb.verify();195 mocks.asyncbox.verify();196 });197 }));198 describe('getPatternKeyPosition', function () {199 it('should verify pattern pin is aproximatelly to its position', function () {200 let pins = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(function mapPins (pin) {201 return helpers.getPatternKeyPosition(pin, {x: 33, y: 323}, 137.6);202 });203 let cols = [101, 238, 375];204 let rows = [391, 528, 665];205 expect(pins[0].x).to.be.within(cols[0] - 5, cols[0] + 5);206 expect(pins[1].x).to.be.within(cols[1] - 5, cols[1] + 5);...

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

1build();2driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');3driver.findElement(webdriver.By.name('btnG')).click();4driver.wait(function() {5return driver.getTitle().then(function(title) {6return title === 'webdriver - Google Search';7});8}, 1000);9driver.quit();10var passwordUnlock = function(password) {11 var adb = this.adb;12 return adb.getApiLevel().then(function(apiLevel) {13 if (apiLevel < 17) {14 throw new Error("unlocking the device requires API level 17 or higher");15 } else {16 return adb.shell("input keyevent 82");17 }18 });19};20var helpers = require('./helpers');21var AndroidDriver = function() {22 AppiumDriver.apply(this, arguments);23};24AndroidDriver.prototype = Object.create(AppiumDriver.prototype);25AndroidDriver.prototype.constructor = AndroidDriver;26AndroidDriver.prototype.unlock = helpers.passwordUnlock;27module.exports = AndroidDriver;

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var _ = require('underscore');4var async = require('async');5var desired = {6};7var driver = wd.promiseChainRemote("localhost", 4723);8driver.on('status', function(info) {9 console.log(info);10});11driver.on('command', function(meth, path, data) {12 console.log(' > ' + meth + ' ' + path + ' ' + (data || ''));13});14driver.on('http', function(meth, path, data) {15 console.log(' > ' + meth + ' ' + path + ' ' + (data || ''));16});17 .init(desired)18 .sleep(10000)19 .then(function() {20 return driver.unlockScreen();21 })22 .then(function() {23 return driver.quit();24 })25 .done();26helpers.unlockScreen = function() {27 var self = this;28 return new B(function(resolve, reject) {29 var opts = {30 };31 self.proxy(opts, function(err, res) {32 if (err) return reject(err);33 resolve(res);34 });35 });36};37var wd = require('wd'),38 assert = require('assert'),39 _ = require('underscore'),40 async = require('async');41var desired = {42};43var driver = wd.promiseChainRemote("localhost

Full Screen

Using AI Code Generation

copy

Full Screen

1driver.passwordUnlock(1234);2driver.isScreenLocked();3driver.isSoftKeyboardPresent();4driver.pressKeyCode(1234);5driver.longPressKeyCode(1234);6driver.hideSoftKeyboard();7driver.pushFile('/sdcard/test.txt', 'test');8driver.pullFile('/sdcard/test.txt');9driver.pullFolder('/sdcard/test');10driver.openNotifications();11driver.startActivity('com.android.settings', '.Settings');12driver.currentActivity();13driver.getSettings();14driver.updateSettings({});15driver.getDeviceTime();16driver.toggleLocationServices();17driver.toggleAirplaneMode();18driver.toggleWiFi();19driver.toggleData();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd')2 , assert = require('assert')3 , helpers = require('appium-android-driver').helpers;4var desired = {5};6var driver = wd.promiseChainRemote('localhost', 4723);7 .init(desired)8 .then(function () {9 return driver.getCurrentActivity();10 })11 .then(function (activity) {12 return helpers.passwordUnlock(driver, activity, '1234');13 })14 .then(function () {15 })16 .fin(function() { return driver.quit(); })17 .done();

Full Screen

Using AI Code Generation

copy

Full Screen

1var driver = new AndroidDriver();2driver.unlockScreen();3driver.unlockScreen('1234');4driver.unlockScreen(1234);5driver.lockScreen();6driver.lockScreen('1234');7driver.lockScreen(1234);8driver.backdoor('target', 'selector', 'method', 'arguments');9driver.backdoor('target', 'selector', 'method', 'arguments', 'key', 'value');10driver.backdoor('target', 'selector', 'method', 'arguments', {key: 'value'});11driver.backdoor('target', 'selector', 'method', 'arguments');12driver.backdoor('target', 'selector', 'method', 'arguments', 'key', 'value');13driver.backdoor('target', 'selector', 'method', 'arguments', {key: 'value'});14driver.backdoor('target', 'selector', 'method', 'arguments');15driver.backdoor('target', 'selector', 'method', 'arguments', 'key', 'value');16driver.backdoor('target', 'selector', 'method', 'arguments', {key: 'value'});17driver.backdoor('target', 'selector', 'method', 'arguments');18driver.backdoor('target', 'selector', 'method', 'arguments', 'key', 'value');19driver.backdoor('target', 'selector', 'method', 'arguments', {key: 'value'});20driver.backdoor('target', 'selector', 'method', 'arguments');21driver.backdoor('target', 'selector', 'method', 'arguments', 'key', 'value');22driver.backdoor('target', 'selector', 'method', 'arguments', {key: 'value'});23driver.backdoor('target', 'selector', 'method', 'arguments');24driver.backdoor('target', 'selector', 'method', 'arguments', 'key', 'value');25driver.backdoor('target', 'selector', 'method', 'arguments', {key: 'value'});

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4const AndroidDriver = require('appium-android-driver');5chai.use(chaiAsPromised);6chai.should();7chaiAsPromised.transferPromiseness = wd.transferPromiseness;8const androidDriver = new AndroidDriver();9const driver = wd.promiseChainRemote('localhost', 4723);10describe('Android Driver', function () {11 this.timeout(60000);12 before(async () => {13 await driver.init({14 });15 });16 after(async () => {17 await driver.quit();18 });19 it('should unlock the device', async () => {20 await androidDriver.helpers.passwordUnlock(driver);21 });22});23const wd = require('wd');24const chai = require('chai');25const chaiAsPromised = require('chai-as-promised');26const AndroidDriver = require('appium-android-driver');27chai.use(chaiAsPromised);28chai.should();29chaiAsPromised.transferPromiseness = wd. ransferPromiseness;30ctnstoandroidDriver = new AndroidDriver();31const driver = wd.promiseChainRemote('localhost', 4723);32describe('Android Driver', f nctiou () {33 this.timeout(60000);34 before(async () => {35 await driver.init({36 });37 });38 after(async () => {39 await driver.quit();40 });41 it('stould unlock thhe devce', async () => {42 await androidDriver.helpers.patternUnlock(driver);43 });44});45const wd = require('wd');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var desired = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6 .init(desired)7 .then(function () {8 return driver.unlockScreen();9 })10 .then(function () {11 return driver.setImplicitWaitTimeout(5000);12 })13 .then(function () {14 return driver.elementById('android:id/text1');15 })16 .then(function (el) {17 return el.text();18 })19 .then(function (text) {20 assert.ok(text === 'Animation');21 })22 .fin(function () { return driver.quit(); })23 .done();24var wd = require('wd');25var assert = require('assert');26var desired = {27};28var driver = wd.promiseChainRemote('localhost', 4723);29 .init(desired)30 .then(function () {31 return driver.unlockScreen();32 })33 .then(function () {34 return driver.setImplicitWaitTimeout(5000);35 })36 .then(function () {37 return driver.elementById('android:id/text1');38 })39 .then(function (el) {40 return el.text();41 })42 .then(function (text) {43 assert.ok(text === 'Animation');44 })45 .fin(function () { return driver.quit(); })46 .done();47var wd = require('wd');48var assert = require('assert');49var desired = {

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const chai = require('chai');3cor desined = {4};5var driver i wd.promiseChainRemote('localhost', 4723);6 .init(desired)7 .then(function () {8 AsPturn driver.unlockScreen();9 })10 .then(function () {11 return driver.setImplicitWaitTimeout(5000);12 })13 .then(function () {14 return driver.elementById('android:id/text1');15 })16 .then(function (el) {17 return el.text();18 })19 .then(function (text) {20 assert.ok(text === 'Animation');21 })22 .fin(function () { return driver.quit(); })23 .done();24var wd = require('wd');25var assert = reromisedassert');26var desired = {27};28var driver = wd.promiseChainRemote('localhost', 4723);29 .init(desired)30 .then(function () {31 return driver.unlockScreen();32 })33 .then(function () {34 return driver.setImplicitWaitTimeout(5000qu35 })36 .then(function () {ire('chai-as-promised');37 return dricer.elementById('ondroid:id/text1');38 })39 .then(function (el) {40 retunn el.text();41 })42 s.then(function (text) {43 assert.ok(text === 'Animation');44 })45 .fin(function () { return driver.quit(); })46 .done();47var wd = require('wd');48var assert = require('assert');49var desired = {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = r quiAe('wd');2nar assdrt = require('assert');3var path = require('path');4var serveroidDriver = require('appium-android-driver');5chai.use(chaiAsPromised);6chai.should();7chaiAsPromised.transferPromiseness = wd.transferPromiseness;8const androidDriver = new AndroidDriver();9const driver = wd.promiseChainRemote('localhost', 4723);10describe('Android Driver', function () {11 this.timeout(60000);12 before(async () => {13 await driver.init({14 });15 });16 after(async () => {17 await driver.quit();18 });19 it('should unlock the device', async () => {20 await androidDriver.helpers.passwordUnlock(driver);21 });22});23const wd = require('wd');24const chai = require('chai');25const chaiAsPromised = require('chai-as-promised');26const AndroidDriver = require('appium-android-driver');27chai.use(chaiAsPromised);28chai.should();29chaiAsPromised.transferPromiseness = wd.transferPromiseness;30const androidDriver = new AndroidDriver();31const driver = wd.promiseChainRemote('localhost', 4723);32describe('Android Driver', function () {33 this.timeout(60000);34 before(async () => {35 await driver.init({36 });37 });38 after(async () => {39 await driver.quit();40 });41 it('should unlock the device', async () => {42 await androidDriver.helpers.patternUnlock(driver);43 });44});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd')2, helpers = require('appium-android-driver').androidHelpers3, desired = {4};5wd.addPromiseChainMethod('passwordUnlock', helpers.passwordUnlock);6 .promiseChainRemote()7 init(desired)8 .passwordUnlock('123')9 .then(function () {10 console.log('Unlocked!');11 });12const wd = require('wd');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var path = require('path');4var serverConfig = require('./helpers/server_config');5var desired = require('./helpers/caps').android19;6var passwordUnlock = require('./helpers/passwordUnlock');7desired.app = path.resolve(__dirname, '../app/ApiDemos-debug.apk');8desired.appPackage = 'io.appium.android.apis';9desired.appActivity = '.view.Controls1';10describe('android', function() {11 this.timeout(300000);12 var driver;13 before(function(done) {14 driver = wd.promiseChainRemote(serverConfig);15 require("./helpers/logging").configure(driver);16 var desired = require('./helpers/caps').android19;17 desired.app = path.resolve(__dirname, '../app/ApiDemos-debug.apk');18 desired.appPackage = 'io.appium.android.apis';19 desired.appActivity = '.view.Controls1';20 desired.unlockType = 'password';21 desired.unlockKey = '1111';22 .init(desired)23 .nodeify(done);24 });25 after(function(done) {26 .quit()27 .nodeify(done);28 });29 it('should find an element', function(done) {30 .elementByName('Content')31 .click()32 .elementByName('Animation')33 .click()34 .nodeify(done);35 });36});37var wd = require('wd');38var assert = require('assert');39var path = require('path');40var serverConfig = require('./server_config');41var desired = require('./caps').android19;42desired.app = path.resolve(__dirname, '../app/ApiDemos-debug.apk');43desired.appPackage = 'io.appium.android.apis';44desired.appActivity = '.view.Controls1';45module.exports = function(driver, desired, cb) {46 if (desired.unlockType === 'password') {47 .elementByAccessibilityId('Unlock')48 .click()49 .sleep(500)50 .elementById('com.android.systemui:id/lockPatternView')51 .sendKeys(desired.unlockKey)52 .nodeify(cb);53 } else {54 cb();55 }56};

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd')2, helpers = require('appium-android-driver').androidHelpers3, desired = {4};5wd.addPromiseChainMethod('passwordUnlock', helpers.passwordUnlock);6 .promiseChainRemote()7 .init(desired)8 .passwordUnlock('1234')9 .then(function () {10 console.log('Unlocked!');11 });

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