androidcommand-lineautomated-testsrooted-device

How to automate unlocking the screen on android for testing?


I need to run some automated tests on an android device, the problem is that the screen needs to be unlocked first. I have tried:

input keyevent KEYCODE_MENU

But this hasn't worked.

I have access to a rooted device for this purpose and have removed the password. Just need a way to unlock the screen.


Solution

  • If you set your lock screen to none, you just need to send a power on command using adb.

    # Switches on the android devices screen if it isn’t already on.
    if `adb shell dumpsys input_method | grep mInteractive`.include? 'false'
      `adb shell input keyevent KEYCODE_POWER`
    end
    
    # Pre lollipop devices respond to this command a bit differently.
    if `adb shell dumpsys input_method | grep mScreenOn`.include? 'false'
      `adb shell input keyevent KEYCODE_POWER`
    end
    

    The above will press the power button if the screen isn't already on.