Tuesday, December 3, 2013

Writing your first test in UiAutomator


Configure your development environment
If you're developing in Eclipse, the Android SDK provides additional tools that help you write test cases using uiautomator and buiild your JAR file. In order to set up Eclipse to assist you, you need to create a project that includes the uiautomator client library, along with the Android SDK library.
To configure Eclipse:  

  1. Create a new Java project in Eclipse, and give your project a name that is relevant to the tests you’re about to create (for example, "MyAppNameTests"). In the project, you will create the test cases that are specific to the application that you want to test.
  2. From the Project Explorer, right-click on the new project that you created, then select Properties > Java Build Path, and do the following:
    1. Click Add Library > JUnit then select JUnit3 to add JUnit support.
    2. Click Add External JARs... and navigate to the SDK directory. Under the platforms directory, select the latest SDK version and add both the uiautomator.jar and android.jar files.
If you did not configure Eclipse as your development environment, make sure that the uiautomator.jar and android.jar files from the <android-sdk>/platforms/<sdk> directory are in your Java class path.
Once you have completed these prerequisite tasks, you're almost ready to start creating your uiautomator tests.


First Test case:
1. Open App tray by clicking on the AppTray icon at the center
2. Click on Apps Tab
3. Click on Widgets Tab


Let us automate above test case using UiAutomator

Step 1: Click on App Tray icon



Procedure:
1. Connect your device to your PC
2. Make sure Android sdk path is added to PATH variable
3. run the command "uiautomatorviewer" to launch the UiAutomator viewer
4. Move your mouse hover the App Tray icon, it shows the properties on the right pane.
5. In the above screenshot, it shows the content description as "Apps"
6. Write java code to launch the App Tray icon having the content description "Apps"

here is the code


//Get the device state

UiDevice mydevice = getUiDevice();

 

//If screen is off, wake up the device (make sure the screen lock is set to None)

if(!mydevice.isScreenOn()){

    mydevice.wakeUp();

}

 

//Press Home button 

mydevice.pressHome();

 

//Create UiObject instance for the UI element

UiObject AppTray_Button = new UiObject(new UiSelector().description("Apps"));

 

//Check if the button exists

if(AppTray_Button.exists()){

    //Click on the Button

    AppTray_Button.clickAndWaitForNewWindow();

}


UiDevice:
Represents the device state. Using which, we can to check the state of various properties, such as current orientation or display size and to perform device level actions, such as forcing the device into a specific rotation, pressing the d-pad hardware button, or pressing the Home and Menu buttons

To get the access, create an instance of UiDevice



UiDevice mydevice = getUiDevice();

 mydevice.pressHome();

UiObject:
Represents a UI element. To create a UiObject instance, use a UiSelector that describes how to search for, or select, the UI element



UiObject AppTray_Button = new UiObject(<Ui Selector instance>);

UiSelector:
Represents a search criteria to query and get a handle on specific elements in the currently displayed UI. If more than one matching element is found, the first matching element in the layout hierarchy is returned as the target UiObject. When constructing a UiSelector, you can chain together multiple properties to refine your search. If no matching UI element is found, a UiAutomatorObjectNotFoundException is thrown. You can use the childSelector() method to nest multiple UiSelector instances.

The following code shows how to select an UI element having the desciption "Apps"


UiObject AppTray_Button = new UiObject(new UiSelector().description("Apps"));
Click on the Ui element:
You can reuse the UiObject instances that you have created in other parts of your app testing, as needed. Note that the uiautomator test framework searches the current display for a match every time your test uses a UiObject instance to click on a UI element or query a property.

Using the UiObject "AppTray_Button", we can check if the button exists in the current display and click on the Apps button if it exists


if(AppTray_Button.exists()){

        AppTray_Button.clickAndWaitForNewWindow();

}


Step 2: Click on the Apps tab

1. In the Step 1, after clicking the Apps icon, it launches App Tray
2. Check if the Apps tab exists
3. Click on the Apps Tab




If you look at "Apps" Tab properties, the element has content description "Apps" and having the class name "com.widget.textview". Use UiSelector to select the element matching these properties
 


UiObject AppsTab = new UiObject(new UiSelector().className("android.widget.TextView").description("Apps"));
       
Click on the Apps tab if it exists

if(AppsTab.exists()){

    AppsTab.click();

}

Step 3: Click on Widgets Tab

1. Check if Widgets tab exists
2. Click on the widgets tab

Select the UI Element having the Textview class and description "Widgets"



UiObject WidgetsTab = new UiObject(new UiSelector().className("android.widget.TextView").description("Widgets"));

 

Since the Widgets tab was not selected by default, you can also use the property "selected = false"


UiObject WidgetsTab = new UiObject(new UiSelector().className("android.widget.TextView").description("Widgets").selected(false));

Here is the complete code:

package clock.ui;

import android.os.RemoteException;   

import com.android.uiautomator.core.UiDevice;

import com.android.uiautomator.core.UiObject;

import com.android.uiautomator.core.UiObjectNotFoundException;

import com.android.uiautomator.core.UiSelector;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

 

 

public class Screen1 extends UiAutomatorTestCase{   

 

    public void testTestScreen1() throws RemoteException, UiObjectNotFoundException{

 

        //Get the device state

        UiDevice mydevice = getUiDevice();        

 

        //If screen is off, wake up the device (make sure the screen lock is set to None)

        if(!mydevice.isScreenOn()){

            mydevice.wakeUp();

       }

 

        mydevice.pressHome();

 

        //Create UiObject instance for the UI element

        UiObject AppTray_Button = new UiObject(new UiSelector().description("Apps"));

 

        //Check if the button exists

        if(AppTray_Button.exists()){

 

            //Click on the Button

            AppTray_Button.clickAndWaitForNewWindow();

        }        

 

        //Get instance of Apps Tab 

        UiObject AppsTab = new UiObject(new UiSelector().className("android.widget.TextView").description("Apps"));

 

        //Check if the Apps Tab exists

        if(AppsTab.exists()){

            AppsTab.click();

        }

 

        UiObject WidgetsTab = new UiObject(new UiSelector().className("android.widget.TextView").description("Widgets").selected(false));

        

        //Check if Widgets tab exists

        if(WidgetsTab.exists()){

            WidgetsTab.clickAndWaitForNewWindow();

        }

 

     }

}