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

android, android-uiautomator, automation, ui-automation

Solution

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.

- Go to 'Settings' -> 'Developer Options'.

- 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.

Problem

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`?

Original source