Selenium:处理随机弹出的 window
Selenium : Handle a window that popups up randomly
我们有一项收集客户反馈的功能。为此,当用户注销时,window 随机弹出 - 并非每次都针对每个客户。
我想在我的自动化代码中处理这个。
目前,在注销时,我期待 window 并切换到它,当弹出窗口 window 没有出现时,该代码失败。
处理此问题的最佳方法是什么。
这是我目前所拥有的...
public static void waitForNumberOfWindowsToEqual(final int numberOfWindows) {
ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return (driver.getWindowHandles().size() == numberOfWindows);
}
};
WebDriverWait wait = new WebDriverWait(driver, BrowserFactory.explicitWait);
wait.until(expectation);
}
如果可能,理想的做法是查看源代码以确定弹出窗口 window 是否会出现,但是如果这无法实现,您可以采用以下方法:
// Get the number of windows open before clicking the log out button.
int numberOfWindowsBeforeLogOut = driver.getWindowHandles().size();
// Click the log out button.
logOutButton.click();
// Check how many windows are open after clicking the log out button.
int numberOfWindowsAfterLogOut = driver.getWindowHandles().size();
// Now compare the number of windows before and after clicking the log out
// button in a condition statement.
if (numberOfWindowsBeforeLogOut < numberOfWindowsAfterLogOut) {
// If there is a new window available, switch to it.
driver.switchTo().window(titleOrWindowHandle);
}
我会用 try/catch 来处理缺少弹出窗口 window。这是一个例子:
try {
WebDriverWait winwait = new WebDriverWait(driver, 3);
String mainWindow = driver.getWindowHandle();
// wait for 2 windows and get the handles
Set<String> handles = winwait.until((WebDriver drv) -> {
Set<String> items = drv.getWindowHandles();
return items.size() == 2 ? items : null;
});
// set the context on the last opened window
handles.remove(mainWindow);
driver.switchTo().window(handles.iterator().next());
// close the window
driver.close();
// set the context back to the main window
driver.switchTo().window(mainWindow);
} catch (TimeoutException ex) {
System.out.println("No window present within 3 seconds");
}
如果您没有获得所需的 window,代码将抛出一个 TimeoutException
。因此,将 wait.until(expectation)
放入 try 块中并捕获异常。在代码中,
try {
wait.until(expectation);
} catch (TimeoutException ex) {
System.out.println("Nowindow This Time");
}
我们有一项收集客户反馈的功能。为此,当用户注销时,window 随机弹出 - 并非每次都针对每个客户。 我想在我的自动化代码中处理这个。
目前,在注销时,我期待 window 并切换到它,当弹出窗口 window 没有出现时,该代码失败。
处理此问题的最佳方法是什么。
这是我目前所拥有的...
public static void waitForNumberOfWindowsToEqual(final int numberOfWindows) {
ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return (driver.getWindowHandles().size() == numberOfWindows);
}
};
WebDriverWait wait = new WebDriverWait(driver, BrowserFactory.explicitWait);
wait.until(expectation);
}
如果可能,理想的做法是查看源代码以确定弹出窗口 window 是否会出现,但是如果这无法实现,您可以采用以下方法:
// Get the number of windows open before clicking the log out button.
int numberOfWindowsBeforeLogOut = driver.getWindowHandles().size();
// Click the log out button.
logOutButton.click();
// Check how many windows are open after clicking the log out button.
int numberOfWindowsAfterLogOut = driver.getWindowHandles().size();
// Now compare the number of windows before and after clicking the log out
// button in a condition statement.
if (numberOfWindowsBeforeLogOut < numberOfWindowsAfterLogOut) {
// If there is a new window available, switch to it.
driver.switchTo().window(titleOrWindowHandle);
}
我会用 try/catch 来处理缺少弹出窗口 window。这是一个例子:
try {
WebDriverWait winwait = new WebDriverWait(driver, 3);
String mainWindow = driver.getWindowHandle();
// wait for 2 windows and get the handles
Set<String> handles = winwait.until((WebDriver drv) -> {
Set<String> items = drv.getWindowHandles();
return items.size() == 2 ? items : null;
});
// set the context on the last opened window
handles.remove(mainWindow);
driver.switchTo().window(handles.iterator().next());
// close the window
driver.close();
// set the context back to the main window
driver.switchTo().window(mainWindow);
} catch (TimeoutException ex) {
System.out.println("No window present within 3 seconds");
}
如果您没有获得所需的 window,代码将抛出一个 TimeoutException
。因此,将 wait.until(expectation)
放入 try 块中并捕获异常。在代码中,
try {
wait.until(expectation);
} catch (TimeoutException ex) {
System.out.println("Nowindow This Time");
}