Automatic Testing of Robotium for Android – Part II

Tram Ho

Today I continue to talk about Robotium

3. Robotium APIs

3.1. Purpose

Introduction to Solo class and information about APIs in Robotium framework, international knowledge about using Robotium – Resource ID test case, Test case evaluation

Solo Class Solo is a main class provided in the Robotium framework, including APIs (Application Program Interface) that support the writing of test cases in projects. Robotium is used to link the testing classes in Android

For example, ActivityInstrumentationTestCase2 and SingleLaunchActivityTestCase.

Solo class has 2 initialization functions:

  • Solo ( instrumentation): initialization function takes 1 instrumentation parameter from the package
  • Solo ( instrumentation, activity): initialization function takes two instrumentation and activity parameters

APIs call

Application program interface (API) is a collection of common tasks, protocols and tools to build software applications. The API basically provides a way to interact with software components. In addition, the API is used when programming graphical user interface components (GUIs).

There are APIs within the Robotium framework and they cover most of the features available in Android. The countIncreases method is based on user feedback and suggestions, which must be implemented if the method involves specific functions, those methods can be added as part of the Robotium framework. That countIncreases method will be used in similar projects.

Robotium development team analyzes requirements based on priority. If API support is stopped when running in project versions, then the Robotium test project may be having problems.

The methods of the Solo class can be found at the following links:

Source code Javadoc about the set of APIs at the following link:

3.2 Resource ID in Robotium

In Robotium there is no need to import Application Under Test (AUT) resources into the test project to use the Resource ID.

The implementer can use the Resource ID without importing the resource. Just get the Resource ID by passing the display name and type into the getIdentifier () method, passing the ID into the getView () method for each of the corresponding display object types.

You can pass string parameters into the getView () method as follows:

Learn about Internationalization

Internationalization means making applications compatible with different languages ​​or environments (multi-language applications), using appropriate components and compiling.

For a multi-language support and testing application this should not use any language in the test case, instead use the strings in the file res / values ​​/ strings.xml to define the meaning of these languages.

4. Support WEB in Robotium

4.1 Purpose

About how to access WebElements in Android and the web support methods in Robotium

** API settings **

Web support has been included in Robotium Framework since Robotium 4.0. Robotium fully supports complex applications. Below is a comparison of native application and hybrid application

Native ApplicationHybrid Application
– Depends on the platform– Multi-platform
– Running on software and hardware inside the device– Constructed using HTML5 and JavaScript and enclosed within a textual framework that provides access to native platform features
– It takes more effort to build applications on other platforms and takes time to learn– Saving development costs and time
– High performance– Lower performance

Some methods available in Robotium help to access web content:

  • searchText (String text)
  • scrollUp () / scrollDown ()
  • clickOnText ()
  • takeScreenshot ()
  • waitForText (String text)

Of the methods included to support the web, the By class is included as a parameter. It is an abstract class used as a bridge in the methods of the web. The above methods are used to select different WebElements based on attributes like ID or name.

The components used in web view are interpreted as WebElement, similar to WebDriver. The following table lists the methods within the By class:

className (String className)Get a WebElement by class name
cssSelector (String selectors)Get a WebElement according to the css selector
getValue ()Returns the value
id (String id)Get a WebElement by id
name (String name)Get a WebElement by name
tagName (String tagName)Get a WebElement by tag name
textContent (String textContent)Get a WebElement according to the text content
xpath (String xpath)Get a xpath WebElement

Some important methods in Robotium framework for connecting between web and Android:

  • clickOnWebElement (By by): Click on WebElement that matches the By class object passed in
  • waitForWebElement (By by): Wait for WebElement to match By class object passed
  • getWebElement (By by, int index): Returns a WebElement that matches the By class object and the index passed.
  • enterTextInWebElement (By by, String text): Enter the text inside a WebElement that matches the By class object passed
  • typeTextInWebElement (By by): Enter text into a WebElement that matches the By class object. In this method the program enters the alphabetic characters on the keyboard
  • clearTextInWebElement (By by): Delete the text in a WebElement that matches the By class object
  • getCurrentWebElement (By by): Returns the ArrayList WebElement displayed in the current web view that matches the By class object passed.

WebView in Android:

It is possible to retrieve a WebView instance using the Solo class as follows:

WebView wb = solo.getCurrentViews(WebView.class).get(0);

Once Webview can add JavaScript code into the WebView as follows:


4.2 Example of Hybrid test

An example of a hybrid application, create a simple test project. This application provides 1 WebView and uploads to Google homepage

The code of class is as follows:

Adding the following code to the web_main.xml file is the layout of Activity WebViewActivity

Add permission to access the Internet

