How to use driver.unlock method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

Run Appium Android Driver automation tests on LambdaTest cloud grid

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

steps.js

Source: steps.js Github

copy
1import { Given, When, Then } from '@cucumber/cucumber';
2import LandingPage from '../pageobjects/landing.page';
3import LoginPage from '../pageobjects/login.page';
4
5Given(/^I open the demo app$/, async () => {
6    await console.log('Mobile app is open');
7});
8
9When(/^I lock device$/, async () => {
10    await driver.lock();
11});
12
13When(/^I unlock device$/, async () => {
14    await driver.unlock();
15});
16
17When(/^I navigate to login tab$/, async () => {
18    await LandingPage.btnLogin_tap();
19});
20
21When(
22    /^I initiate login with ([^"]*)? and ([^"]*)?$/,
23    async (email, password) => {
24        await LoginPage.txtEmail_setText(email);
25        await LoginPage.txtPassword_setText(password);
26        await LoginPage.btnLogin_tap();
27    },
28);
29
30Then(/^I close the app$/, async () => {
31    await driver.closeApp();
32});
33
Full Screen

index.js

Source: index.js Github

copy
1'use strict';
2
3(function () {
4    function getDriver (callback) {
5        var interval = window.setInterval(function () {
6            var testCafeDriver = window['%testCafeDriverInstance%'];
7
8            if (testCafeDriver) {
9                window.clearInterval(interval);
10                callback(testCafeDriver);
11            }
12        }, 50);
13    }
14
15    // NOTE: enable interaction with a page when the last test is completed
16    var UNLOCK_PAGE_FLAG = 'testcafe-live|driver|unlock-page-flag';
17
18    // TestCafe > 0.18.5 required
19    getDriver(function (testCafeDriver) {
20        var testCafeCore = window['%testCafeCore%'];
21        var hammerhead   = window['%hammerhead%'];
22
23        testCafeDriver.setCustomCommandHandlers('unlock-page', function () {
24            testCafeCore.disableRealEventsPreventing();
25
26            testCafeDriver.contextStorage.setItem(UNLOCK_PAGE_FLAG, true);
27
28            return hammerhead.Promise.resolve();
29        });
30
31        var chain = testCafeDriver.contextStorage ? hammerhead.Promise.resolve() : testCafeDriver.readyPromise;
32
33        chain.then(function () {
34            if (testCafeDriver.contextStorage.getItem(UNLOCK_PAGE_FLAG))
35                testCafeCore.disableRealEventsPreventing();
36        });
37    });
38})();
Full Screen

exercice3.js

Source: exercice3.js Github

copy
1// https://webdriver.io/docs/api/appium.html
2
3describe('Exercice 3', function() {
4	it('Can open notifications center', function() {
5		driver.openNotifications()
6		driver.pause(3000)
7	})
8
9	it('Can lock device', function() {
10		driver.lock()
11		driver.pause(3000)
12		expect(driver.isLocked()).toBe(true)
13		driver.unlock()
14	})
15
16	it('Can open settings app', function() {
17		// android
18		// adb shell dumpsys window windows | grep -E 'mCurrentFocus'
19		driver.startActivity('com.android.settings', 'com.android.settings.Settings')
20		driver.pause(3000)
21
22		// ios
23		// https://emm.how/t/ios-12-list-of-default-apps-and-bundle-id-s/790
24		// driver.launchApp({ bundleId: "com.apple.Preferences" })
25	})
26})
Full Screen

itemsController.js

Source: itemsController.js Github

copy
1angular.module("gdCameApp").controller("ItemsController",
2[
3    "$rootScope", "$scope", "gameData", "gdCameApiService",
4    function ($rootScope, $scope, gameData, gdCameApiService) {
5        $scope.items = [];
6        $scope.itemsGroups = [];
7
8        gameData.then(function (data) {
9            $scope.items = data.items;
10            var indexInGroup = 0;
11            var itemsGroup = [];
12            $scope.items.forEach(function (value, index) {
13                if (indexInGroup === 4) {
14                    $scope.itemsGroups.push(itemsGroup);
15                    itemsGroup = [];
16                    indexInGroup = 0;
17                }
18                itemsGroup.push(value);
19                indexInGroup++;
20            });
21            if (itemsGroup.length > 0) {
22                itemsGroup.push(new Array(4 - itemsGroup.length));
23                $scope.itemsGroups.push(itemsGroup);
24            }
25        });
26
27        $scope.$on("update.items",
28            function (event, value) {
29                $scope.items = value.items;
30            });
31
32        $scope.buyFundDriver = function (fundDriverId) {
33            var oldFundDriver = $scope.items.filter(function (item, index) {
34                return item.id === fundDriverId;
35            })[0];
36
37            if (oldFundDriver && oldFundDriver.unlockBalance > $rootScope.gameData.cash.counters[0].value) {
38                return;
39            }
40
41            gdCameApiService.buyFundDriver(fundDriverId)
42                .then(function (value) {
43                    if (value.data == undefined) return;
44                    var itemIndex;
45                    var oldFundDriver = $scope.items.filter(function (item, index) {
46                        itemIndex = index;
47                        return item.id === value.data.itemBuyInfo.id;
48                    })[0];
49
50                    if (oldFundDriver != undefined) {
51                        oldFundDriver.bought = value.data.itemBuyInfo.bought;
52                        oldFundDriver.price = value.data.itemBuyInfo.price;
53
54                        if (value.data.modifiedCountersInfo != undefined) {
55                            $rootScope.$broadcast("counters.update", value.data.modifiedCountersInfo);
56                        }
57                    }
58                });
59        };
60    }
61]);
Full Screen

dataHandler.js

Source: dataHandler.js Github

copy
1/**
2 * (C) Copyright 2015 Manuel Martins.
3 *
4 * This module is inspired by json_file_system.
5 * (json_file_system is Copyright (c) 2014 Jalal Hejazi,
6 *  Licensed under the MIT license.)
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *     http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * Created by: ManuelMartins
21 * Created on: 26-01-2016
22 *
23 */
24
25'use strict';
26
27var Memory = require('./memory');
28var Disk = require('./disk');
29
30/**
31 * Provides the most common I/O operations.
32 *
33 * @param {object} options
34 * @param {object} options.db
35 * @param {object} options.db._
36 * @param {string} options.db._._driver
37 * @constructor
38 */
39function DataHandler(options) {
40  options = options || {};
41  var dataHandler = options.db._db._driver || 'disk';
42  switch (dataHandler) {
43    // load the driver to be used just once
44    case 'memory':
45      this.dataHandlerDriver = new Memory(options);
46      break;
47    case 'disk':
48    default:
49      this.dataHandlerDriver = new Disk(options);
50  }
51}
52
53/**
54 * Writes a list of objects to the data driver
55 *
56 * @param content the content to write
57 * @param callback
58 */
59DataHandler.prototype.set = function write(content, callback) {
60  return this.dataHandlerDriver.set(content, callback);
61};
62
63/**
64 * Reads a list of objects from the data driver
65 *
66 * @param callback
67 */
68DataHandler.prototype.get = function read(callback) {
69  return this.dataHandlerDriver.get(callback);
70};
71
72/**
73 * Locks the data provider
74 *
75 * @param callback
76 */
77DataHandler.prototype.lock = function read(callback) {
78  return this.dataHandlerDriver.lock(callback);
79};
80
81/**
82 * Unlocks the data provider
83 *
84 * @param callback
85 */
86DataHandler.prototype.unlock = function read(callback) {
87  return this.dataHandlerDriver.unlock(callback);
88};
89
90
91module.exports = DataHandler;
92
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Android Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)