【Android】UI測試:UIAutomator & Espresso

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());
}

參考

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,569評論 2 45
  • 為什么要用Markdown Markdown十分的簡潔,跨平臺性更是不用說,一個VIM或記事本就可以完成的編寫,我...
    BigJelly閱讀 369評論 0 1
  • 第一次見到他的時候,我還在感嘆著家里的她真是風情萬種,引來無數追求者。現在看來他們仿佛是一對喜歡吵架的戀人未滿,天...
    小九jdjd閱讀 539評論 0 0
  • 發現效率最高的時候是一個人的時候 某種意義上social會嚴重影響我的效率 但是不能放棄social 因為存在的意...
    littleairplane閱讀 215評論 0 1