繁体   English   中英

是否有明确的 Selenium 解决方案来使用 Java 在 Internet Explorer 中弹出模态弹出对话框?

[英]Is there a definite Selenium solution to modal pop up dialogs in Internet Explorer with Java?

我已经搜索了很多很多地方来解决我的问题,但还没有找到。 我认为到目前为止,Selenium 将提供一个直接且简单的解决方案来使用 Java 处理来自 Internet Explorer 的模式窗口/对话框。

我正在测试的 Web 应用程序具有以下特征:

  • 它仅受 Internet Explorer 支持(无法解决此问题)
  • 主页接受用户名和密码,带有“登录”按钮
  • 在登录和页面加载时,会弹出一个“欢迎 - 新功能”窗口,其中包含一个“不再显示”复选框和一个“确定”按钮以关闭该窗口。
  • 在关闭弹出窗口之前,我无法对父窗口执行任何操作
  • 弹出窗口禁用右键单击(但是,我可以在登录和弹出窗口之前打开F12工具查看源代码)

这是我尝试过的:

  • getWindowHandles()总是为父窗口返回 1,所以这使得driver.switchTo(handle)不适用
  • 这不是警报,因此driver.switchTo().alert()accept()不起作用
  • findElement(By whatever)不会在弹出窗口中找到任何元素(如“确定”按钮或复选框等)
  • Robot类是我见过的唯一工作,在那里我可以发送按键以导航到“确定”按钮并单击它以关闭窗口...

这是我的问题:

  • 由于有一个“不再显示”复选框,因此有些用户会显示此模式弹出窗口,有些则不会。 我需要考虑这两种情况
  • 我需要找到一种 100% 确定的方法来知道是否显示弹出窗口。 如果我有这些信息,我可以使用 Robot 类(尽管“脏”)在需要时对弹出窗口执行操作
  • 我尝试找出是否使用isEnabled()启用了父窗口元素,但是即使在显示模式弹出窗口时项目不是手动“可点击”, isEnabled()总是返回 TRUE——所以这不起作用——有没有更好的方法来检查背景中的“阻塞”元素?

我的问题:

  • 您如何检查是否存在未 100% 显示的模式弹出窗口? (在 Internet Explorer 10 上,使用 Selenium 和 Java)
  • 除了使用 Robot 类之外,您如何与模式弹出对话框中的实际元素交互(例如,并不总是向用户显示相同选项的动态单选按钮)?

谢谢你。

在我看来,您应该在某些预期条件下使用WebDriverWait 例如,

WebDriverWait wait = new WebDriverWait(driver, 5); // sets timeout to 5 seconds
wait.until(...); // Use ExpectedCondition to set the condition you need to check for (i.e. element to be clickable, frame to be visible, etc.)
// Do your thing.

until方法将返回与传递的函数相关的对象类型。 例如, until(ExpectedConditions.elementToBeClickable(...)); 将返回一个WebElement对象,您可以使用它来执行操作(例如单击它)。

最后,您应该将这些行包装在 try/catch 中并处理TimeoutException ,如果条件永远不会出现,wait 方法将抛出。

总而言之,在结构上,您的代码应如下所示:

// instantiate the WebDriver
...
int timeoutMax = 2; // The maximum number of seconds you wish to wait before timing out (let's assume 2 seconds is reasonable for your case)
try {
    By checkboxLocator = By.id("checkboxID"); // Locate element by some criteria (id, css, xpath). Using by ID for illustration purposes only
    By buttonLocator = By.id("buttonID"); // same as above
    By popupLocator = By.id("frameid"); // same as above
    WebDriverWait wait = new WebDriverWait(driver, timeoutMax);
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(popupLocator)); // assuming it is an iframe

    // The next lines will not be executed if the "Don't display this again" and clicking "OK" were clicked before (locating the frame will timeout because it not be visible)
    WebElement checkbox = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
    WebElement okBtn = wait.until(ExpectedConditions.elementToBeClickable(checkboxLocator));
    checkbox.click();
    okBtn.click();
    driver.switchTo().defaultContent(); // Switch back to default window
} catch (TimeoutException exc) {
    // Handle exception (i.e. log a warning) - This should be thrown as long as the modal dialog doesn't become visible
    // If modal dialog ever becomes visible again, clicking on the checkbox and OK button will be executed again.
}

像这样的事情应该有效。 当然,这做出了一些可能不适用于您的案例的假设。 但是,如果您为模态对话框使用正确的定位技术,您应该能够:

  1. 定位模态窗口(使用By类定位)
  2. 使用WebDriverWait设置超时条件
  3. 告诉驱动程序切换到它(如果超时,请跳过步骤 3、4 和 5)
  4. 找到复选框和确定按钮
  5. 按顺序单击复选框和确定按钮
  6. 告诉司机切换回主窗口
  7. 继续你的测试

根据布尔变量创建一个 If 语句,以检查是否存在未 100% 显示的模式弹出窗口。

如果模态是 html 生成的(它应该是,如果它包含动态内容),然后尝试:

driver.switchTo().activeElement();

driver.switchTo().defaultContent();

此外,您可能需要插入一个等待,以便 html 有时间生成。

如果模态是浏览器警报,请尝试:

警报。接受();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM