1

我有一个 android SDK,按下按钮会打开一个 Webview,我可以在其中看到一个按钮,其 html 视图如下:

<button type="button" class="button js-analytics-submit" id="submit-try-sample" data-analytics-category="Sample" data-analytics-label="Intro">
  <svg xmlns="http://www.w3.org/2000/svg" width="58" height="58" viewBox="0 0 58 58" id="button-logo
" class="button__logo">
  <path fill="currentColor" fill-rule="evenodd" d="M37.32 21.933l2.877-2.876-.626-.625c-5.04-5.043-13.247-5.043-18.29 0-4.832 4.833-5.03 12.568-.6 17.643l-2.876 2.875.625.625c5.042 5.043 13.247 5.043 18.29 0 4.832-4.832 5.03-12.567.6-17.642zm-14.79-2.25c4.144-4.144 10.76-4.344 15.142-.6l-1.64 1.64c-3.488-2.86-8.644-2.664-11.9.593-3.256 3.256-3.454 8.412-.594 11.9l-1.608 1.608c-3.743-4.382-3.542-11 .6-15.142zm12.94 18.642c-4.143 4.144-10.76 4.344-15.142.6l1.64-1.64c3.488 2.86 8.644 2.664 11.9-.593 3.256-3.256 3.454-8.412.594-11.9l1.608-1.608c3.743 4.382 3.543 10.998-.6 15.14z"></path>
</svg>
  <span class="button__wrapper" id="button-text">
    <span class="button__text">
      Let’s do this!
    </span>
  </span>
</button>

现在我正在尝试使用以下方法按下/单击一个按钮,但没有任何效果:(

onWebView().perform(script("return document.getElementById(\"submit-try-sample\").click()"));

onWebView().withElement(findElement(Locator.ID, "submit-try-sample")).perform(webClick());

onWebView().withElement(findElement(Locator.XPATH, "//button//span[contains(text(),'do this')]")).perform(webClick());

上面的代码没有抛出任何错误并成功执行,但它没有点击按钮。

4

2 回答 2

0

我不明白您在回答中的意思,但是当按钮具有 ID(id="button-text")时,这是单击 Button 的简单方法。

你可以简单地写

onWebView().withElement(findElement(Locator.ID,"button-Text")).
perform(DriverAtoms.webClick());
于 2016-08-17T10:49:59.760 回答
0

在您的测试前部分添加以下代码行 -

onWebView().forceJavascriptEnabled();   

阅读更多关于 Espresso Webviews http://www.qaautomated.com/2016/02/testing-web-views-using-espresso-web.html

于 2016-09-26T13:20:22.077 回答