Friday 27 June 2014

Simple steps to automate Android Application test cases using UIAutomator


Advantages:
  • Can be used on device displays with different resolution
  • Events can be linked with Android UI controls. For example, click on a button with text that says “Ok,” instead of clicking a coordinate location (x=450, y=550).
  • Can reproduce a complex sequence of user actions
  • Always performs the same sequence of actions, allowing us to collect performance metrics on different devices.
  • Can run several times and on different devices without changing any Java* code
  • Can use hardware buttons on devices
Disadvantages:
  • Hard to use with OpenGL* and HTML5 applications because these apps have no Android UI components.
  • Time consuming to write JavaScript*


Introduction:
Ui Automator is an Android Test Framework used for functional Testing, black box Testing. This is an easy to understand and execute. It’s open source. UI Automator identifies the ui element using their Text, Decription, Index, Class Names etc. UI Automator can be used to write Test Cases for any android App either default or the third party apps. This Doesn’t require the Tester to change the signature of the Apps thus can be used for the native apps like Settings.apk
UI Automator is also a JUnit Based Test Suite. So we can use the Assert Class to get the specific test case pass/fail Reason.
Limitation:
The biggest limitation of UI Automator framework is that it only works on API level16 or higher (Android 4.1 or later).


      Set Up for the UI Automator:
1) Android application apk file for Testing. Ex: ApplicationToTest.apk
2) Eclipse for building Test project
3) ADT (Android Development Tools)
4) SDK (Software Development Kit)
If you are using Eclipse for just your android Development then download eclipse from
This includes SDK‘s for android also.
5) JDK (Java Development Kit)
6) Go to the location where your SDK is present Check for android.jar and uiautomator.jar. It should be present under the sdk>Platform> (any Folder with) API level 16 or higher.


Creating the Test Project:

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:
Click Add Library > JUnit then select JUnit3 to add JUnit support.
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.
  1. Goto the project create a Package name as per standards and create a class file .
  2. Create New android application project.
And copy paste the below code:
package com.yatendra;
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;
public class writeMsg extends UiAutomatorTestCase{
public void testDemo2() throws UiObjectNotFoundException {
// this create a uiDevice instance an perss on the Home button
getUiDevice().pressHome();
//this will create a instance of the “Apps” on the home screen
UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
//this will click on the apps and wait for the new window to open
allAppsButton.clickAndWaitForNewWindow();
//this will create a ui object of the app tab using the String “Apps”
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
//This will click on the tab


appsTab.click();
//this will create a scroller object which we can use to perform some actions
UiScrollable appViews = new UiScrollable(
new UiSelector().scrollable(true));
//now it scrolls horizontal
appViews.setAsHorizontalList();
//now find the app you want to click using the Text i.e “Messaging” and create a instance of it


UiObject msgApp = appViews.getChildByText(new UiSelector().className(android.widget.TextView.class.getName()),
"Messaging");
//now Click on the object found using name
msgApp.clickAndWaitForNewWindow();
//Now it will opne a new activity identify it using its package name
UiObject msgUI = new UiObject(new UiSelector()
.packageName("com.android.mms"));
UiObject ui = msgUI.getChild(new UiSelector().className("android.widget.TextView").descriptionContains("New message"));
//check if the window is present or now or give a assert
assertTrue("Unable to find the message Ui ",ui.exists());
if (ui.exists()!= true){
getUiDevice().pressBack();
getUiDevice().pressBack();
}
ui.clickAndWaitForNewWindow();
//type the send to number
UiObject sendTo = new UiObject(new UiSelector().className("android.widget.MultiAutoCompleteTextView"));
sendTo.setText("9739562085");


//here I am handling a pop that sometimes appear
UiObject addToPop = new UiObject(new UiSelector().className("android.widget.EditText").textContains("Add to People"));
if (addToPop.exists()== true){
getUiDevice().pressBack();
}
//find the message text body ,write the message .
UiObject sendTomsg = new UiObject(new UiSelector().className("android.widget.EditText"));
sendTomsg.setText("Hello this i to test Meassaging apk");
}
}








Executing and Creating the Test automation JAR:
  1. Using Terminal enter the location where your automation project is present.


Path/yatendra$ android create uitest-project -n yatendra -t android-17 -p /Path/yatendra


  • Here the android-17 is the folder under sdk/platform where the uiautomator and android JARs are present
    • 1st yatendra is my project name
    • This will create the build file .xml
Path/yatendra$ ant build
    • This will create the Jar file .
    • Make sure the ant path is also set properly.
    • If ant is not install : sudo apt-get install ant1.8
    • Or sudo apt-get update or sudo apt-get install ant
    • if ant build showing error like below - use update-alternative --config Java
      1. BUILD FAILED
      2. /home/yatendra/adt-bundle-linux-x86_64-20131030/sdk/tools/ant/uibuild.xml:183: java.lang.UnsupportedClassVersionError: com/sun/tools/javac/Main : Unsupported major.minor version 51.0
    • If ant is showing error like below – use sudo apt-get install ant-optional
Or Check the sdk path and whether u have installed for 32 bit or 64 bit.


      1. Buildfile: build.xml
      2. BUILD FAILED
      3. /home/yatendra/Workspace_android/testAPK/build.xml:90: The following error occurred while executing this line:
      4. /home/yatendra/adt-bundle-linux-x86_64-20131030/sdk/tools/ant/uibuild.xml:105: No supported regular expression matcher found: java.lang.ClassNotFoundException: org.apache.tools.ant.util.regexp.Jdk14RegexpRegexp
      5. Total time: 0 seconds


  1. Now we need to push the jar file created using above command to the device under test.
Path/yatendra$ adb push bin/yatendra.jar /data/local/tmp
  • We are pushing yatendra.jar to /data/local/tmp


  1. Now to run the particular test case we need to execute one more command :
Path/yatendra$ adb shell uiautomator runtest yatendra.jar -c com.yatendra.writeMsg
  • Here com.yatendra.writeMsg is the particular testcase we are running.
  • Com.staish is package name and write_Msg is class name






No comments:

Post a Comment