Lock and Wake/Unlock the device screen (Android, Ruby)

We need to be able to lock and unlock the device for testing. I know that Appium does this as part of start up, but it doesn’t look like the method to unlock is available (See Lock() and unlock() android methods are not displayed appium 1.2.3).

I’d also like to wake up the device if it is not awake. I see there is a wake.apk installed on the device, but I can’t find an example of how to call it.

Ruby supports lock.

There’s a work around for unlock (close_app then launch again) however I agree we should add an unlock method if it’s not available already.

To press on lock button, execute adb shell input keyevent 26

@kirill, appium doesn’t support direct access to adb, so you would need to use
appium.driver.press_keycode 26
Similarly, 82 can be used to unlock, assuming there is no passcode on the lock screen. But simulating these key presses doesn’t work if I can’t detect if the screen is off or not. If it’s on and I press the power button (26), I would turn it off, and then I can’t unlock by using the unlock key code.

@bootstraponline, should I file an issue?

@willosser, I am not saying to use it via appium. Execute it as shell command…

@kirill, that’s always a possibility, but requires me to support code outside of Appium. I’d rather have it made available for everyone.

Sorry, I don’t get wha is the problem…
What language are you using?
For example on Ruby in your code to execute bash command you do like that:

`adb shell input keyevent 26`

or

system("adb shell input keyevent 26")

Just create the method like press_lock_button which contain this single line. It is readable. Doesn’t require any support. You could provide UDID though… It is simple and available for everyone.
Sorry, if I got you wrong… Let me know, I will try to figure out how to do that in different way :smiley:

Yes. If the other devs approve then I’d expect this feature to be added to appium.

Neither of those methods work on remote devices (for example Sauce Labs).

Hey Guys, I was trying to run my script over multiple devices In parallel But this Unlocking thing is causing hurdle for me:

Here it is like:

When I keep my devices unlocked only then script runs in parallel but as soon as my devices are locked then script run one by one on each devices (First unlocking screen in one devices and run script and then same follows on second devices)

My usecase is to achieve parallelism without manually unlocking devices.

Can someone advise and suggest on this scenario?

Looking forward for help

@Shankeyisgem, I’d love to hear more about how you are running everything in parallel over a single script, and it also might help up debug your problem.

In the mean time, you can set your device to never sleep (which is what I assume you mean by locking) if you set “Developer Options”-> “Stay Awake” to true

1 Like

Hello @Willosser,

Thanks for your response. It is combination of selenium Grid and my devices registration with this.

So Here It is:

  1. Making sure my Selenium grid is up and running

  2. Then registering Node android devices with Grid (Having a bat file for the nodes).

  3. Then In eclipse Passing my Script to Testng.XML

and with all this, I am able to achieve parallelism but only when my devices are not locked (Awake) but problem starts with devices locked and then Parallelism fails.

Here is Testng.XML

<?xml version="1.0" encoding="UTF-8"?>
<test name="Moto E1">
    <parameter name="port" value="4567"></parameter>
    <parameter name="deviceID" value="192.168.1.24:5555"></parameter>
    <classes>

        <class name="SamplePackage.ThreeBrowsers">
        <methods>
            <include name="testcase" />
        </methods>
        </class>
    </classes>
</test>
<test name="Mote E2">
    <parameter name="port" value="4568"></parameter>
    <parameter name="deviceID" value="192.168.0.6:5555"></parameter>
    <classes>
        <class name="SamplePackage.ThreeBrowsers">
        <methods>
            <include name="testcase" />
        </methods>
        </class>
    </classes>


Please let me know if anything missing in this or some other issue?

This is not my area of expertise, but it seems right to me. It would be interesting to compare the appium server logs in a successful vs. unsuccessful scenario. As I said in my last post, my likely solution would be to disable device locking.

Lock/Unlock the screen using the below command

adb shell input keyevent KEYCODE_POWER

Swipe up to obtain the PIN screen

adb shell input swipe 800 400 400 400

Entering PIN

adb shell input text 0000

for tapping Enter key

adb shell input keyevent KEYCODE_ENTER

“Developer Options”-> “Stay Awake” to true
Thanks you a lot. It works for me.