UIAutomator
允许以全局的视角操作设备和OS,可用于跨进程跨应用做黑盒测试。
支持 API 18 或以上,不支持 WebView。
提供5个基本工具类,用于操作测试流程:
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
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();
new UiObject(new UiSelector().className("android.widget.Button").
text("Sign In").instance(0)).click();
// Wait Sign in progress window
getUiDevice().waitForWindowUpdate(null, 2000);
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 #Testdroid!");
// Tweeting!
new UiObject(new UiSelector().text("Tweet")).click();
Espresso
基于 Android instrumentation framework 构建的快速UI测试框架,拥有简单易用的API及与UI线程同步的响应性能。用于 App 单应用测试,可通过动作录制方式自动生成测试代码。
支持 API 8及以上,不支持 WebView。
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(”#Testdroid")));
// Tweeting!
onView(withId(getInstrumentation().getTargetContext().getResources()
.getIdentifier("com.twitter.android:id/composer_post", null, null))).perform(click());
}