Showing posts with label robotium. Show all posts
Showing posts with label robotium. Show all posts
Friday, September 29, 2017
Using Robotium to test Android “Text Fields”
Using Robotium to test Android “Text Fields”
Robotium supports various functions to test Android single and multi line text fields.


The following are few APIs supported by Robotium to test the Text Fields,
- searchEditText(String text)
- clickOnEditText(int index)
- clearEditText(int index)
- typeText(int index, String text)
- enterText(int index, String text)
Test case for verifying the Text Fields :
Steps :- Wait for the activity to launch
- Enter a text in the Text Field
- Verify the text entered in the Text Field
- Clear the Text Entered in the Text Field
- Enter a new Test again and Verify the text entered.
1: public void testEditField() throws Exception { 2: solo.waitForActivity("MainActivity"); 3: 4: solo.clickOnEditText(0); 5: String strInput = "Sample code 1"; 6: solo.enterText(0, strInput); 7: boolean actual = solo.searchEditText(strInput); 8: assertEquals("text entered is not matching",true, actual); 9: 10: solo.clearEditText(0); 11: strInput = "Sample code 2"; 12: solo.typeText(0, strInput); 13: actual = solo.searchEditText(strInput); 14: assertEquals("text entered is not matching",true, actual); 15: }Explanation:
- solo.waitforActivity( ), is to wait for the activity to launch
- Enter Text in the Text Field : to enter a text in the Text filed, both �solo.enterText()� and �solo.typeText()� can be used.
- in �enterText(int index, String text)�, index is the index of the Text Field.
- if multiple Text Fields exists, first one will be assigned an index of 0, second one will get an index 1 and so on.. index increases from Top to Bottom or Left to Right
- Verify the Text Entered : to verify the text contained in the Text Filed, �solo.searchEditText()� can be used. it returns True, if pattern matches otherwise return False
- Clear the Text in the Text Field : to clear the Text exist in the Text Field, �solo.clearEditText()� can be used.
- �assertEquals()�, can be used to assert True/False
download file now
Tuesday, September 26, 2017
Use Robotium to test Android Checkbox
Use Robotium to test Android Checkbox

Check Boxes in Android can be tested using Robotium. The following are different functions supported by Robotium to check the Checkbox functionality.
- getCurrentCheckBoxes()
- isCheckBoxChecked(int index)
- isCheckBoxChecked(String text)
- clickOnCheckBox(int index)
The Activity contains three checkboxes, let us write some sample code to test them.
Test case for Android Check Boxes :
Steps :- Click on the Check box �Android�
- Click on the Check box �iPhone�
- Verify the Status of check boxes
- Unselect the Checkboxes �iPhone� and �Android�
- Verify the Status again
public void testCheckBoxes() throws Exception {
solo.waitForActivity("MainActivity"); //click on Image Buttonsolo.clickOnImageButton(0);
//look for checkbox text actual = solo.searchText("Windows"); assertEquals("Windows text not found",true, actual); //select checkbox Androidsolo.clickOnCheckBox(0);
//select checkbox iPhonesolo.clickOnCheckBox(2);
//check the Status of checkbox Android boolean actual1 = solo.isCheckBoxChecked(0); assertEquals("Android is not Selected",true, actual1); //check the Status of checkbox iPhone boolean actual2 = solo.isCheckBoxChecked(2); assertEquals("iPhone is not Selected",true, actual2); // unselect Android and iPhonesolo.clickOnCheckBox(0);
solo.clickOnCheckBox(2);
//check the status againactual1 = solo.isCheckBoxChecked(0);
assertEquals("Android OS is Selected",false, actual);actual2 = solo.isCheckBoxChecked(2);
assertEquals("iPhone is Selected",flase, actual2);}
- Select/Unselect Checkbox : �solo.clickOnCheckBox()� can be for both Select and Unselect checkbox. First time, when used this function, it will select the option and second time when performed the same action, it will unselect the Checkbox.
- Verify the Status of Checkboxes : �solo.isCheckBoxChecked()� function is used to verify the status of Checkbox. function returns �true�, if it is selected and �false� otherwise.
- Please note that, in the assert statements, while verifying the status of unselected check boxes, we used the expected result as �false�.
download file now
Friday, September 15, 2017
Using Robotium to test Android Toggle Buttons
Using Robotium to test Android Toggle Buttons
Robotium supports various APIs to test Android Toggle Button functionality

