Class AndroidDriver (implements AppiumDriver interface) has certain methods for locking and unlocking an android device / emulator at run time. (should upgrade java-client.jar to version 4)
Methods:
- isLocked() – Check if device is locked
Returns true, if device is locked, else returns false
- Unlock a device
- Lock a device
NOTE: these methods doesn’t hold good if you have set pass code / pin code or patterns to unlock.
Open emulator or connect android mobile to computer, start Appium and run below code
public class Dialer { static DesiredCapabilities cap; static AppiumDriver driver; @BeforeTest static void setup() throws MalformedURLException{ cap = new DesiredCapabilities(); cap.setCapability(CapabilityType.PLATFORM, "Android"); cap.setCapability(CapabilityType.VERSION, "5.1.0"); cap.setCapability("deviceName", "mygeny510"); cap.setCapability("appPackage", "io.selendroid.testapp"); cap.setCapability("appActivity", ".HomeScreenActivity"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap); } @Test static void Selendroid(){ if (((AndroidDriver) driver).isLocked()) ((AndroidDriver) driver).unlockDevice(); else ((AndroidDriver) driver).lockDevice(); } }