Automated Testing

Selenium WebDriver – Using AdvancedUserInteractions API

The Advanced User Interactions API is more comprehensive API for describing actions a user can perform on a web page such as drag and drop or clicking multiple elements while holding down the Control key.

Actions vMyActions = new Actions(vDriver);
      vMyActions keyDown(Keys.CONTROL)
        .click(someElement)
        .click(someOtherElement)
        .keyUp(Keys.CONTROL);

Then get the action:

   Action vBindAction = vMyActions.build();

And execute it:

   vBindAction.perform();

  • It supports single and composite actions for Mouse and Keyboards
  • It supports most of common browsers including mobile browsers

source

Following are my reusable custom actions, will add more as needed šŸ™‚

 1. MouseMove
Move mouse over an element, usually to get hover

Locatable vElement = (Locatable) vDriver.findElement(by);
Mouse vMouse = ((HasInputDevices) vDriver).getMouse();
vMouse.mouseMove(vElement.getCoordinates());

2. Drag and drop
I need to drag and element on some other element,

vFirtElement = vDriver.findElement(By.id(ā€œid));
vContaingerElement = vDriver.findElement(By.id(ā€œcontainerid));
new Actions(vDriver). dragAndDrop(vFirtElement, vContaingerElement).build().perform();

3. Drag and Drop By
I need to drag and element from one place to other,

vElementToDrag = vDriver.findElement(By.id(ā€œidā€));
new Actions(vDriver).dragAndDropBy(vElementToDrag, 120, 1100).build().perform();

4. Select multiple elements
If I need to check/select multiple list items,

List vList = vDriver.findElements(By .cssSelector(ā€œ.class));
Actions vClickMultiple = new Actions(vDriver);
 vClickMultiple.clickAndHold(vList.get(1))
        .clickAndHold(vList.get(2))
        .click();
Action vBindActions = vClickMultiple.build();
 vBindActions.perform();

5. Slide
we can use dragAndDropBy to emulate sliding event

vElementToSlide = vDriver.findElement(By .className(ā€œclassā€));
new Actions(vDriver).dragAndDropBy(vElementToSlide, 120, 0).build().perform();

How to analyse Memory Availability of Android Testing Project

Both approaches worked for me, please using any of your choice,

Getting Runtime Memory using Runtime

static void checkMemory() {

double vTotalMem = Double.valueOf(Runtime.getRuntime().totalMemory());
double vMaxMem = Double.valueOf(Runtime.getRuntime().maxMemory());
double vAvailableMem = Double
.valueOf(Runtime.getRuntime().freeMemory());
print(“Runtime Memory info: TotalMem=”
+ Math.round(vTotalMem / 1048576) + “mb. MaxMemory”
+ Math.round(vMaxMem / 1048576) + ” mb. AvailableMemory=”
+ Math.round(vAvailableMem / 1048576) + ” mb.”);

}
Getting Memory using ActivityManager 

static void checkMemory() {
              ((ActivityManager)solo.getCurrentActivity()
                        .getSystemService(“activity”)).getMemoryInfo(mi);
              print(“Kernal Memory info: AvailableMemory=”+mi.
                         availMem/1048576+“mb IsMemLow=”+mi.lowMemory);
        if(mi.lowMemory) print(“memory is increasing”);
              else print(“Memory condition is fine”);
 
}

Get number of views on Android screen using Robotium

Please call the respective function to get its number on screen,

ImageViews:

       static int getNoOfImageViews() {
return solo.getCurrentViews(ImageView.class).size();
}
ImageButtons:
static int getNoOfImageButtons() {
return  solo.getCurrentViews(ImageButton.class).size();
}
RadioButtons:
static int getNoOfRadioButtons() {
return  solo.getCurrentViews(RadioButton.class).size();
}
RadioGroup:
static int getNoOfRadioGroup() {
return  solo.getCurrentViews(RadioGroup.class).size();
}
CheckBox:
static int getNoOfCheckbox() {
return  solo.getCurrentViews(CheckBox.class).size();
}
ListView:
static int getNoOfListViews() {
return  solo.getCurrentViews(ListView.class).size();
}
TextViews in a ListView:
static int getNoOfTextViewsInListView() {
return  solo.getCurrentViews(TextView.class,
solo.getCurrentViews(ListView.class).get(0)).size();
}
ImageViews in a ListView:
static int getNoOfImageViewsInListView() {
return  solo.getCurrentViews(ImageView.class,
solo.getCurrentViews(ListView.class).get(0)).size();
}
GridView:
static int getNoOfGridViews() {
return  solo.getCurrentViews(GridView.class).size();
}
ImageViews in a GridView:
static int getNoOfImageViewsInGridView() {
return  solo.getCurrentViews(GridView.class,
solo.getCurrentViews(GridView.class).get(0)).size();
}
ToggleButton:
static int getNoOfToggleButtons() {
return  solo.getCurrentViews(ToggleButton.class).size();
}
Button:
static int getNoOfButtons() {
return  solo.getCurrentViews(Button.class).size();
}
TextViews:
static int getNoOfTextViews() {
return  solo.getCurrentViews(TextView.class).size();

}

