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

...78 let caps = {unlockKey: '123'};79 mocks.adb.expects('getApiLevel').returns(23);80 mocks.adb.expects('fingerprint').withExactArgs(caps.unlockKey).once();81 mocks.asyncbox.expects('sleep').withExactArgs(UNLOCK_WAIT_TIME).once();82 await helpers.fingerprintUnlock(adb, driver, caps).should.be.fulfilled;83 mocks.adb.verify();84 mocks.asyncbox.verify();85 });86 it('should throw error if API level < 23', async function () {87 mocks.adb.expects('getApiLevel').returns(22);88 mocks.adb.expects('fingerprint').never();89 mocks.asyncbox.expects('sleep').never();90 await helpers.fingerprintUnlock(adb)91 .should.eventually.be.rejectedWith('only works for Android 6+');92 mocks.adb.verify();93 mocks.asyncbox.verify();94 });95 }));96 describe('pinUnlock', withMocks({adb, helpers, driver, asyncbox}, (mocks) => {97 const caps = {unlockKey: '13579'};98 const keys = ['1', '3', '5', '7', '9'];99 const els = [100 {ELEMENT: 1}, {ELEMENT: 2}, {ELEMENT: 3},101 {ELEMENT: 4}, {ELEMENT: 5}, {ELEMENT: 6},102 {ELEMENT: 7}, {ELEMENT: 8}, {ELEMENT: 9},103 ];104 afterEach(function () {...

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

1var webdriver = require('selenium-webdriver');2var By = webdriver.By;3var until = webdriver.until;4var driver = new webdriver.Builder()5 .forBrowser('chrome')6 .build();7driver.findElement(By.name('q')).sendKeys('webdriver');8driver.findElement(By.name('btnK')).click();9driver.wait(until.titleIs('webdriver - Google Search'), 1000);10driver.quit();11var webdriver = require('selenium-webdriver');12var By = webdriver.By;13var until = webdriver.until;14var driver = new webdriver.Builder()15 .forBrowser('chrome')16 .build();17driver.findElement(By.name('q')).sendKeys('webdriver');18driver.findElement(By.name('btnK')).click();19driver.wait(until.titleIs('webdriver - Google Search'), 1000);20driver.quit();21var webdriver = require('selenium-webdriver');22var By = webdriver.By;23var until = webdriver.until;24var driver = new webdriver.Builder()25 .forBrowser('chrome')26 .build();27driver.findElement(By.name('q')).sendKeys('webdriver');28driver.findElement(By.name('btnK')).click();29driver.wait(until.titleIs('webdriver - Google Search'), 1000);30driver.quit();31var webdriver = require('selenium-webdriver');32var By = webdriver.By;33var until = webdriver.until;34var driver = new webdriver.Builder()35 .forBrowser('chrome')36 .build();37driver.findElement(By.name('q')).sendKeys('webdriver');38driver.findElement(By.name('btnK')).click();39driver.wait(until.titleIs('webdriver - Google Search'), 1000);40driver.quit();41var webdriver = require('selenium-webdriver');42var By = webdriver.By;43var until = webdriver.until;44var driver = new webdriver.Builder()45 .forBrowser('chrome')46 .build();

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2driver.init({3}).then(() => {4 return driver.fingerprintUnlock();5})6driver.quit();7[UiAutomator2] at io.appium.uiautomator2.handler.FingerprintUnlock.fingerprintUnlock(FingerprintUnlock.java:83)8[UiAutomator2] at io.appium.uiautomator2.handler.FingerprintUnlock.handle(FingerprintUnlock.java:50)9[UiAutomator2] at io.appium.uiautomator2.handler.FingerprintUnlock.handle(FingerprintUnlock.java:38)10[UiAutomator2] at io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:250)11[UiAutomator2] at io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:241)12[UiAutomator2] at io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:44)13[UiAutomator2] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)14[UiAutomator2] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)15[UiAutomator2] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)16[UiAutomator2] at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)17[UiAutomator2] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)18[UiAutomator2] at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)19[UiAutomator2] at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)20[UiAutomator2] at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2driver.init({3}).then(function() {4 return driver.fingerprintUnlock();5}).then(function() {6 return driver.elementById("some_id").click();7}).fin(function() {8 return driver.quit();9}).done();10var wd = require('wd');11driver.init({12}).then(function() {13 return driver.touchId();14}).then(function() {15 return driver.elementById("some_id").click();16}).fin(function() {17 return driver.quit();18}).done();19var wd = require('wd');20driver.init({21}).then(function() {22 return driver.touchId();23}).then(function() {24 return driver.elementById("some_id").click();25}).fin(function() {26 return driver.quit();27}).done();

Full Screen

Using AI Code Generation

copy

Full Screen

1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3 .forBrowser('chrome')4 .build();5driver.quit();6driver.driver.fingerprintUnlock();

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Appium Android Driver', function () {2 it('should unlock the device', function () {3 return driver.fingerprintUnlock();4 });5});6exports.fingerprintUnlock = function () {7 return this.requestHandler.create({8 });9};10commands.fingerprintUnlock = async function () {11 return await this.helpers.fingerprintUnlock();12};13commands.fingerprintUnlock = async function () {14 return await this.helpers.fingerprintUnlock();15};

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