DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Top 5 Android Testing Frameworks With Code Examples

Top 5 Android Testing Frameworks With Code Examples

Find the best testing framework for your Android app with this analysis of five popular choices, complete with code examples.

Lingkai Shao user avatar by
Lingkai Shao
·
Oct. 15, 17 · Opinion
Like (8)
Save
Tweet
Share
12.56K Views

Join the DZone community and get the full member experience.

Join For Free

With the rollout of Android Oreo, Google’s mobile ecosystem continues to expand rapidly. It is evident that new mobile OEMs are emerging in every corner of the world, bringing in different screen sizes, ROM/firmware, chipsets, etc. The complexity of Android app testing has been growing all the time.

To cope with increasing testing workload, it has become a common practice to rely on a cloud-based device lab for scalable mobile app testing and automate scripted tests on a large scale of real devices for extensive QA. Though there are multiple choices on Android testing frameworks, it's critical to understand the basics and how each framework performs so the selected tool can help you meet your testing needs and ultimately achieve your business goals.

Here we are going to behold 5 most used Android testing frameworks and break down the basics and code examples of each. 

Appium

Appium is a mobile test automation framework (and tool) for native, hybrid, and web apps for iOS and Android. It uses JSONWireProtocol internally to interact with iOS and Android apps using Selenium’s WebDriver. It supports Android via uiautomator (API level 16 or higher) and Selendroid (API level lower than 16), iOS via UI Automation, and mobile web as Selenium driver for Android and iOS. This Appium beginner's guide will walk you through more details.