The steps above are just WebView application settings.

Now start writing test cases to access WebElement of Google pages

The above code enters “Robotium” into the google search box and clicks on the search button. The results appear as follows:

The screenshot above is saved to the / sdcard / Robotium-Screenshots / folder by the API. It requires permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml file

The above results are recorded by JUnit when running Android JUnit test project as follows:

The test case of SearchSearch Robotium took 66.106 seconds to complete the test

5. Compare with other Frameworks

This chapter compares Robotium with other test frameworks based on certain parameters. This will provide information to help select the appropriate Frameworks according to the needs of each project. In this section, you and I will compare Robotium with MonkeyRunner, Robolectric, UI Automator and Calabash.

5.1 MonkeyRunner

MonkeyRunner is a tool used to write Android emulator / device access programs from outside Android code.

The program takes screenshots of the Android user interface and sends it to the workstation for storage.

MonkeyRunner is an API, not a program.

Since MonkeyRunner is a Python module, you can do anything supported by Python. All you need to do is create a Python program and add MonkeyRunner!

See the difference between Robotium and MonkeyRunner in the following table:

ObjectSelect objects based on attributes like index, text / name, image and IDThe object selection is based on its position (x, y coordinates), which may change as the application develops.
WorkIt can only perform actions on test applicationsIt can be implemented into an entire device, meaning all existing applications.
VerificationBased on JUnitVerify based on screenshot
LanguagejavaPython script

There are several things in common:

  • They can be run on a virtual machine / device by sending specific commands and events from an API.
  • In the Android testing domain, different framwords are present for different needs. Because Robotium is primarily used for user interface testing, it does not support the following features of MonkeyRunner:
  1. Automated expansion
  2. Test many applications and device drivers

5.2 Robolectric

Robolectric is a test virtual machine, part of Android-enabled Frameworks that allows to run test cases directly on the Java Virtual Machine (JVM) with the help of JUnit Frameworks 4. The most important things about Robolectric are:

  • No need virtual machine / device support.
  • Robolectric contains Android objects that behave like those contained in the Android SDK.

See the difference between Robotium and Robolectric in the following table:

Virtual machine / deviceRobotium needs virtual machines or assistive devices to perform the testsRobolectric does not need any virtual machines / assistive devices to execute the tests. This is why it is much faster than Robotium
ServerIt needs some virtual or support equipment on the server to run the test cases; Otherwise, will not be addedIt can be easily configured on the server
DevelopmentIt is used for testing on an actual Android device and the APIs are not simulated by Robolectric.It speeds up the test development cycle than Robotium
Evaluation equipmentIt uses JUnit 3 testingIt uses JUnit 4 testing

5.3 UI Automator

UI Automator is a Java library that is used to initialize customized, UI test cases for an android application and it provides an automated executable tool to run the test cases.

Take a look at the differences between Robotium and UI Automator in the following table:

FeatureRobotiumUI Automator
Cross applicationcan not overcomeCan pass. For example, if the library application and can select any album, this can be achieved by using the Automation Tool UI. The library is another application package and selecting into an album inside the library is an application cross-activity.
API setRobotium has a huge API set, containing methods to enable pattern recognition, etc. Therefore, Robotium provides more control than UI Automator.UI Automator contains methods to enable pattern recognition, but that method performs a different function than Robotium.
API level supportLevel 4 and aboveSupport for devices with API level 16 (or higher) and not coming back to support older API levels; Therefore, there is no backward compatibility
Integrate with IDEIntegrates seamlessly with the Eclipse IDEIntegrating the UI with the IDE is cumbersome when you need to add the JUnit library using Android.jar and uiautomator.jar manually and build it using Ant.

| Web support | Full support for Web in applications | This feature is missing |

5.4 Calabash

Calabash is a cross-platform that allows you to write functional test cases automatically for mobile applications, supporting Android and iOS applications.

Take a look at the differences between Robotium and Calabash in the following table:

LanguageJavaLanguage is more flexible than Ruby, Java
ControlFrom the deviceControl from computer instead of device
SessionYou can set the device orientation to Landscape or PortraitIt is not possible to mimic a phone change to Landscape or Portrait

In brief

In this section, there was a comparison of Robotium with other test templates based on different factors and it was concluded that all molds were used according to different needs. No template is perfect, there are always some pros and cons related to it.

In the next section, we will examine the remote control feature and how to use it in Robotium.

There’s more , …….

References Automated Testing for Android [Zadgaonkar 2013-11-22] .pdf? fbclid = IwAR0JSTaqgDW4ShQEDgY9KKCSEulJ3cLYr4OkF50dPZ6M8OALYnVsJckYz_k

Share the news now

Source : Viblo