聲明:本系列文章是對 Android Testing Support Library官方文檔的翻譯,水平有限,歡迎批評指正。
1. Espresso 概覽
2. Espresso 設(shè)置說明
3. Espresso 基礎(chǔ)
4. Espresso 備忘錄
5. Espresso 意圖
6. Espresso 高級示例
7. Espresso Web
8. AndroidJUnitRunner
9. ATSL 中的 JUnit4 規(guī)則
10. UI Automator
11. 可訪問性檢查
Espresso-web 是測試 Android 上 WebView 的切入點晋涣。它使用流行的 WebDriver API 原子內(nèi)省并控制 WebView 的行為。
與 onData 類似沉桌,WebView 的交互實際上是幾個視圖原子的組合谢鹊。一個原子可以被看作一個 ViewAction,一個在 UI 上執(zhí)行操作的自包含單元留凭。然而佃扼,它們需要適當?shù)木牟邉澆⑶沂謫隆eb 和 WebInteraction 對此樣本進行了包裝蔼夜,提供了 Espresso 風格的 WebView 交互體驗兼耀。
WebView 經(jīng)常在 Java/JavaScript 之間跨界工作,由于沒有機會將 JavaScript 中的數(shù)據(jù)引入到競態(tài)機制(Espresso 得到的所有 Java 端的數(shù)據(jù)都一個獨立的副本),WebInteractions 全面支持數(shù)據(jù)的返回瘤运。
常規(guī) 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)
? 將在當前的上下文中執(zhí)行提供的原子操作官撼,示例如下:
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
- 確保你已經(jīng)安裝了 Android Support Repository(查看說明)
- 打開應(yīng)用的
?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'