聲明:本系列文章是對 Android Testing Support Library官方文檔的翻譯,水平有限,歡迎批評指正。
1. Espresso 概覽
2. Espresso 設置說明
3. Espresso 基礎
4. Espresso 備忘錄
5. Espresso 意圖
6. Espresso 高級示例
7. Espresso Web
8. AndroidJUnitRunner
9. ATSL 中的 JUnit4 規則
10. UI Automator
11. 可訪問性檢查
Espresso-web 是測試 Android 上 WebView 的切入點。它使用流行的 WebDriver API 原子內省并控制 WebView 的行為。
與 onData 類似,WebView 的交互實際上是幾個視圖原子的組合。一個原子可以被看作一個 ViewAction,一個在 UI 上執行操作的自包含單元。然而,它們需要適當的精心策劃并且十分啰嗦。Web 和 WebInteraction 對此樣本進行了包裝,提供了 Espresso 風格的 WebView 交互體驗。
WebView 經常在 Java/JavaScript 之間跨界工作,由于沒有機會將 JavaScript 中的數據引入到競態機制(Espresso 得到的所有 Java 端的數據都一個獨立的副本),WebInteractions 全面支持數據的返回。
常規 WebInteractions
-
?withElement(ElementReference)
? 將把?ElementReference
? 提交到原子中,示例如下:
onWebView().withElement(findElement(Locator.ID, "teacher"))
-
withContextualElement(Atom<ElementReference>)
將把 ElementReference 提交到原子鐘,示例如下:
onWebView()
.withElement(findElement(Locator.ID, "teacher"))
.withContextualElement(findElement(Locator.ID, "person_name"))
-
?check(WebAssertion)
? 將會檢查斷言的真假性。示例如下:
onWebView()
.withElement(findElement(Locator.ID, "teacher"))
.withContextualElement(findElement(Locator.ID, "person_name"))
.check(webMatches(getText(), containsString("Socrates")));
-
?perform(Atom)
? 將在當前的上下文中執行提供的原子操作,示例如下:
onWebView()
.withElement(findElement(Locator.ID, "teacher"))
.perform(webClick());
- 當之前的操作(如點擊)改變了界面導航,從而使 ElementReference 和 WindowReference 點失效時,必須使用
?reset()
?。
WebView 示例
Espresso web 需要啟用 JavaScript 來控制 WebView。你可以通過覆寫 ActivityTestRule 類中的 afterActivityLaunched 方法強制啟用它。
@Rule
public ActivityTestRule<WebViewActivity> mActivityRule = new ActivityTestRule<WebViewActivity>(WebViewActivity.class, false, false) {
@Override
protected void afterActivityLaunched() {
// Enable JS!
onWebView().forceJavascriptEnabled();
}
}
@Test
public void typeTextInInput_clickButton_SubmitsForm() {
// Lazily launch the Activity with a custom start Intent per test
mActivityRule.launchActivity(withWebFormIntent());
// Selects the WebView in your layout. If you have multiple WebViews you can also use a
// matcher to select a given WebView, onWebView(withId(R.id.web_view)).
onWebView()
// Find the input element by ID
.withElement(findElement(Locator.ID, "text_input"))
// Clear previous input
.perform(clearElement())
// Enter text into the input element
.perform(DriverAtoms.webKeys(MACCHIATO))
// Find the submit button
.withElement(findElement(Locator.ID, "submitBtn"))
// Simulate a click via javascript
.perform(webClick())
// Find the response element by ID
.withElement(findElement(Locator.ID, "response"))
// Verify that the response page contains the entered text
.check(webMatches(getText(), containsString(MACCHIATO)));
}
在 GitHub 上查看 Espresso Web sample。
下載 Espresso-Web
- 確保你已經安裝了 Android Support Repository(查看說明)
- 打開應用的
?build.gradle
? 文件。它通常不是頂級?build.gradle
?,而是??app/build.gradle
?。
在 dependencies 中添加以下行:
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
Espresso-Web 只兼容 Espresso 2.2+ 和 testing supprot library 0.3+,所以你也要更新如下行:
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'