Espresso和UIAutomator - 完美的結合

本篇文章翻譯自Espresso & UIAutomator - the perfect tandem

Espresso是個功能強大、執行速度很快的Android自動化測試框架,但是它有一個重要的局限-你只可以在被測App的Context中操作。

意味著下列App自動化測試需求無法實現:

  • 應用的推送消息
  • 同步聯系人
  • 從另一個應用程序進入被測App

因為你要處理移動設備的其他應用程序,比如通知欄、聯系人等。

事實上,UIAutomator 2.0發布后可以實現上述功能。如Android Developers blog post所說“最重要的是,UIAutomator現在已經基于Android Instrumentation...”,因此,使用Instrumentation test runner既可以運行UIAutomator,也可以運行Espresso。

另外,我們可以將UIAutomator和Espresso測試結合起來,可以更加得心應手的處理被測應用和電話之間的交互。

下面的例子,我將解釋如何使用這種方法來自動化測試App的聯系人同步功能。

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiSelector;

@RunWith(AndroidJUnit4.class)
public class TestContactsSync {

    @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule(LoginActivity.class);

    UiDevice mDevice;
    String PEOPLE_APP = "People";
    String MY_APP = "XING";
    String userContactName = "Android Tester";

    @Before
    public void setUp() throws Exception{
        super.setUp();
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    }
    @Test
    public void testContactsSync() throws Exception {
    
        // Espresso code: login the user, navigate to contact sync and enable clicking on toggle
        logInUser();
        onView(withText(R.string.sync_button)).perform(click());
        onView(withId(R.id.contacts_sync_enable_toggle)).perform(click());

        // UIAutomator code: navigate to People app 
        mDevice.pressHome();
        UiObject conutactsApp = mDevice.findObject(new UiSelector().text(PEOPLE_APP));
        conutactsApp.clickAndWaitForNewWindow();

        // UIAutomator code: check that contact is present in it after sync was triggered    
        UiObject contactName = mDevice.findObject(new UiSelector().text(userContactName));
        assertTrue(contactName.exists());

        // UIAutomator code: navigate back to my app under testm
        Device.pressHome();
        UiObject myApp = mDevice.findObject(new UiSelector().text(MY_APP));
        myApp.clickAndWaitForNewWindow();

        // Espresso code: turn off contact sync and logout
        onView(withId(R.id.contacts_sync_enable_toggle)).perform(click());
        onView(withId(R.id.logout)).perform(click());
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}

是不是很完美?UIAutomator需要Android 4.3(API Level 18)及更高版本。稍微修改下build.gradle文件即可-添加productFlavors和聲明uiautomator依賴。

productFlavors {
    // The actual application flavor 
    production {
        minSdkVersion 14
    }
    // Test application flavor for uiautomatior tests
    test {
        minSdkVersion 18
    }
}

dependencies {
    // Instrumentation tests
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.1'
    androidTestCompile 'com.android.support.test:rules:0.2'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.0.0'
}

祝你好運:)

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

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • 作者:Ringoyan,騰訊測試開發工程師。先后為植物大戰僵尸Online,糖果傳奇等游戲擔任測試經理,其負責的“...
    飯盒閱讀 2,813評論 2 41
  • 一:移動端自動化測試框架對比 概述 1、Monkey是Android SDK自帶的測試工具,在測試過程中會向系統發...
    PeytonWu閱讀 1,889評論 0 3
  • 語言的起源可以追溯到人類的起源,每一個民族都有自己的語言,無論你能聽懂與否,其共通的就是說話者的情感、語氣、表情,...
    Miss墨菲閱讀 406評論 1 4
  • 時間線後面 十二三歲的頭哥登上破舊的校車 坐到我身邊 說肥仔 然後木棉花開 蘿蔔田跟我說一輩子第一次有人跟我說的話...
    康飄釀閱讀 231評論 0 2