How to unlock the pattern locked device through appium?

Hi, I’m testing an application which is related to secure domain and for installing and running that application, I must need to enter pattern lock or pin lock. My question is. is there any way by which I can unlock the pattern or pin through appium while running my tests in Appium?

PS: I’m not using rooted device, and also not having the access to internal files. Also I am planning to run scripts on various devices due to which location based locators may not work.

To lock/Unlock the screen

adb shell input keyevent KEYCODE_POWER

To swipe up for obtainging the PIN screen

adb shell input swipe 800 400 400 400

Enter PIN

adb shell input text 0000

Tap on Enter to unlock

adb shell input keyevent KEYCODE_ENTER

@mashkurm, that’s an interesting problem. For the purposes of solving this problem, I’ll assume you can both get to the pattern-lock screen and identify that you are on it.

The points on the pattern are not individually addressable, so we have no easy way of obtaining their exact locations. We can, however, calculate where they should be. First, let’s get the element by resource-id. On a Nexus 5 with Android M, this resource-id is com.android.systemui:id/lockPatternView
I haven’t tested this on other devices, so you’ll need to check that resource-id on whatever devices you test.

Once you have that element, you can obtain the size. On my device, its location/size is: [84,737][996,1649], or a 912x912 square. The center column and row would then be at 456. The outer columns and rows appear to be 2/3’s of the distance between the center point and the edge, so you could calculate what those are as well, e.g. the upper left corner is (152,152) from the upper left corner, or (236, 1148) on my display).

Now that you’ve obtained the location of all the points, it’s relatively simple to construct an algorithm to swipe from point to point, although you should be careful not to lift the swiping finger.

Best of luck, and please let us know how it goes

1 Like

have tried this but it did not unlock my android phone:

@Test
public void Tc3_Unlock_Pattern() throws Exception {
System.out.print(“TC3:Unlock pattern”);
Runtime.getRuntime().exec(“adb shell input swipe 257 1235 572 1235”);
Thread.sleep(500);
Runtime.getRuntime().exec(“adb shell input swipe 572 1235 260 1564”);
Thread.sleep(500);
Runtime.getRuntime().exec(“adb shell input swipe 260 1564 532 1504”);
Thread.sleep(500);
System.out.print(“TC3:Executed Successfully”);
}

.I even tried this but it did not work:

Integer x_topLeftCorner=257;
Integer y_topLeftCorner=1235;

Integer x_topRightCorner=572;
Integer y_topRightCorner=1235;

Integer x_bottomLeftCorner= 260;
Integer y_bottomLeftCorner=1438;

Integer x_bottomRightCorner=532;
Integer y_bottomRightCorner=1504;

//Use coordinates to draw the security pattern and unlock the screen 
TouchAction obj = new TouchAction(MBase.getDriver()); 
obj.press(x_topLeftCorner,y_topLeftCorner).moveTo(x_topRightCorner,y_topRightCorner).moveTo(x_bottomLeftCor

Try adding the optional fifth parameter to ‘input swipe’ to slow down your swipe.

In developer options on Android, you can enable visible touches on the screen, so you can verify your swipe occurs and is in the right spot visually.

@ramaninaresh / @willosser - We can use capabilities, where we can directly set the unlockType and unlockKey…

unlockType: [‘pin’, ‘password’, ‘pattern’, ‘fingerprint’]

unlockKey; If you want to draw suppose ‘L’, then it would be 1478 in key section.

Let me know if this doesn’t work.

Appium version - 1.6.4

1 Like

That’s cool, @mashkurm. Something to keep in mind in case I ever have to implement it.

@mashkurm I tried using the capability of unlockType and unlockKey in Android 10 OS device. But I am getting the exception Noelement exception since I have added my parameters correctly.

cap.setCapability(“unlockType”, “pattern”); //Trying to unlock the device using pin or pattern
cap.setCapability(“unlockKey”, “7586”);