使用 Selenium 处理浏览器弹出窗口
-
01-07-2019 - |
题
我们正在针对现有代码库运行 Selenium 回归测试,并且我们的 Web 应用程序中的某些屏幕使用弹出窗口作为中间步骤。
目前我们测试中使用的命令:
// force new window to open at this point - so we can select it later
selenium().getEval("this.browserbot.getCurrentWindow().open('', 'enquiryPopup')");
selenium().click("//input[@value='Submit']");
selenium().waitForPopUp("enquiryPopup", getWaitTime());
selenium().selectWindow("enquiryPopup");
...有效 大多数时候. 。有时测试会失败 waitForPopUp()
与
com.thoughtworks.selenium.SeleniumException: Permission denied
谁能建议一个更好、更多的 可靠的 方法?
此外,我们主要在 IE6 和 7 上运行这些测试。
解决方案
有用!!只是为了让那些喜欢 Selenese 的人更容易。
这对我使用 IE7(正常模式)有用。
真是个麻烦事。感谢天空中的意大利面怪物,否则我不可能在 IE 中运行它。
<tr>
<td>getEval</td>
<td>selenium.browserbot.getCurrentWindow().open('', 'windowName');</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>buttonName</td>
<td></td>
</tr>
<tr>
<td>windowFocus</td>
<td>windowName</td>
<td></td>
</tr>
<tr>
<td>waitForPopUp</td>
<td>windowName</td>
<td>3000</td>
</tr>
<tr>
<td>selectWindow</td>
<td>windowName</td>
<td></td>
</tr>
其他提示
如果您在 *iehta 模式下运行,那么您将会在这里或那里遇到一些故障。我们在工作中运行 Selenium,并且 IE 和 AJAX 似乎存在很多问题。
然而,听起来您遇到的问题是 Selenium 试图在完全加载之前访问另一个窗口中的组件。我不确定您的默认超时范围设置为多少,但您可能需要尝试将其增加到 60 (60000ms) 秒左右以解决该问题。
除此之外,我建议在 Firefox 中运行测试(使用 *chrome),因为它会产生更可靠的结果,但有时由于业务需求,这是不可能的。
我刚刚尝试添加另一个硒功能, windowFocus()
:
// force new window to open at this point - so we can select it later
selenium().getEval("this.browserbot.getCurrentWindow().open('', 'enquiryPopup')");
selenium().click("//input[@value='Submit']");
selenium().windowFocus("enquiryPopup");
selenium().waitForPopUp("enquiryPopup", getWaitTime());
selenium().selectWindow("enquiryPopup");
当我在本地运行测试时,测试成功了,但仅限于所有这些方法调用 - 创建/焦点/等待/选择。
我即将让构建服务器运行所有测试,如果也成功,我将用它创建一个库函数......!
我需要在弹出窗口中选择一个 iframe 并填写表格。我在使用 selectWindow cmd 时遇到问题,其中 selenium 找不到我的 iframe,因此我删除了该命令。
这个 Selenese 对我来说效果很好(其中 iframe 标题和 id = account_frame):
<tr>
<td>click</td>
<td>//a[@class='item_add']</td>
<td></td>
</tr>
<tr>
<td>windowFocus</td>
<td>account_frame</td>
<td></td>
</tr>
<tr>
<td>waitForPopUp</td>
<td>account_frame</td>
<td>10000</td>
</tr>
尝试在导致问题的调用周围添加一些等待语句。
我以前也犯过同样的错误,这是我能够做到的唯一方法 可靠地 通过调用 System.Threading.Thread.Sleep(5000) 来解决它们。