Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate unlock pattern on a real phone using uiautomator?

I have recently started learning uiautomator for the UI testing of various Android devices. Currently I am testing on Galaxy S4.

I am looking for any class or method which can be used to automate the unlock pattern that user draws to unlock the phone. For example, I have letter N as a "draw pattern" to unlock the phone. How can I automate this unlock pattern in uiautomator?

like image 684
rumit patel Avatar asked Dec 02 '13 03:12

rumit patel


People also ask

How do you run the UIAutomator?

To launch the uiautomatorviewer tool: Launch the target app on a physical device. Connect the device to your development machine. Open a terminal window and navigate to the <android-sdk>/tools/ directory.

What is uiautomator2?

UIAutomator is Google's test framework for native app automation at the UI level. Typical usage would be to pass the following in desired capabilities: automationName: uiautomator2. With version 1.6, Appium has provided support to UiAutomator 2.

How to unlock Android phones with pattern unlock?

Install Dr.Fone and launch it to perform pattern unlock. From the home screen, select the “Screen Unlock” option. 2. Connect your device to the system. Once it is detected, click on the “Unlock Android Screen” button.

What is uiautomator in android testing?

Answer: UiAutomator is a testing framework/tool provided by Android for automating Android application testing, Appium internally uses it internally. Q #12) How do you inspect an element in UIAutomator?

How to unlock Android pattern lock if forgotten using Device Manager?

Here are simple steps on how to unlock Android pattern lock if forgotten using Android Device Manager: Step 1: To commence it, visit the Android Device Manager official website either from your computer or another device. Now, enter account details which you have entered in your Android device.

What version of uiautomator should I set the automationname to?

In the case of this driver, for Appium versions below 1.14.0 no automationName capability should be used, while for version 1.14.0 and above the automationName should be set to UiAutomator1.


3 Answers

Suppose you have letter "N" as unlock pattern, then first you have find the co-ordinates of each point of that N shape in your device. As you mentioned, the entire pattern lock will have 9 dots, you have to get (x,y) co-ordinates of 4 dots. To get the co-ordinates, you can use the same method mentioned earlier in one of the answer.

  1. Go to 'Settings' -> 'Developer Options'.
  2. Under 'INPUT' section -> you will find a option 'Pointer Location' -> enable that option.

Once you get your 4 dots' co-ordinates, use swipe(Point[] segments, int segmentSteps) method of UiAutomator Framework.

The input for this method is the 4 set of co-ordinates that you got from your device screen as Point array. This will give continuous swipe through the points.

I have given a sample script below for your understanding.

import android.graphics.Point;

public void unlockpatternlock() throws UiObjectNotFoundException, Exception {
    Point[] cordinates = new Point[4];
    cordinates[0] = new Point(248,1520);
    cordinates[1] = new Point(248,929);
    cordinates[2] = new Point(796,1520);
    cordinates[3] = new Point(796,929);
    getUiDevice().wakeUp();
    getUiDevice().swipe(cordinates, 10);
} 

Above script would draw N shape smoothly. Remember input the co-ordinates according to your device screen.

like image 57
sriharry16 Avatar answered Oct 30 '22 11:10

sriharry16


This is the only way I know to do it, but it can be tedious trying to find your x and y coordinates.

UiDevice.getInstance().swipe(int startX, int startY, int endX, int endY, int steps)

The only probelm I see is to do an "N", you would need 3 of these swipe's. To unlock, it needs to be one continuous swipe.

Give it a show. Finding your x and y will be tough. I would go to my "apps home" page and look at apps (with the uiautomatorviewer) that are in relatively the same spot, find their coords, then go from there.

NOTE The int steps is how fast and "smooth" you want to swipe. I like to use 5 or 10. It seems pretty natural.

like image 32
Chad Bingham Avatar answered Oct 30 '22 11:10

Chad Bingham


To find out the co-ordinates of the screen , you can follow this :

[1] Go to 'Settings' -> 'Developer Options'.
[2] Under 'INPUT' section -> you will find a option 'Pointer Location' -> enable that option.

After that if you touch anywhere on the screen -> you can view the exact screen coordinates of that point on top of the screen on your device.

And after you get the coordinates , you can use swipe method say like this -

UiDevice.getInstance().swipe(390, 1138, 719, 1128, 40);

method easily giving the exact co-ordinates where to drag from and till what point.

I have already used this and it works!

like image 42
Smriti Avatar answered Oct 30 '22 13:10

Smriti