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
Driver for platforms
@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());
}
@Test
public void testClear() throws Exception {
WebElement text =
driver.findElement(By.xpath("//UIATextField[1]"));
text.sendKeys("12");
text.clear();
assertEquals("", text.getText());
}
@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());
}
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'
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
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);
}
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();
}
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
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'
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
@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);
}
@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()));
}
Cons
Cons:
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