Feb 24, 2014

UIAutomator Tutorial - Part II

In continuation to earlier post on UIAutomator Introduction / UIAutomator Basics lets discuss more UIAutomator Android with few examples

Reference: UIAutomator Documentation / Android Developer Docs

UIAutomator API in brief

It provides six different classes, these classes with interfaces and exceptions allows to capture and manipulate UI components on Android Device.

Following are the classes :
   UiDevice
   UiObject
   UiScrollable
   UiSelector
   UiCollection
   UiConfigurator

UiDevice
  Provides access to state information about the device. We can  also use this class to simulate user actions on the device, such  as pressing the d-pad hardware button or pressing the Home and Menu buttons.
UiObject
 Represents a user interface (UI) element.
UiScrollable
 Provides support for searching for items in a scrollable UI .
UiSelector
 Represents a query for one or more target UI elements on a device screen.
UiCollection
  Used to enumerate  screen elements for the purpose of counting, or targeting a sub elements by a child's text or description.
UiConfigurator
  Allows  to set key parameters for running uiautomator tests

We need to import following classes
import android.widget.LinearLayout;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.android.uiautomator.core.UiDevice;

UiObject/UISelector

Click on Text
new UiObject(new UiSelector().text(text)).click();

Click on Button
We need to use Button Class name for this
new UiObject(new UiSelector().text(btnText).className("android.widget.Button")).click();

Long Click
new UiObject(new UiSelector().text(text)).longClick();

Press Back
getUiDevice().pressBack();

Go Back to Home
getUiDevice().pressHome();

Click and Wait
new UiObject(new UiSelector().description(text)).clickAndWaitForNewWindow();

Check if text exists
new UiObject(new UiSelector().text(value)).exists();

Wait until 
new UiObject(new UiSelector().className(android.widget.ProgressBar.class.getName())).waitUntilGone(1000);


UIDevice

Example : Swipe Down Notification

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObjectNotFoundException;

public static void swipeDownNotificationBar() throws UiObjectNotFoundException {
UiDevice deviceInstance = UiDevice.getInstance();
int dHeight = deviceInstance.getDisplayHeight();
int dWidth = deviceInstance.getDisplayWidth();
int xScrollPosition = dWidth / 2;
int yScrollStop = dHeight / 2;
UiDevice.getInstance().swipe(xScrollPosition, 0, xScrollPosition, yScrollStop, 100);
}

UiScrollable

Example: 
1) Scroll and Click event

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiScrollable;

new UiScrollable(new UiSelector().scrollable(false))
.scrollIntoView(new UiSelector().text(text));
new UiObject(new UiSelector().text(text)).click();

2) Scroll Back
new UiScrollable(new UiSelector().scrollable(true)).scrollBackward();

How to create a project in Eclipse

Create a Java project in Eclipse
After Creating Java Project, add external jar files which are android.jar and uiautomator.jar
android.and uiautomator.jar are present in
“D:\adt-bundle-windows-x86\adt-bundle-windows\sdk\platforms\android-4.2”

Create Java File

Create Test.java in project / eclipse work space, say it is in D:\UIAutomator

public void clickText(String text) throws UiObjectNotFoundException {
    new UiObject(new UiSelector().text(text)).click();
}

How to build UIAutomator project

You need Apache Ant software for building Java project

Syntax:
android create uitest-project -n "project-name" -t 1 -p "project-location"

"-t" referes to Android target (in case of different versions of Android are present, you can define which target to build this JAR)

E.g.,
cd D:\UIAutomator
D:
android create uitest-project -n UIAutomatorTest -t 1 -p D:\UIAutomator
ant build
cd bin
adb push UIAutomatorTest.jar /data/local/tmp
cd ..

The above commands create a JAR file in the bin folder
Then push the JAR file to /data/local/tmp folder of Android phone

How to call UIAutomator methods from Adb Shell

Go to command line and execute the below command

adb shell uiautomator runtest UIAutomatorTest.jar -c com.test.uiautomator.Test#clickText -e text "Phone"

Here Test refers to Test.java class
ClickText refers to the method inside Test.java
-e <Name> <Value> pairs for passing to test classes

If you haven't go through earlier post, Please go through


3 comments:

  1. HI,

    Thanks for the article.
    I have recently published Uiautomator-bot tool for automating Uiautomator. It makes creation and executing of Uiautomator Test cases on multiple devices simultaneously very easy and simple. You can download it from
    https://sourceforge.net/projects/uiautomator . For further details refer http://uiautomator-bot.blogspot.in/

    Thanks. waiting for your valuable inputs.

    ReplyDelete
    Replies
    1. Hi Syed,
      i dont know java, is it possible to execute python testcases on multiple devices simultaneously using uiautomator-bot?? if yes please help me.

      Delete
  2. hi,
    as mentioned above click on button can be done if the button has some text assosiated with it, but if the button contains only image but no text on it, how can we access that button ?
    Thanks in advance

    ReplyDelete