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
- @Before
- public void setUp() throws Exception {
- File appDir = new File(System.getProperty("user.dir"), "../../../apps/TestApp/build/release-iphonesimulator");
- File app = new File(appDir, "TestApp.app");
- DesiredCapabilities capabilities = new DesiredCapabilities();
- capabilities.setCapability("platformVersion", "9.3");
- capabilities.setCapability("deviceName", "iPhone 6");
- capabilities.setCapability("app", app.getAbsolutePath());
- driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
- values = new ArrayList<Integer>();
- }
Sample Code
-
@Test
-
public void testActive() throws Exception {
-
WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
-
assertTrue(text.isDisplayed());
-
WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
-
assertTrue(button.isDisplayed());
-
}
Sample Code
-
@Test
-
public void testClear() throws Exception {
-
WebElement text =
driver.findElement(By.xpath("//UIATextField[1]")); -
text.sendKeys("12");
-
text.clear();
-
-
assertEquals("", text.getText());
-
}
Sample Code
-
@Test
-
public void testLocation() throws Exception {
-
WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
-
-
Point location = button.getLocation();
-
-
assertEquals(110, location.getX());
-
assertEquals(143, location.getY());
-
}
Sample Code
- @After
- public void tearDown() throws Exception {
- driver.quit();
- }
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
-
public void setUp() throws Exception {
-
solo = new Solo(getInstrumentation(), getActivity());
-
}
Sample Code
-
public void testActivity() throws Exception {
-
// check that we have the right activity
-
solo.assertCurrentActivity("wrong activity", SimpleActivity.class);
-
// Click a button which will start a new Activity
-
// Here we use the ID of the string to find the right button
-
solo.clickOnButton(solo.getString(R.string.button1));
-
// Validate that the Activity is the correct one
-
solo.assertCurrentActivity("wrong activity", SimpleListActivity.class);
-
solo.clickInList(1);
-
}
Sample Code
-
public void testMenu() throws Exception {
-
// open the menu
-
solo.sendKey(Solo.MENU);
-
solo.clickOnText("Preferences");
-
solo.clickOnText("User");
-
solo.clearEditText(0);
-
Assert.assertTrue(solo.searchText(""));
-
solo.enterText(0, "example");
-
Assert.assertTrue(solo.searchText("example"));
-
solo.goBack();
-
}
Sample Code
-
@Override
-
public void tearDown() throws Exception {
-
solo.finishOpenedActivities();
-
}
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
-
@Rule
-
public ActivityTestRule<MainActivity> mActivityRule =
-
new ActivityTestRule<>(MainActivity.class);
Sample Code
-
@Test
-
public void buttonShouldUpdateText(){
-
onView(withId(R.id.update)).perform(click());
-
onView(withId(getResourceId("Click"))).check(matches(withText("Done")));
-
}
-
-
private static int getResourceId(String s) {
-
Context targetContext = InstrumentationRegistry.getTargetContext();
-
String packageName = targetContext.getPackageName();
-
return targetContext.getResources().getIdentifier(s, "id", packageName);
-
}
Sample Code
-
@Test
-
public void ensureListViewIsPresent() throws Exception {
-
onData(hasToString(containsString("Frodo"))).perform(click());
-
onView(withText(startsWith("Clicked:"))).
-
inRoot(withDecorView(
-
not(is(rule.getActivity().
-
getWindow().getDecorView())))).
-
check(matches(isDisplayed()));
-
}
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
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)
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
Github: github.com/BlackLake
Linkedin: https://www.linkedin.com/in/oguzhankaragol/
Gmail: oguzhankaragol60@gmail.com
test
By blacklake
test
- 363