Espresso UI測試入門

定義

一個基于instrumentation-based API 的UI測試框架,運行于AndroidJunitRunner中。

特點

  • One of the key parts of Espresso is its ability to synchronize all test actions. Espresso waits until the UI is idle before it moves to the next operation.

From https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex#9

  • Espresso detects when the main thread is idle, so it is able to run your test commands at the appropriate time, improving the reliability of your tests. This capability also relieves you from having to add any timing workarounds, such as Thread.sleep() in your test code.

From https://www.google.com/search?q=%E7%BF%BB%E8%AF%91&oq=%E7%BF%BB%E8%AF%91&aqs=chrome..69i57j69i61l3j0l2.1255j0j7&sourceid=chrome&ie=UTF-8

  • 一次只能測試一個應用程序。

概括

OnView() => Click()==>Check():
1.onView 、Ondata找到一個view。
2.ViewAction.click()等執(zhí)行一組UI交互操作。
3.等待UI交互操作執(zhí)行完畢,使用ViewAssertions去斷言期望的狀態(tài)或行為。
就像下面代碼片段:

onView(withId(R.id.my_view))            // withId(R.id.my_view) is a ViewMatcher
        .perform(click())               // click() is a ViewAction
        .check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion

找到一個View

Calling methods in the ViewMatchers class. For example, to find a view by looking for a text string it displays, you can call a method like this:

onView(withText("Sign-in"));

Similarly you can call withId() and providing the resource ID (R.id) of the view, as shown in the following example:

onView(withId(R.id.button_signin));

Android resource IDs are not guaranteed to be unique. If your test attempts to match to a resource ID used by more than one view, Espresso throws an AmbiguousViewMatcherException.

From https://developer.android.com/training/testing/ui-testing/espresso-testing.html#build

更加精確地找到一個view:

onView(allOf(withId(R.id.button_signin), withText("Sign-in")));

From https://developer.android.com/training/testing/ui-testing/espresso-testing.html#build

注意

如果是AdapterView類型的viewGroup中的某個子View,(ListView、GridView)則需要使用onData()。

執(zhí)行交互操作

用來執(zhí)行UI交互操作的API封裝在ViewActions中,如:

?   ViewActions.click(): Clicks on the view.
?   ViewActions.typeText(): Clicks on a view and enters a specified string.
?   ViewActions.scrollTo(): Scrolls to the view. The target view must be subclassed from ScrollView and the value of its android:visibilityproperty must be VISIBLE. For views that extend AdapterView (for example, ListView), the onData() method takes care of scrolling for you.
?   ViewActions.pressKey(): Performs a key press using a specified keycode.
?   ViewActions.clearText(): Clears the text in the target view.

From https://developer.android.com/training/testing/ui-testing/espresso-testing.html#build

驗證結果

ViewInteraction 、 DataInteraction提供了check()去驗證UI的狀態(tài)是否符合期望。Check()接受一個參數(shù)ViewAssertion .

 // Check that the text was changed.
   onView(withId(R.id.textToBeChanged))
           .check(matches(withText(STRING_TO_BE_TYPED)));

From https://developer.android.com/training/testing/ui-testing/espresso-testing.html#build

Idling resources

Espresso關鍵的優(yōu)點是: 自動等待UI變成閑置狀態(tài)才開始執(zhí)行下一步操作。例如,Espresso會自動等待AsyncTask后臺操作執(zhí)行完畢才開始執(zhí)行下一步操作。 不需要自己去執(zhí)行wait()或者sleep()。

但是,如果自己管理線程調(diào)度后者其他后臺服務時,也會遇到不能同步的情況。不過Espresso提供了Idling resources去處理同步。

From https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex#9
我們可以使用Espresso內(nèi)置的實現(xiàn)類
CountingIdlingResource
在Test中,需要在@Before函數(shù)里注冊registerIdlingResource.
它包含一個計數(shù)器,可調(diào)用increment()decrement()訪問它。

// The network request might be handled in a different thread so make sure Espresso knows
    // that the app is busy until the response is handled.
    EspressoIdlingResource.increment(); // App is busy until further notice
mNotesRepository.getNotes(new NotesRepository.LoadNotesCallback() {
        @Override
        public void onNotesLoaded(List<Note> notes) {
            EspressoIdlingResource.decrement(); // Set app as idle.
            mNotesView.setProgressIndicator(false);
            if (notes.isEmpty()) {
                mNotesView.showNotesEmptyPlaceholder();
            } else {
                mNotesView.showNotes(notes);
            }
        }
    });

當執(zhí)行耗時操作時,可調(diào)用

EspressoIdlingResource.increment()//UI開始處于忙碌狀態(tài)

在耗時操作完成時,在回調(diào)函數(shù)調(diào)用

EspressoIdlingResource.decrement(); // 通知Espresso此時UI處于閑置狀態(tài),

這時候可繼續(xù)執(zhí)行下一步操作。

相關鏈接

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容