Mobile Test Automation

What is test automation

In software testing, test automation is the use of special software (separate from the software being tested) to control the execution of tests and the comparison of actual outcomes with predicted outcomes

Mobile Test Automation Frameworks

  • Appium
  • Robotium
  • Espresso

Appium

  • Appium is an open source cross-platform test automation framework to use with native, hybrid and mobile web apps.

 

  • It drives iOS and Android apps using the WebDriver JSON Wire Protocol. It’s a black-box testing tool, no need for the source code.

Dependencies

  • Most of these requirements are the same requirements as for app development on a specific platform. For example, to automate Android applications using one of our Android drivers, you'll need the Android SDK configured on your system.

Driver for platforms

  • The XCUITest Driver (for iOS apps) 
  • The UiAutomator2 Driver (for Android apps)

Sample Code

  1.  @Before
  2.   public void setUp() throws Exception {
  3.   File appDir = new File(System.getProperty("user.dir"), "../../../apps/TestApp/build/release-iphonesimulator");
  4.   File app = new File(appDir, "TestApp.app");
  5.   DesiredCapabilities capabilities = new DesiredCapabilities();
  6.   capabilities.setCapability("platformVersion", "9.3");
  7.   capabilities.setCapability("deviceName", "iPhone 6");
  8.   capabilities.setCapability("app", app.getAbsolutePath());
  9.   driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  10.   values = new ArrayList<Integer>();
  11.     }

Sample Code

  1.     @Test

  2.     public void testActive() throws Exception {

  3.         WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));

  4.         assertTrue(text.isDisplayed());

  5.         WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));

  6.         assertTrue(button.isDisplayed());

  7.     }

Sample Code

  1.     @Test

  2.     public void testClear() throws Exception {

  3.         WebElement text = 
    driver.findElement(By.xpath("//UIATextField[1]"));

  4.         text.sendKeys("12");

  5.         text.clear();

  6.  

  7.         assertEquals("", text.getText());

  8.     }

Sample Code

  1.     @Test

  2.     public void testLocation() throws Exception {

  3.         WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));

  4.  

  5.         Point location = button.getLocation();

  6.  

  7.         assertEquals(110, location.getX());

  8.         assertEquals(143, location.getY());

  9.     }

Sample Code

  1. @After
  2.     public void tearDown() throws Exception {
  3.         driver.quit();
  4.     }

Robotium

  • Robotium is an open source mobile testing automation framework for native and hybrid Android apps.

  • With it’s automatic black-box UI tests for apps: you just need the .apk, no source code is needed

 

  • You can also run gray-box tests if the source code is available.
  • It provides simple API to write UI automation scripts.

Dependencies

To use Robotium in your Android test project, you need to add a dependency to the latest Robotium release to your build file.

 

dependencies {
    // Unit testing dependencies
    androidTestCompile 'com.jayway.android.robotium:robotium:5.4.12'
}

Sample Code

  1. public void setUp() throws Exception {

  2.         solo = new Solo(getInstrumentation(), getActivity());

  3.     }

Sample Code

  1. public void testActivity() throws Exception {

  2. // check that we have the right activity

  3. solo.assertCurrentActivity("wrong activity", SimpleActivity.class);

  4. // Click a button which will start a new Activity

  5. // Here we use the ID of the string to find the right button

  6. solo.clickOnButton(solo.getString(R.string.button1));

  7. // Validate that the Activity is the correct one

  8. solo.assertCurrentActivity("wrong activity", SimpleListActivity.class);

  9. solo.clickInList(1);

  10. }

Sample Code

  1. public void testMenu() throws Exception {

  2. // open the menu

  3. solo.sendKey(Solo.MENU);

  4. solo.clickOnText("Preferences");

  5. solo.clickOnText("User");

  6. solo.clearEditText(0);

  7. Assert.assertTrue(solo.searchText(""));

  8. solo.enterText(0, "example");

  9. Assert.assertTrue(solo.searchText("example"));

  10. solo.goBack();

  11. }

Sample Code

  1.     @Override

  2.     public void tearDown() throws Exception {

  3.         solo.finishOpenedActivities();

  4.     }

Espresso

 

  • Espresso is an open source mobile testing automation framework for native Android apps developed by Google. The executed test .apk is synchronised by the framework with the Android UI thread.
  • Espresso has a small, predictable, easy to learn API and it is built on top of the Android instrumentation framework.
  • This framework allows white-box tests written in Java, using the Hamcrest matchers to simplify the reading and writing of test scripts.

Dependencies

To use Espresso in your Android test project, you need to add a dependency to the latest Espresso release to your build file.

    // Espresso support
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    // add this for intent mocking support
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
    // add this for webview testing support
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'

Sample Code

  1.     @Rule

  2.     public ActivityTestRule<MainActivity> mActivityRule =

  3.             new ActivityTestRule<>(MainActivity.class);

Sample Code

  1.    @Test

  2.    public void buttonShouldUpdateText(){

  3.        onView(withId(R.id.update)).perform(click());

  4.        onView(withId(getResourceId("Click"))).check(matches(withText("Done")));

  5.    }

  6.  

  7.    private static int getResourceId(String s) {

  8.        Context targetContext = InstrumentationRegistry.getTargetContext();

  9.        String packageName = targetContext.getPackageName();

  10.        return targetContext.getResources().getIdentifier(s, "id", packageName);

  11.    }

Sample Code

  1.    @Test

  2.    public void ensureListViewIsPresent() throws Exception {

  3.        onData(hasToString(containsString("Frodo"))).perform(click());

  4.        onView(withText(startsWith("Clicked:"))).

  5.        inRoot(withDecorView(

  6.            not(is(rule.getActivity().

  7.            getWindow().getDecorView())))).

  8.            check(matches(isDisplayed()));

  9.    }

Comparison

Appium

Cons

  • On iOS you can only run one test at a time per Mac
  • Limited support for gestures (even though you can write JavaScript wrappers for more gestures)
  • Limited support for Android < 4.1
  • It takes a long time to configure Appium for both Android and iOS

 

Pros:

  • Cross-platform open source mobile testing automation framework
  • You can use with native, hybrid or mobile web apps
  • Client libraries: Python, Java, JavaScript, Ruby, PHP, C#
  • Doesn’t require access to your source code or library
  • Compatibility with many CI environments
  • Has a strong community

Robotium

Cons:

  • Requires knowledge in coding with Java
  • Maintenance is high, e.g. you’ll probably lose 90% of your code with a UI refresh (very common in mobile apps)

 

Pros:

  • Simple API to write UI automation scripts

  • Open source Android framework for native and hybrid apps

  • Black-box and grey-box tests

  • No need for the source code, just the .apk

  • Easy integration with CI Maven, Gradle and ANT

Espresso

Cons:

  • Very small, not very active community

  • Lack of documentation

 

Pros:

  • Open source framework for native Android apps

  • Easy to set up and easily extendable

  • Provides a rich debugging information when a failure happens

  • Is supported on all API versions

Oğuzhan Karagöl

test

By blacklake

test

  • 363