How to get all ImageButtons on screen using Robotium

static void getAllImageButtons() {
ArrayList allImageButton = solo
.getCurrentViews(ImageButton.class);
print(“Total ImageButtons:” + allImageButton.size());
for (ImageView vImageButton : allImageButton) {
if (vImageButton.getVisibility() == View.VISIBLE) {
print(“Image Button ID: “ + vImageButton.getId() + “Tag: “
+ vImageButton.getTag().toString() + ” Visibility:”
+ vImageButton.getVisibility() + “View String: “
+ vImageButton.toString());
}
}

}

How to get all ImageViews on screen using Robotium

static void getAllImageViews() {
ArrayList allImageViews = solo
.getCurrentViews(ImageView.class);
print(“Total ImageViews:” + allImageViews.size());
for (ImageView vImageView : allImageViews) {
if (vImageView.getVisibility() == View.VISIBLE) {
print(“Image ID: “
+ vImageView.getId()
+ “Tag: “
+ (vImageView.getTag() != null ? vImageView.getTag()
.toString() : “null”) + ” Visibility:”
+ vImageView.getVisibility() + ” View String :”
+ vImageView.toString());
}
}

}

How to get all ToggleButtons on screen using Robotium

static void printAllToggleButtons() {
ArrayList allToggleButtons = solo.getCurrentViews(ToggleButton.class);
print(“Total ToggleButtons:” + allToggleButtons.size());
for (ToggleButton vToggleButton : allToggleButtons) {
if (vToggleButton.getVisibility() == View.VISIBLE) {
print(“Button ID: “ + vToggleButton.getId() + ” Tag :”
+ vToggleButton.getTag().toString() + ” Value:”
+ vToggleButton.getText().toString() + ” Visibility:”
+ vToggleButton.getVisibility() + ” View String :”
+ vToggleButton.toString());
}
}

}

How to get all Buttons on screen using Robotium

static void printAllButtons() {
ArrayList
print(“Total Buttons:” + allButtons.size());
for (Button vButton : allButtons) {
if (vButton.getVisibility() == View.VISIBLE) {
print(“Button ID: “
+ vButton.getId()
+ ” Tag:”
+ (vButton.getTag() != null ? vButton.getTag()
.toString() : “null”) + ” Value:”
+ vButton.getText().toString() + ” Visibility:”
+ vButton.getVisibility() + ” View String:”
+ vButton.toString());
}
}

}

How to get all TextViews on screen using Robotium

static void printAllTextViews() {
vTextViewsList = null;
vTextViewsList = solo.getCurrentViews(TextView.class);
print(“Total TextViews:” + vTextViewsList.size());
vIndex = 0;
for (TextView vTextView : vTextViewsList) {
if (vTextView.getVisibility() == View.VISIBLE) {
print(“TextView ID: “
+ vTextView.getId()
+ “Index: “
+ vIndex
+ ” Tag: “
+ (vTextView.getTag() != null ? vTextView.getTag()
.toString() : “null”) + ” Value: “
+ vTextView.getText().toString() + ” Visibility: “
+ vTextView.getVisibility() + ” View String: “
+ vTextView.toString());
vIndex++;
}
}

}

How to Get all Views on screen using Robotium

static void printAllViews() {
ArrayList allViews = solo.getCurrentViews();
print(“Total Views:” + allViews.size());
for (View vView : allViews) {
if (vView.getVisibility() == View.VISIBLE) {
print(“View : “ + vView.toString() + “View ID: “
+ vView.getId() + ” Value:”
+ vView.getClass().getName().toString()
+ ” Visibility:” + vView.getVisibility());
}
}

}

Dealing with common Robotium Issues

Common Robotium Issues
When working with Robotium one may encounter issues that are due to the SDK, Eclipse or how Robotium works.  

