15+ Useful Robotium Code Snippets for Android Test Automation
Robotium is a powerful android test automation framework. It can be used to create robust apps using unit, functional & automatic black box UI test cases.
When I first looked at Robotium, it sounded almost like Selenium (for web application). Though the names are similar, they are not related. Robotium is developed by different developer group.
I have tried several android testing libraries (http://www.javacodegeeks.com/2011/06/android-dependency-injection-testing.html ) and was not able to fully accomplish my app testing requirements.
After few months of coding in Robotium, I realized that its a good candidate to be a part of android and jenkins continuous integration environment – http://www.javacodegeeks.com/2012/09/android-and-jenkins-continuous.html
In past few weeks, I started exploring more in depth features of Robotium and started liking it. I have been a selenium (web driver) lover and had collected a handful of code snippets for web driver (Proved handy for cheating code :) ) . Therefore I decided to do the same for Robotium as well. I used it for a real app testing and collected a few commonly used code snippets that may prove helpful for other beginners including me.
Below are some commonly used code snippets for testing using robotium framework.
Take a Screenshot
Taking snapshot of any android screen is easy using takeScreenshot method.
solo.takeScreenshot()
The takeScreenshot() method saves the picture in “/sdcard/Robotium-Screenshots/” folder on device.
This will require write permission (android.permission.WRITE_EXTERNAL_STORAGE) in the AndroidManifest.xml of the application being tested.
Check If a Button Element Exists
Often its required to check if a button appears on the screen. We may want to do further action once the button is found. This can be achieved by searchButton method.
solo.searchButton("My Beautiful Button", true )
Notice that button text is provided as first parameter, and second parameter indicates that method will return button object that is visible. You may want to set it false if you want to check for not visible buttons too.
Running Same Test cases on Two Different Devices
Testing android apps on multiple devices is a common need. Though there is nothing special required from robotium side you can use a adb command option to run it on specific device.
The -s flag in adb command can be used to point to a specific device. When you want to run on two devices you can open two terminals and run the command pointing to two different devices. This will run the test cases in both devices in parallel.
Lets say We want to run two different test cases on 2 connected android devices. Below command should list out both devices
adb devices
Command output
List of devices attached SER1 device1 SER2 device2
then using the serial numbers shown in command output above we can run below command:
adb -s SER1 shell am instrument -w -e class com.test.Test1 com.test/android.test.InstrumentationTestRunner adb -s SERIALOFDEVICE2 shell am instrument -w -e class com.test.Test2 com.test/android.test.InstrumentationTestRunner
where
com.test.Test1 com.test.Test2
Are the test classes we want to run on each device.
How to Simulate a Long Press on Screen Robotium
Long press can be done using clickLongOnScreen method that accept two parameters for location (x,y) on the screen. Example
solo.clickLongOnScreen(10, 15);
If you are not sure about x,y coordinates on device screen below picture may be useful
How to Maintain the order of Robotium Test Cases
If you want to execute test cases in specific order than you can use test suites. Simple example is shown below.
public static final MyTest suite() { TestSuite testSt = new TestSuite(); testSt.addTest(new SampleTestCase("one")); testSt.addTest(new SampleTestCase("two")); return testSuite; }
Wait for activity to finish
Waiting for an activity to finish may be required in few test cases. You may want to perform test actions after an activity is finished. waitForActivity method can be used to accomplish this.
solo.waitForActivity(solo.getCurrentActivity().toString());
Wait for toast to apear
There is no direct method for wait for toast, however you can use a workaround by waiting for text (that is inside the toast) to appear
solo.waitForText("Some text inside toast");
Wait for view to disappear
There is no direct method to wait for a view to disappear, however you can use a workaround by waiting on condition for a field to become invisible. Lets say we have a text view. We can use below code snippet to wait on the textField to become invisible.
TextView textField = solo.getText("My Text Value", true); solo.waitForCondition(new Condition() { @Override public boolean isSatisfied() { return textField.getVisibility() == View.INVISIBLE; } }, 1000);
Notice that we have used a anonymous class implementation here. You can always create a class for the same in case you use this code snippet often.
Conditional Wait – just like above example we can specify custom conditions on the wait method that proves really helpful for custom programmable wait configuration. Below is the signature of wait for condition method. It also has a timeout field that can be your upper limit on wait time for condition.
solo.waitForCondition(Condition condition, int timeout);
Wait for fragment
Waiting for fragment is a supported method in robotium. Just need to call the waitForFragmentByTag method
solo.waitForFragmentByTag("MyTag");
Above code snippet waits for a fragment matching the specified tag (MyTag).
solo.waitForFragmentByTag("MyTag", 100);
Above code snippet will wait for a fragment up to 100ms and continue execution.
How to validate a text value in text view
TextView textV = solo.getText("My Expected Value"); // if textV is not null than may be we do not need next line. assertEquals("My Expected Value", textV.getText().toString());
How to Hide soft keyboard
Hiding soft keyboard is a common need and robotium has a method for that too.Simple calling hideSoftKeyboard method hides the soft keyboard.
solo.hideSoftKeyboard();
How to navigate back
Going back to previous screen is easy with robotium. Just use the back() method.
solo.goBack();
How to check sufficient memory is available on device
There is a dedicated method to validate if the memory available is not considered low by android system
solo.assertMemoryNotLow();
How to Clear the log file using robotium api
Clearing the log file using robotium clearLog method is really helpful specially when you have long running test cases and huge log files eating device memory.
solo.clearLog();
How to delete text from a editable text field
You can clear the text from EditText field using below code
solo.clearEditText(editText object);
How overwrite update/enter text in a editable text field
Below method call will overwrite the existing content of EditField
solo.enterText(editText, "new value on field");
How append text in a editable text field
There is no dedicated method for appending text to EditText field therefore you may need to do it by extracting old value and appending new value to it.
solo.enterText(editText, editText.getText() + "new value on field");
How to set date in a date picker object
We can use setDatePicker method for setting specific dates on app using robotium
solo.setDatePicker(datePicker, 2014, 1, 5)
Sets the date (April 1st 2014) in the specified DatePicker.
Summary
My application test cases were mostly focused on native android apps, therefore these code snippets are more useful for native apps. I am sure the hybrid applications may have similar feature too. Feel free to suggest more snippets and it can become a really handy resource.
I hope you found this post useful, don’t forget to share or bookmark this page.
If you enjoyed this, then subscribe to our newsletter to enjoy weekly updates and complimentary whitepapers! Also, check out JCG Academy for more advanced training!
References:
Official Website: http://code.google.com/p/robotium/
FAQs
I recommend you to browse thru the FAQs pages before posting a question on any forum:
- http://code.google.com/p/robotium/wiki/QuestionsAndAnswers
- http://stackoverflow.com/questions/tagged/robotium?sort=frequent&pageSize=15
Forums
Official Discussion forum to ask questions related to robotium:
- https://groups.google.com/forum/#!forum/robotium-developers
- Stackoverflow is also a good place to get answers about robotium related queries – http://stackoverflow.com/questions/tagged/robotium .