Appium: How to handle device lock and unlock actions for Android Emulators

Girish Nair
CodeX
Published in
5 min readAug 11, 2022

Hello Everyone!! In this quick article, we will explore how we can lock and unlock android emulator devices using various techniques such as PIN, Pattern, password, etc using Appium Desired Capabilities. Let’s get started.

Let’s say you need to test a scenario with your mobile app wherein we need to check App State post device lock is performed. For instance, let’s say you are working with secure applications such as some banking application or any trading app, maybe you would like to validate that every time user performs a login to the app, does some action, and then locks the device, the next time they unlock the device the mobile app state should be reset and they will need to log in again. The vice-versa is also valid wherein you might need to save the app state post a lock/unlock action is done by the user, let’s take a look as to how we can handle this scenario.

For our testing purposes, I am using an Android emulator which is running locally in my system and we will test on Swag Labs app provided by Sauce Labs using Appium server running locally.

When you launch the Swag labs app in an android emulator, you will be need to perform a login using username and password details.

The mobile app has some pre-configured usernames which can be used. The password for all users is the same as shown below (“secret_sauce”)

UserNames for SauceLabs

How can we lock, unlock and check the lock status in the android emulator device?

AndroidDriver provides the following methods to lock, unlock and check lock status of the deviceisDeviceLocked(), lockDevice() and unlockDevice().

These methods are available in AndroidDriver, so if your driver is of type AppiumDriver then you need to cast it to AndroidDriver.

((AndroidDriver<MobileElement>)driver).unlockDevice();

The method is isDeviceLocked() will return a boolean value (true or false)

There is also an overloaded method available for lockDevice() wherein we can specify the duration for which it needs to be locked.

Let’s take a look at the steps that we will be performing

• Launch the Swag Labs app already installed on the emulator device.
• Perform login using username: standard_user and password: secret_sauce
• Wait for 5 seconds
• Lock the device.

For now, I am not using any separate Driver class for initializing nor using any Page Object model concept, this is just the bare minimum script that should work. Go ahead, you can call me lazy!! 😜

Test Run — Lock Device

Now, that we have seen how can lock the device, let’s see the various ways in which we can unlock the device.

As you might be aware, that are multiple ways in which we can unlock devices for Android, this can be the default option wherein you just press the power button to lock/unlock the device (i.e. no PIN/Password/Pattern set) or it could be you have setup some PIN or Password or a Pattern.

All these options can be handled via Appium DesiredCapabilities and you will need to specify “unlockType” and “unlockKey” details for the same.

Unlocking the Android Emulator device using a PIN

In this case, we have some setup some PIN for our android emulator device. This can be done from Settings App > Security > Device Security > Screen Lock

In our case, I will just set a PIN: 123456 for my device.

Accordingly in my script, I have mentioned these details using desired capabilities and now my setup method will look as below.

Also In my test method post lockDevice(), we will have to add unlockDevice()

Added Device Unlock Capabilities
Test Class updated

This is what my test run looks like now post adding these lines, as you see post the device lock it will automatically unlock it using the PIN that we had set.

Unlocking the Android Emulator device using Password

The approach for this will be similar to PIN, we will need to specify the details: unlockType as ‘password’ and unlockKey as the password text that you have set in your device. Desired Capabilities will be as below.

In my example case, I have set the password as “Android”

Unlocking the Android Emulator device using Pattern

As you might be aware, the pattern looks as shown below.

Now, how this works for unlocking in Appium is that each dot corresponds to a specific number starting from 1.

Top row: the three dots will be 1 2 3
Middle row: the three dots will be 4 5 6
Last row: the three dots will be 7 8 9

Assume, I have set a pattern which is the English character “Z” let’s say that I have decided to start from the top left corner.

My pattern should be “1235789”

Let’s add these details to our setup script (Desired Capabilities)

And that’s it, this is how you can lock and unlock your android emulator devices using Appium DesiredCapabilities. 😎

Thanks for reading!!!

--

--