【问题标题】:Interacting with <dialog> with Geb/Selenium (only with Chrome)使用 Geb/Selenium 与 <dialog> 交互(仅限 Chrome)
【发布时间】:2017-06-30 14:41:45
【问题描述】:

我所有的 Geb/Spock 测试都在 Firefox 上完美运行,然后我尝试使用 Chrome (v59),当出现警报或确认弹出窗口时它一直挂起。

对于 Firefox,我使用 withAlert{}withConfirm{} 来处理这些问题,但它不适用于 Chrome。

所以我对视图进行了一些挖掘,发现对话框由this widget 管理。所以我在我的页面对象中创建了&lt;dialog&gt; 的访问器,但 Chrome 仍然无法与之交互(无法检测到弹出窗口是否显示或关闭)。 我尝试了几件事,例如使用displayed 或等待显示文本但没有任何效果。

我不习惯 jQuery,所以也许我在这里遗漏了什么?

这是我必须与之交互的代码示例:

<dialog class="qq-alert-dialog-selector">
    <div class="qq-dialog-message-selector"></div>

    <div class="qq-dialog-buttons">
        <button type="button" class="qq-cancel-button-selector">Close</button>
    </div>
</dialog>

这就是我尝试访问它的方式(在我的页面对象中):

alertPopUp {$("dialog", class:"qq-alert-dialog-selector")}
alertCloseButton {alertPopUp.children("div.qq-dialog-buttons").children("button")}

最后我想如何测试弹出窗口:

when: "Trying to upload ..."
uploadFileButton = incorrect.absolutePath

then: "An alerting pop up occurs"
waitFor{alertPopUp.displayed}


when: "Closing the pop up"
alertCloseButton.click(SubmitAClaim)

then: "Number of uploaded files should not change"
uploadedElements.size() == initialUploadedFiles

有人可以帮忙吗?我想这可能与 chrome 的 focus 有关(我的意思是它当前是在弹出窗口上还是在后面的页面上。

编辑:

毕竟,问题似乎与 jQuery 无关(我猜是好事)。感谢&lt;dialog&gt; 中的open attribute,我们可以验证对话框是否打开。我做过这样的事情:

when: "Trying to upload ..."
uploadFileButton = incorrect.absolutePath

then: "A alerting pop up occurs"
waitFor {alertPopUp.getAttribute("open") == "true"}

但我仍然无法与对话框交互:当我尝试单击alertCloseButton 时,它会引发ElementNotVisibleException。所以我猜它与浏览器焦点有关。 我试过了:

  • withWindow{}driver.switchTo().window() 但唯一可用的窗口是主窗口。
  • withFrame() 但我得到了NoSuchFrameException
  • driver.switchTo().alert() 但它引发了NoAlertRaisedException

所以我想知道是否有人知道 Chrome 是如何看待&lt;dialog&gt; 标签(警报,确认,...)?

注意:根据caniuse.com,Chrome 是目前唯一兼容&lt;dialog&gt; 的浏览器。

【问题讨论】:

  • 我设法通过使用waitFor{alertPopUp.getAttribute("open") != null} 来测试弹出窗口是否通过了,但我什至不确定它是否真的断言正确。我仍然挂断单击对话框中的按钮。 Selenium 提出ElementNotVisibleException 并且我已经检查过:没有几个选定的元素不可见。只有一个 (alertCloseButton.size() == 1)。有没有人有想法或者我是否将其作为 ChromeDriver 2.30 上的问题记录?

标签: html google-chrome selenium spock geb


【解决方案1】:

终于我设法找到了解决这个问题的方法。我查看了 google demo 以供 &lt;dialog&gt; 使用,并使用 Geb 提供的 js object 执行来自 Google 的 js 示例。这最终给出了这个:

when: "Trying to upload ..."
uploadFileButton = incorrect.absolutePath

then: "A alerting pop up occurs"
waitFor {alertPopUp.getAttribute("open") == "true"}


when: "Closing the pop up"
report "After upload"
js.exec """var dialog = document.querySelector(".qq-alert-dialog-selector");
    dialog.close();
    return true;
"""

then: "Number of uploaded files should not change"
uploadedElements.size() == initialUploadedFiles

这是真的蛮力,我不喜欢它,因为它在很大程度上依赖于后端,如何设置对话框等等......但它有效。我真的希望 Geb/Selenium 能提供一些更容易使用的东西(因为&lt;dialog&gt;is W3C 它将在所有浏览器上变得很常见)。

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 2022-07-20
    • 2013-08-17
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多