One of the biggest advantages of Appium is that you can write your Appium scripts in almost any programming language (e.g. Java, Objective-C, JavaScript, PHP, Ruby, Python or C#), freedom from having to select tools, compatibility across the most important platforms (Android and iOS), freedom from having to install and configure devices to test and more. Also if you are familiar with Selenium, then it’s easy for you to use Appium in mobile app testing. They use the same WebDriver and DesiredCapabilities is used in the same way. Configuring an application to run on Appium has a lot of similarities to Selenium.

Appium code example:

# wait for hello
 sleep(3)
 textFields = driver.find_elements_by_tag_name('textField')
 assertEqual(textFields[0].get_attribute("value"), "Hello")
 # click sign-in button
 driver.find_elements_by_name('Sign in')[0].click()
 # find the text fields again, and enter username and password
 textFields = driver.find_elements_by_tag_name('textField')
 textFields[0].send_keys("twitter_username")
 textFields[1].send_keys("passw0rd")
 # click the Login button (the first button in the view)
 driver.find_elements_by_tag_name('button')[0].click()
 # sleep
 sleep(3)
 # click the first button with name "Compose"
 driver.find_elements_by_name('Compose')[0].click()
 # type in the tweet message
 driver.find_elements_by_tag_name('textField')[0].send_keys(”#Bitbar is awesome!")
 # press the Send button
 driver.find_elements_by_name('Send')[0].click()
 # exit
 driver.quit()

Calabash

Calabash is a cross-platform test automation framework for Android and iOS native and hybrid applications. Calabash’s easy-to-understand syntax enables even non-technical people to create and execute automated acceptance tests for apps on both of these mobile platforms. Calabash’s tests are described in Cucumber and then converted to Robotium or Frank in runtime. It supports about 80 different natural language commands (controllers), and new controllers can be implemented in Ruby or Java. To learn more, this Calabash 101 ebook will give you an extensive understanding.

Calabash code example:

Feature: Login feature
 Scenario: As a valid user I can log into my app
 I wait for text "Hello"
 Then I press view with id "Sign in"
 Then I enter text "username" into "login_username"
 Then I enter text "password" into "login_password"
 Then I wait for activity "HomeTabActivity"
 Then I press view with id "menu_compose_tweet"
 Then I enter text "Bitbar" into field with id "edit"
 Then I press view with id "composer_post"

Espresso

Espresso is the latest Android test automation framework that got open-sourced by Google, making it available for developers and testers to hammer out their UIs. Espresso has an API that is small, predictable, easy to learn and built on top of the Android instrumentation framework. You can quickly write concise and reliable Android UI tests with it. It is supported on API level 8 (Froyo), 10 (Gingerbread), and 15 (Ice Cream Sandwich) and onwards.
It’s quite reliable, synchronizing with the UI thread and fast because there is no need for any sleeps (tests run the same millisecond when an app becomes idle). But, it does not have support for webviews as well.

Espresso code example:

public void testEspresso() {
 // Check if view with the text 'Hello.' is shown
 onView(withText("Hello.")).check(matches(isDisplayed()));
 // R class ID identifier for 'Sign in' - and click it
 onView(withId(getInstrumentation().getTargetContext().getResources()
 .getIdentifier("com.twitter.android:id/sign_in", null, null))).perform(click());
 // R class ID identifier for entering username
 onView(withId(getInstrumentation().getTargetContext().getResources()
 .getIdentifier("com.twitter.android:id/login_username", null, null))).perform((typeText("username")));
 // R class ID identifier for entering password
 onView(withId(getInstrumentation().getTargetContext().getResources()
 .getIdentifier("com.twitter.android:id/login_password", null, null))).perform((typeText("password")));
 // R class ID identifier for clicking log in
 onView(withId(getInstrumentation().getTargetContext().getResources()
 .getIdentifier("com.twitter.android:id/login_login", null, null))).perform(click());
 // Activate the text field to compose a tweet
 onView(withId(getInstrumentation().getTargetContext().getResources()
 .getIdentifier("com.twitter.android:id/menu_compose_tweet", null, null))).perform(click());
 // Type the tweet
 onView(withId(getInstrumentation().getTargetContext().getResources()
 .getIdentifier("com.twitter.android:id/edit", null, null))).perform((typeText(”#Bitbar")));
 // Tweeting!
 onView(withId(getInstrumentation().getTargetContext().getResources()
 .getIdentifier("com.twitter.android:id/composer_post", null, null))).perform(click());
 }

uiautomator

While Robotium is a good yet basic framework, uiautomator allows you to do more in testing Android apps and games. Google’s test framework allows you to test user interface (UI) of your native Android apps on one or more devices. Another advantage of uiautomator is that it runs JUnit test cases with special privileges, which means test cases can span across different processes. It also provides five different classes for developers to use:

com.android.uiautomator.core.UiCollection;
 com.android.uiautomator.core.UiDevice;
 com.android.uiautomator.core.UiObject;
 com.android.uiautomator.core.UiScrollable;
 com.android.uiautomator.core.UiSelector

Similar to its time of birth, it only works on Android devices with API level 16 or higher. Another downside of uiautomator is that it doesn’t support webview, with no way to directly access Android objects.

uiautomator code example:

// Public void for the operation
 public void testSignInAndTweet() throws Exception {
 // Starting application:
 getUiDevice().wakeUp(); // Press Home button to ensure we're on homescreen
 getUiDevice().pressHome(); // Select 'Apps' and click button
 new UiObject(new UiSelector().description("Apps")).click(); // Select 'Twitter' and click
 new UiObject(new UiSelector().text("Twitter")).click(); // Locate and select 'Sign in'
 UiSelector signIn = new UiSelector().text("Sign In"); // If button is available, click
 UiObject signInButton = new UiObject(signIn);
 if (signInButton.exists()) {
 signInButton.click(); // Set the username
 new UiObject(new
 UiSelector().className("android.widget.EditText").instance(0)).setText("username");
 new UiObject(new
 UiSelector().className("android.widget.EditText").instance(1)).setText("password");
 new UiObject(new UiSelector().className("android.widget.Button").
 text("Sign In").instance(0)).click(); // Wait Sign in progress window
 getUiDevice().waitForWindowUpdate(null, 2000); // Wait for main window
 getUiDevice().waitForWindowUpdate(null, 30000);
 }
 new UiObject(new UiSelector().description("New tweet")).click(); // Typing text for a tweet
 new UiObject(new UiSelector().className("android.widget.LinearLayout").instance(8)).
 setText("Awesome #Bitbar!"); // Tweeting!
 new UiObject(new UiSelector().text("Tweet")).click();

Robotium

Undoubtedly, Robotium was once the most widely used Android testing framework in the early days of the Android world. With a similarity with Selenium in Android, it makes testing API simpler.

Robotium is an open source library extending JUnit with plenty of useful methods for Android UI testing. It provides powerful and robust automatic black-box test cases for Android apps (native and hybrid) and web testing. With Robotium you can write function, system and acceptance test scenarios, and test applications where the source code is available.

Robotium code example:

// Public void for the operation
 public void testRecorded() throws Exception {
 // Wait for the text 'Hello!' to be shown for newbie
 if (solo.waitForText("Hello!")) {
 // R class ID identifier for 'Sign in' - and click it
 solo.clickOnView(solo.findViewById("com.twitter.android.R.id.sign_in"));
 // R class ID identifier for entering username
 solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.login_username"),"username");
 // R class ID identifier for entering password
 solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.login_password"),"password");
 // R class ID identifier for clicking log in
 solo.clickOnView(solo.findViewById("com.twitter.android.R.id.login_login"));
 // Wait until log in is done
 solo.waitForActivity("HomeTabActivity");
 }
 // Activate the text field to compose a tweet
 solo.clickOnView(solo.findViewById("com.twitter.android.R.id.menu_compose_tweet"));
 // Type the tweet
 solo.enterText((EditText) solo.findViewById("com.twitter.android.R.id.edit"), "Bitbar");
 // Tweeting!
 solo.clickOnView(solo.findViewById("com.twitter.android.R.id.composer_post"));
 }

Wrapping Up

Image title

Here we have listed the top 5 testing frameworks for your daily Android builds, creation, and correction. Certainly, each of them has its pros and cons, and it depends on your goal and how comfortable you are with each framework. In general, Appium and Calabash are good cross-platform frameworks in testing both your Android and iOS versions at the same time. Espresso is a good choice if you are purely testing Android apps. Therefore, think about your testing need - functional testing, compatibility testing, UI testing, etc. - and pick the right and best Android testing framework(s).

Android (robot) Framework mobile app Testing Test automation

Published at DZone with permission of Lingkai Shao. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • HTTP vs Messaging for Microservices Communications
  • Spring Boot, Quarkus, or Micronaut?
  • How To Handle Secrets in Docker
  • Fargate vs. Lambda: The Battle of the Future

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: