Tuesday, May 20, 2014

Longclick an object

Longclick an object

With the latest Android version of Kitkat the long click in uiautomator over an object does not function as expected. It works similar to a click thereby blocking a part of your automation.
This can be worked around by using the swipe functionality.

For instance, if you want to click on an object using its text attribute, you can follow the code snippet below to perform long click. Make sure the number of steps mentioned in the swipe() is high.

Code Snippet:

public static void longClick(String text) {
  UiObject object = new UiObject(new UiSelector().textContains(text));
  Rect coordinates = object.getBounds();
  mUiDevice.swipe(coordinates .centerX(), coordinates .centerY(), 
    coordinates .centerX(), coordinates .centerY(),
    100);
}

Saturday, November 16, 2013

Running ADB shell commands within uiautomator


Running ADB shell commands within uiautomator

ADB Input Keyevents:


While running UI automation tests we would love to set up our prerequisites for the test with as much little UI interaction as possible. This will ensure that the tests would not get blocked due to failures in the prerequisites.

For instance, you can go to the home of your phone/tablet/emulator with a simple adb shell command:
adb shell input keyevent KEYCODE_HOME (or)
adb shell input keyevent 3

For more details about input key events you can look into this link,
http://developer.android.com/reference/android/view/KeyEvent.html

Implementation with uiautomator:

The usage of the above adb shell commands can be implemented by using exec() property which executes the specified command and arguments in a separate process.
The output of the command is then read by using an Buffered reader.

Code Snippet:


public String runADBCommand(String adbCommand) throws IOException {
  String returnValue = "", line;
  InputStream inStream = null;
  try {
    Process process = Runtime.getRuntime().exec(adbCommand);
    inStream = process.getInputStream();
    BufferedReader brCleanUp = new BufferedReader(
                                               new InputStreamReader(inStream));
    while ((line = brCleanUp.readLine()) != null) {
         returnValue = returnValue + line + "\n";
    }
    brCleanUp.close();
    try {
         process.waitFor();
    } catch (InterruptedException e) {
         e.printStackTrace();
    }
  } catch (Exception e) {
    e.printStackTrace();
    System.err.println("Error: " + e.getMessage());
  }
  System.out.println(returnValue);
  return returnValue;
 }


Sunday, September 22, 2013

Starting with uiautomator


Uiautomator:


Uiautomator was introduced along with the release of Android Jelly Bean 4.2.
Its a perfect tool to start off with your UI Testing on Android Devices.
Its a clear indication of support provided by Google to help the testers struggling with their changing frameworks.

Prerequisites to test:

  • Have your Android SDK version upgraded to revision 21.
  • If you do not have the Android SDK installed in your system. Here is the link to get going: http://developer.android.com/sdk/index.html
  • Your test device must have an Android API version 16 or more. 
  • You can the device's Android API version through the following command:  
  • adb shell getprop ro.build.version.release
    

Starting with the tests:

  • Open Eclipse downloaded from the above link and create a new Java project
  • Import the android.jar and uiautomator.jar  into your project from the following path : <adt-bundle>/sdk/platforms/<android-17/18>/
  • You can import the jars in you project by:
    • Right-click Project -> Properties -> Java Build Path -> Libraries ->Add External JAR's

Code Snippet:

    package com.uiautomator.tests;
    
    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 FirstTest extends UiAutomatorTestCase{
     
     public void testFirst() throws UiObjectNotFoundException {
                    getUiDevice().pressHome();
      UiObject obj = new UiObject(new UiSelector().text("ButtonText"));
      obj.clickAndWaitForNewWindow();
     }
    }
    

Execution of tests:

The execution of the tests is an easy four step process:

Step 1:
Create the configuration file to build the jar file:
<android-sdk>/tools/android create uitest-project -n <name> -t 1 -p <path>
The is the name of the project that contains your uiautomator test source files, and the is the path to the corresponding project directory.

Step 2:
Create the JAR file. Go to the project directory where your build.xml is located. Then execute:
ant build

Step3:
Push the jar file to the device:
adb push ~/dev/workspace/Settings/bin/LaunchSettings.jar /data/local/tmp/

Step4:
Trigger the tests on the device after pushing the jar file to device:
adb shell uiautomator runtest LaunchSettings.jar -c com.my.LaunchSettings