The intent of this tutorial is to explain and show solutions to  common issues that new users experience.

1. NullPointerException when creating a test project for the first time

This problem comes especially with novice, when they for the first time create a new workspace and start working with Robotium.

If we create a new workspace for this tutorial, when creating the Test Project, Eclipse will show error ‘java.lang.NullPointerException’, because it is a new workspace and eclipse is not able to get selected Android resources for first test project.


We can solve this issue by following any of the suggestions below:

1. Create another TestProject & eclipse will automatically get selected resources(for the new one) & not show error for second created Test Project(we can use the second one for our work)

OR

2. First create an Android Project (we can also use Android Sample application bundled with Android SDK), run it, then create Test Project will not show error

2.Java.lan.NoClassDefFoundError: com.jayway.android.robotium.solo.solo

In the latest Android SDK versions (17 or above)  java.lang.NoClassDefFoundError:
com.jayway.android.robotium.solo.Solo error is shown if the Robotium jar is not exported. To fix the issue, after adding the Robotium jar go to the “Order & Export” tab and click the check-box besides the Robotium Jar and then click “OK”. Please see the screenshots below.



3. Robotium does not work with all the activities

Important to know that Robotium runs in the same process as the application under test. Due to this Robotium only works with the activities and views within the defined package (see below).  


In the AndroidManifest.xml we use the following code to describe which application to test:


android:targetPackage=”com.app.testingapp”
android:name=”android.test.InstrumentationTestRunner” />

ā€œcom.app.testingappā€ is the package name of the application under test. Robotium is locked to this package.

4. Unstable test case

Its important to continuously use the waitFor methods, especially if new windows open or loading screens are shown. The waitFor methods tell Robotium to wait for a condition to happen before the execution continues.

Robotium contains the following wait methods:

waitForActivity(String name)
         Waits for the given Activity.

waitForActivity(String name, int timeout)
         Waits for the given Activity.

waitForDialogToClose(long timeout)
         Waits for a Dialog to close.

waitForFragmentById(int id)
         Waits for a Fragment with a given id to appear.

waitForFragmentById(int id, int timeout)
         Waits for a Fragment with a given id to appear.

waitForFragmentByTag(String tag)
         Waits for a Fragment with a given tag to appear.

waitForFragmentByTag(String tag, int timeout)
         Waits for a Fragment with a given tag to appear.

waitForLogMessage(String logMessage)
         Waits for a log message to appear.

waitForLogMessage(String logMessage, int timeout)
         Waits for a log message to appear.
waitForText(String text)
         Waits for a text to be shown.

waitForText(String text, int minimumNumberOfMatches, long timeout)
         Waits for a text to be shown.

waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll)
         Waits for a text to be shown.

waitForText(String text, int minimumNumberOfMatches, long timeout, boolean scroll, boolean onlyVisible)
         Waits for a text to be shown.

waitForView(Class viewClass)
         Waits for a View of a certain class to be shown.

waitForView(Class viewClass, int minimumNumberOfMatches, int timeout)
         Waits for a View of a certain class to be shown.

waitForView(Class viewClass, int minimumNumberOfMatches, int timeout, boolean scroll)
         Waits for a View of a certain class to be shown.

waitForView(android.view.View view)
         Waits for a given View to be shown.

waitForView(android.view.View view, int timeout, boolean scroll)
         Waits for a given View to be shown.

5. Hanging/Freezing test cases

During test execution we open multiple activities, which stay alive depending upon the free memory available.  Therefore use  solo.finishOpenedActivities() in your tearDown method, it will close all the opened activities and free resources for the following test executions.

@AfterClass
   public void tearDown() throws Exception    
   {    
       solo.finishOpenedActivities();
   }

6. Problems when resigning APK

Use resign tool to re-sign an APK, observe that tool doesn’t work with Java 7(Java 1.7).

Manually re-sign APK
To manually re-sing APK:s follow the below steps:

* Un-zip the APK file
* Delete the META-¬INF folder
* Re‐zip the APK file

       It will zip file as “applicationName.apk.zip” , rename it to: “applicationName.apk”
* In Dos prompt/Terminal write following commands
> jarsigner -keystore ~/.android/debug.keystore -storepass android -keypass android applicationName.apk androiddebugkey
> zipalign 4 applicationName.apk TempApplicationName.apk
Then rename TempApplicationName.apk to applicationName.apk  if you want.