2017-08-22 35 views
1

我正在從咖啡的Web咖啡webView的webkeys在Android 8.0模擬器失敗

@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 WebView objects, 
    // 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 and enter new text into the input element. 
     .perform(clearElement()) 
     .perform(DriverAtoms.webKeys(MACCHIATO)) 

     // Find the "Submit" button and simulate a click using JavaScript. 
     .withElement(findElement(Locator.ID, "submitBtn")) 
     .perform(webClick()) 

     // Find the response element by ID, and verify that it contains the 
     // entered text. 
     .withElement(findElement(Locator.ID, "response")) 
     .check(webMatches(getText(), containsString(MACCHIATO))); 
} 

它製作的7.1.1模擬器但不是在8.0 我得到了錯誤消息

一些測試代碼

所致:了java.lang.RuntimeException:錯誤evaluationEvaluation:狀態:13值:{消息=無法設置的選定結束} hasMessage:真消息:不能設定的選定結束

如果我更改代碼

element. 
     .perform(clearElement()) 
     .perform(DriverAtoms.webKeys(MACCHIATO)) => perform(webClick()) 

然後,它的工作原理。所以我猜想它可以找到元素只是不執行操作。 有什麼我需要改變我的代碼?

回答

1

我面臨同樣的問題。請參閱Cant add text to webview text field with Espresso的帖子

我的代碼適用於Android 7.1.1模擬器,但在Android 8.0模擬器上也失敗。 我通過在build.gradle中執行以下操作來解決問題。

1)升級espresso庫。 我是:

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' 
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') 

現在到:

androidTestCompile 'com.android.support.test:runner:1.0.0' 
androidTestCompile 'com.android.support.test:rules:1.0.0' 
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0' 
androidTestCompile('com.android.support.test.espresso:espresso-contrib:3.0.0') 

2)這樣做之後,你的應用程序可能無法建立。它可能會說這是不可能找到測試:亞軍:1.0.0 在這種情況下,你需要添加

repositories { 
    maven { url "https://maven.google.com" } 
} 

3)您可能需要解決以下的問題是,它可能會抱怨「版本的應用程序(2x.xx)和測試應用程序(2x.xx)不同「 因此,我在gradle中添加以下內容。

configurations.all { 
    resolutionStrategy.force 'com.android.support:support-annotations:2x.x.x' 
} 

4)另外,您可能需要確保您已添加Runner。

defaultConfig { 
     "android.support.test.runner.AndroidJUnitRunner" 
} 
+0

謝謝它適合我。一段時間以來,我一直在使用舊版的android espresso。 – Puff