the following are some of the APIs supported by Robotium to test the Toggle Buttons
Let us write a test case for the Example project shown on the Right side, which has one toggle button
the following are some of the APIs supported by Robotium to test the Toggle Buttons
- searchToggleButton(String text)
- clickOnToggleButton(String name)
- isToggleButtonChecked(int index)
- isToggleButtonChecked(String text)
Test case for testing Toggle Button :
Steps:- Search for the Toggle Button
- Assert True/False based on the search result
- Click on the Toggle Button
- Check if Toggle Button status is changed or not?
- Click on the Toggle Button again
- Check whether the Toggle Button status is changed or not?
1: public void testToggleButton() throws Exception {
2: solo.waitForActivity("MainActivity");
3: 4: //search for toggle button
5: actual = solo.searchToggleButton("OFF");
6: assertEquals("Toggle Button not found",true, actual);
7: 8: //Turn ON toggle button
9: solo.clickOnToggleButton("OFF");
10: actual = solo.isToggleButtonChecked(0);11: assertEquals("Toggle Button is OFF",true, actual);
12: 13: //Turn OFF toggle button
14: solo.clickOnToggleButton("ON");
15: actual = solo.isToggleButtonChecked(0);16: assertEquals("Toggle Button is ON",false, actual);
17: 18: }- Search for Toggle Button : �solo.searchToggleButton()� is used to search for toggle buttons in the current activity
- the functions returns �true� if the Toggle Button with the given string is found otherwise return �false�
- Click on the Toggle Button : �solo.clickOnToggleButton()� is used to click on Toggle Button with the given string
- on enabling the Toggle Button, it�s text is changed from �OFF� to �ON�. hence, second time we modified the search pattern to �ON�
- Check the Status of Toggle Button : �isToggleButtonChecked()� is used to check the status of Toggle Button
- argument passed can be a String or Index
- returns true if the toggle button is checked, otherwise, return false.
download file now
Wednesday, September 6, 2017
Getting Started with Robotium
Getting Started with Robotium
Just before getting started, let us discuss few things about Robotium.
Robotium can be used to test applications where source code is available and applications where only APK file is available.
In this example, let us discuss about the case when source code is available. I will discuss the other scenario in a separate post. let us start with setting up test environment first,
In this example, let us discuss about the case when source code is available. I will discuss the other scenario in a separate post. let us start with setting up test environment first,
Requirements :
- Eclipse for building the Test project
- ADT (Android Development Kit)
- Android SDK (Software Development Kit)
- JDK (Java Development Kit)
- Latest Robotium Jar file
Prerequisites for Creating Test project :
- Install JDK, Eclipse, SDK and ADT to your system
- After installation, set proper path to Environment variables JAVA_HOME and ANDROID_HOME and add the SDK and JDK installation path to environment variable PATH.
- Download the Latest Robotium jar file from http://code.google.com/p/robotium/downloads/list
Create a Test project in Eclipse:
- Create the Test project by "File --> New --> Project --> Android --> Android Test Project", the window will open
- Enter the Project Name, click on Next
- if project source code is already available/imported in eclipse, in the "Test Target" field, choose An existing Android project and select from the list of Projects, click on Next
- in the "Build Target", select the application development platform, suppose, if application is developed on Android 2.2, choose API level as 8 and so on..
- click on Finish, a new test project will be created in the Eclipse.
Add the Robotium jar file to the Project
Right click on your project, --> Build Path --> Configure Build Path --> select "Libraries" tab
Then select "Add External Jars" -> select the Robotium Jar file downloaded
go to "Order and Export" tab, select the check box next to the Robotium jar file.
Create a Test case :
- Create New class file:
- select Project folder --> "src" directory --> select package, Right click on it, select "New --> Class", enter the class name and click on Finish.
- extend your class to ActivityInstrumentationTestCase2
- when eclipse shows error, click on import statement, which will import android.test.ActivityInstrumentationTestCase2
- copy the following code to your project and do modifications when required
1: public class <your class> extends ActivityInstrumentationTestCase2{ //no need to copy this line
2: private Solo solo;
3: // replace the "SampleTest()" with your class name ex: MyTest()
4: // replace "MainActivity.class" with your application projects starting activity class name
5: public SampleTest() {
6: super(MainActivity.class);
7: }8: //setUp() is run before a test case is started. This is where the solo object is created.
9: @Override10: public void setUp() throws Exception {
11: solo = new Solo(getInstrumentation(), getActivity());
12: }13: //tearDown() is run after a test case has finished.
14: //finishOpenedActivities() will finish all the activities that have been opened during the test execution.
15: @Override16: public void tearDown() throws Exception {
17: solo.finishOpenedActivities(); 18: }19: // your test case starts here...
20: // Test case name starts with keyword "test"followed by your Test case name in Capital Letter
21: //here, "MyFirstTestcase" is the test case name, change this with your desired name .
22: public void testMyFirstTestcase() throws Exception {
23: // write your test code
24: } 25: } //no need to copy this line
Now you can write your Test case in the test block
Next : How to Run the Test? -- see the Post
download file now
Tuesday, September 5, 2017
Use Robotium to test Android Image Buttons
Use Robotium to test Android Image Buttons
As it is common that most of the Android developers will use Image Buttons instead Text buttons, it is important criteria to test Image buttons.

Robotium has support for Image Buttons. The following APIs can be used to test Image Button functionalities,
Explanation :

Robotium has support for Image Buttons. The following APIs can be used to test Image Button functionalities,
- getImageButton(int index)
- clickOnImageButton(int index)
Test case for Image Buttons :
Steps :- Click on the Image Button �Alarm�
- go back
- Click on the Image Button �Music�
1: public void testCheckBoxes() throws Exception { 2: solo.waitForActivity("AlarmPlusActivity"); 3: 4: //click on Alarm Image Button 5: solo.clickOnImageButton(0); 6: assertTrue(solo.searchText("Add alarm")); //this text appears in the next activity 7: //go to previous screen 8: solo.goBack(); 9: //click on Gallery button 10: solo.clickOnImageButton(1); 11: 12: }Explanation :
- Click on Image Button : �solo.clickOnImageButton()�, is used to click on the Image button, which accepts only index number as argument.
- Return to Previous screen, �solo.goBack()�, is used to go to Previous screen.
- index of the Image buttons increase from Left to Right. Alarm button has 0, Gallery button has 1 and Home button with index
download file now
Monday, September 4, 2017
Use Robotium to test Android Progress bar Seek bar and Rating bar
Use Robotium to test Android Progress bar Seek bar and Rating bar
Robotium Supports various functions to support Progress bar, Seek bar and Rating bar. it uses the following functions to support these views.
Let us see one example Test case to the sample project shown on the Right side,
Explanation :

- getCurrentProgressBars()
- setProgressBar(int index, int progress)
- setProgressBar(android.widget.ProgressBar progressBar, int progress)
Let us see one example Test case to the sample project shown on the Right side,
Test case for Progress Bar :
Steps:- Set the Progress bar status from Zero to High
- Set the Seek bar position from Minimum to Maximum
- Set the Rating Bar from 1 start to 5 stars
1: public void testProgressBar() throws Exception { 2: 3: solo.waitForActivity("MainActivity"); 4: // set the Progree Bar Status 5: solo.setProgressBar(0, 10); 6: solo.setProgressBar(0, 20); 7: solo.setProgressBar(0, 30); 8: solo.setProgressBar(0, 40); 9: solo.setProgressBar(0, 50); 10: solo.setProgressBar(0, 10); 11: // set the Seek Bar Position 12: solo.setProgressBar(1, 10); 13: solo.setProgressBar(1, 20); 14: solo.setProgressBar(1, 30); 15: solo.setProgressBar(1, 40); 16: solo.setProgressBar(1, 50); 17: solo.setProgressBar(1, 10); 18: // set the Rating Bar status 19: solo.setProgressBar(2, 2); 20: solo.setProgressBar(2, 3); 21: solo.setProgressBar(2, 5); 22: solo.setProgressBar(2, 7); 23: solo.setProgressBar(2, 9); 24: solo.setProgressBar(2, 10); 25: }Explanation :
- Set the Progress bar and Seek bar position : �solo.setProgressBar()� is used to set the position of status bar, the position has to be specified in Integer number say 10,20,30,40 so on.. i hope max value is 100. just, try with different numbers and see the change in position.
- Set the Rating bar : �solo.setProgressBar()�, is used to set the Rating bar. use the numeric value from 1 to 10.
- Since there are 3 similar items(Progress bar, Seek bar and Rating bar), index will starts from 0 to 2.
download file now
Subscribe to:
Posts (Atom)