Robotium Step by Step v.03

25
Android Automated Testing Using JUnit and Robotium

description

Robotium

Transcript of Robotium Step by Step v.03

Robotium

Android Automated Testing Using JUnit and RobotiumIntroductionWhat is JunitWhat is RobotiumTools usedCreating a New Test ProjectImporting Robotium jarCreating a new Test CaseCoding convention for JUnit ClassAdapting the Junit Test Case to test Android ApplicationsAccesing elementsSample AssertionsRunning Tests

What is JUnitJUnit is an open source framework designed for writing and running tests in Java programming language

It is used too write and run repeatable tests

What is RobotiumRobotium is another testing framework

It is designed for writing robust black box tests for Android Applications

We can test multiple activities using Robotium

The tests are robust

The user needs a minimum knowledge about the way the tested application works.Tools usedWe need to import Robotium framework into the project

It is used for a accessing graphical elements in a more simple and intuitive way

The jar file can be found athttps://code.google.com/p/robotium/downloads/list

We used robotium-solo-4.3.1.jarCreating a New Test ProjectFile->New->Other->Android Test Project.Select the project to be Tested

6Importing Robotium jarMain steps:1. Import robotium-solo- 4.3.1.jar in the following way:

TestProjectName -> right click-> Properties -> Java Builder Path -> Add External Jar

2. Search for the download location of robotium-solo-4.3.1.jar

3. On Order and Export tab, check the box corresponding to our imported JarImporting Robotium jar

Creating a new Test ClassAfter creating the test project, in the existing empty package, we need to have a new Junit Test Case.

After the class is generated, it must be changed to work with Android Projects.

Creating a New Test ClassSteps:

Define a subclass of TestCase

Override the setUp() method to initialize objects under test

Override the tearDown() method to release objects under test

Define one or more public testAAA() methods that perform a series of commands and assert expected results

Coding convention for JUnit ClassName of the test class must end with Test

Name of the method must begin with test

Return type of a test method must be void

Test method must not throw any exception

Test method must have any parameterInitial project code

This code will be modified to work for an Android Application. The changes that must be made are displayed in the next slidepackage com.rinf.mobile.rotemps.test;

import junit.framework.TestCase;

public class ActivityTesting extends TestCase {}Adapting the Junit Test Case to test Android Applicationspublic class ActivityTesting extendsActivityInstrumentationTestCase2 {

Solo solo;

public ActivityTesting() {super(MainActivity.class);}

protected void setUp() throws Exception {solo = new Solo(getInstrumentation(), getActivity());}

protected void tearDown() throws Exception {solo.finishOpenedActivities();}}Accesing elementsClick a Button

Radio Button

Check Box

Complete EditTextsolo.clickOnButton("Yes"); // the text displayed on the buttonsolo.clickOnRadioButton(0); // 0 indicates the first element of type Radio // Button existing in the layoutsolo.clickOnCheckBox(0);solo.enterText(0, "text"); // text is actually the string that will //be offered as input for the Edit TextAccesing elementsThis is a way in which we can access a spinner and select one of its items (in this case the 3rd one)

Check that a message was displayed in a ToastView view1 = solo.getView(Spinner.class, 0);solo.clickOnView(view1);solo.scrollToTop(); // I put this in here so that it always keeps the list at // start select the 3rd item in the spinnersolo.clickOnView(solo.getView(TextView.class, 2)); solo.waitForText("Please insert all required Information")Accesing elementsThere are 2 ways of accessing an elementBy id

By position

For a Button we have the option of accessing it by name instead of positionEditText et = (EditText) solo.getView(com.rinf.mobile.rotemps.R.id.etName);solo.enterText(et, "text");solo.enterText(0, "text");solo.clickOnButton("Yes");Creating a new testWhen creating a new test, usually we add the adnotation @SmallTest

The test method must be named in the following way: testName().

If we dont put the keyword test before the actual name, it will not be recognized as a test and so it will be completely ignored. @SmallTestpublic void testFieldCompletion() {}Sample AssertionsAssertions are used to check conditions. Examples of Assertions:

assertEquals (boolean expected, boolean actual)Asserts that two booleans are equal

assertTrue(java.lang.Stringmessage, booleancondition Asserts that a condition is true.

assertEquals(java.lang.Stringmessage, Stringexpecteds, Stringactuals) Asserts that two Strings are equal. We can set a specific message for each test to have a small description of the condition to be checked

For complete list see http://junit.sourceforge.net/javadoc/org/junit/Assert.html

Running TestsRight click on Project Name -> Run As -> Android JUnit Test

If the test conditions are fulfilled, the JUnit box has the following appearance:

Running TestsFor a failed test we have the possibility of tracing which condition failed

Complete Example of a Test ProjectRequirements for a Test Project

Test the page Send CV Complete all the fieldsPress the Send buttonCheck if a specific text is displayed

The last requirement checks the content provider.

The test passes if information from the input corresponds to the information displayed on the second paceComplete Example of a Test Projectpublic class SendCvClassTesting extendsActivityInstrumentationTestCase2 {Solo solo;EditText nameET;

public SendCvClassTesting() {super(SendCV.class);}

protected void setUp() throws Exception {solo = new Solo(getInstrumentation(), getActivity());nameET = (EditText) solo.getView(com.rinf.mobile.rotemps.R.id.etName);}

protected void tearDown() throws Exception {solo.finishOpenedActivities();}Complete Example of a Test Project @SmallTestpublic void testFieldCompletion() {solo.enterText(nameET, "text");solo.enterText(1, " my address");solo.enterText(2, "this is my email");solo.enterText(3, "0179");solo.clickOnCheckBox(0);

solo.clickOnButton("Send");solo.clickOnButton("Yes");assertTrue("Could not find message list!", solo.searchText("text"));}Questions ?Useful online resourceshttp://robotiumsolo.blogspot.ro/search/label/Toggle%20Buttonhttp://www.slideshare.net/mobilemarch/robotium-tutorialhttp://blog.3pillarglobal.com/mobile-testing-automated-testing-android-robotiumhttp://www.qualitytesting.info/profiles/blog/show?id=2064344%3ABlogPost%3A354479