← 開發日常

用 Espresso 測試 Android (一)

Import Espresso

Add the following dependency to app/build.gradle

plain text
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'

Espresso Basic Usage

Espresso提供了十分簡潔的API,幾乎是一行程式碼就能代表一個UI操作。

假設我們有一個id為R.id.button的Button與R.id.text的TextView。當Button被按下時,TextView會顯示Hello World。我們可以針對這個功能寫一個簡單的測試。

plain text
//按下 R.id.button
onView(withId(R.id.button)).perform(click());

//確認 R.id.text上顯示Hello World
onView(withId(R.id.text)).check(matches(withText("Hello World")));

觀察上面的例子,我們發現程式碼可以分成三個部分 :

1. Find a View :

plain text
onView(withId(R.id.button)).perform(click());

透過onView(Matcher<View>)找到一個ViewInteraction。之後我們便可以透過ViewInteraction去操作這個View或者是Check他的狀態。

我們在找View時,可以透過許多方式去獲得一個唯一的View,例如:

  • withId(int)指定Resource id
  • withText(String)指定View中的文字

2. Perform a Action

plain text
onView(with(R.id.button)).perform(click());

找到View對應的ViewInteraction之後,透過ViewInteraction.perform(ViewAction)去操作View,例如 :

  • click()模擬點擊View
  • typeText(String)模擬輸入文字到View上
  • scrollTo()模擬滑動螢幕直到View出現

3. Check

plain text
onView(withId(R.id.text)).check(matches(withText("Hello World")));

當我們模擬了一系列的UI操作之後,我們就得確認App的UI是否如我們預期。透過ViewInteraction.check(matches(Matcher<? super View>)),我們便可以驗證UI的狀態。

Reference

  1. https://developer.android.com/training/testing/espresso/index.html