【问题标题】:Selenium WebDriver : Verify Print Window dialog displayed on the pageSelenium WebDriver:验证页面上显示的打印窗口对话框
【发布时间】:2014-03-27 16:16:36
【问题描述】:
我有一个场景来验证单击打印链接后打印属性对话框(Windows 组件)是否正确打开。了解 Java 中的 Robot 实用程序类,它可以模拟 Escape/Enter 等键盘事件以在该窗口上操作。
我们有什么方法可以验证打开的新对话框是 Print 对话框 - 验证对话框标题的东西,即 Print 或从该窗口对话框中检索文本或其他东西否则将确认对话框是 打印 对话框。
【问题讨论】:
标签:
java
selenium-webdriver
【解决方案1】:
打印对话框来自操作系统,硒无法处理(还)。因此,您将无法检查是否存在。我能想到的唯一方法是使用 java.awt.Robot,发送 VK_ESCAPE 并断言测试继续。
作为初学者,你可以试试这个:
Runnable r = new Runnable() {
@Override
public void run() {
try {
Robot r = new Robot();
r.delay(1000);
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
Actions actions = new Actions(getDriver());
actions.sendKeys(Keys.CONTROL).sendKeys("p");
Thread t = new Thread(r);
t.start();
actions.perform();
//some stupid asserts that we reached here
【解决方案2】:
如果您在 Windows 中操作(我假设您是),您可以使用 Visual Studio 附带的 inspect.exe 工具。它将允许您与对话框交互,甚至发送您想要准确的任何信息,包括从下拉列表中选择元素或任何其他所需的交互。如果您希望使用 selenium 保存文件,这甚至可以工作,但要回答您的问题,您甚至可以使用它来检测该窗口是否确实存在。你想如何从那里开始是你的电话。
//using System.Windows.Automation;
//using System.Windows.Forms;
AutomationElement desktop = AutomationElement.RootElement;
AutomationElement Firefox = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaWindowClass"));
AutomationElement PrinterComboBox = PrintForm1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "1139"));
SelectionPattern selectPrinterComboBox = (SelectionPattern)PrinterComboBox.GetCurrentPattern(SelectionPattern.Pattern);
AutomationElement ItemInDropdown = PrinterComboBox.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "SelectPrintMethod"));
SelectionItemPattern ItemInDropdownSelectItem = (SelectionItemPattern)ItemInDropdown.GetCurrentPattern(SelectionItemPattern.Pattern);
ItemInDropdownSelectItem.Select();
AutomationElement OKButton = PrintForm1.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "1"));
InvokePattern ClickOK = (InvokePattern)OKButton.GetCurrentPattern(InvokePattern.Pattern);
ClickOK.Invoke();