[Espresso 4 Android Doc] 7. Espresso Web

聲明:本系列文章是對(duì) Android Testing Support Library官方文檔的翻譯,水平有限,歡迎批評(píng)指正。

1. Espresso 概覽
2. Espresso 設(shè)置說(shuō)明
3. Espresso 基礎(chǔ)
4. Espresso 備忘錄
5. Espresso 意圖
6. Espresso 高級(jí)示例
7. Espresso Web
8. AndroidJUnitRunner
9. ATSL 中的 JUnit4 規(guī)則
10. UI Automator
11. 可訪問(wèn)性檢查

下載 Espresso-Web

Espresso-web 是測(cè)試 Android 上 WebView 的切入點(diǎn)。它使用流行的 WebDriver API 原子內(nèi)省并控制 WebView 的行為。

與 onData 類似,WebView 的交互實(shí)際上是幾個(gè)視圖原子的組合。一個(gè)原子可以被看作一個(gè) ViewAction,一個(gè)在 UI 上執(zhí)行操作的自包含單元。然而,它們需要適當(dāng)?shù)木牟邉澆⑶沂謫隆eb 和 WebInteraction 對(duì)此樣本進(jìn)行了包裝,提供了 Espresso 風(fēng)格的 WebView 交互體驗(yàn)。

WebView 經(jīng)常在 Java/JavaScript 之間跨界工作,由于沒(méi)有機(jī)會(huì)將 JavaScript 中的數(shù)據(jù)引入到競(jìng)態(tài)機(jī)制(Espresso 得到的所有 Java 端的數(shù)據(jù)都一個(gè)獨(dú)立的副本),WebInteractions 全面支持?jǐn)?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)? 將會(huì)檢查斷言的真假性。示例如下:
onWebView()
  .withElement(findElement(Locator.ID, "teacher"))
  .withContextualElement(findElement(Locator.ID, "person_name"))
  .check(webMatches(getText(), containsString("Socrates")));
  • ?perform(Atom)? 將在當(dāng)前的上下文中執(zhí)行提供的原子操作,示例如下:
onWebView()
  .withElement(findElement(Locator.ID, "teacher"))
  .perform(webClick());
  • 當(dāng)之前的操作(如點(diǎn)擊)改變了界面導(dǎo)航,從而使 ElementReference 和 WindowReference 點(diǎn)失效時(shí),必須使用 ?reset()?。

WebView 示例

Espresso web 需要啟用 JavaScript 來(lái)控制 WebView。你可以通過(guò)覆寫 ActivityTestRule 類中的 afterActivityLaunched 方法強(qiáng)制啟用它。

@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(查看說(shuō)明
  • 打開應(yīng)用的 ?build.gradle? 文件。它通常不是頂級(jí) ?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'
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,716評(píng)論 25 709
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,835評(píng)論 2 45
  • 離開你很久了像茶喝過(guò)很多遍后漸漸淡下來(lái)我以為感情也如此 歲月無(wú)聲地流逝突然你又出現(xiàn)在我面前字里行間仿佛又回到了過(guò)去...
    一生如燕閱讀 322評(píng)論 0 4
  • 依然直接上代碼,很簡(jiǎn)單 2行 就實(shí)現(xiàn)第一種解決方案: 第二種解決方案: 提示當(dāng)設(shè)置后,文字依然跑偏,解決方案:首先...
    1b3bd36d9d21閱讀 15,449評(píng)論 6 5
  • 病毒 北上的火車轟隆隆的前行,身后的紅土地在熱帶闊葉林的掩映下,漸漸消失,四年前也...
    穴鳥閱讀 509評(píng)論 0 0

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