We have a feature that collects customer feedback. For this , when the user logs out , a window pops up up randomly - not every time for every customer. I want to handle this in my automation code.
我们有一项收集客户反馈的功能。为此,当用户注销时,会随机弹出一个窗口 - 而不是每次都为每个客户弹出一个窗口。我想在自动化代码中处理这个问题。
Currently, at the log out, I'm expecting a window and switching to it and that code is failing when the popup window doesn't show up.
目前,在注销时,我期待一个窗口并切换到它,并且当弹出窗口没有显示时代码失败。
What's the best way to handle this .
处理这个问题的最佳方法是什么。
This is what I have so far ...
这就是我到目前为止......
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);
}
3 个解决方案
#1
1
I would handle the absence of popup window with a try/catch. Here is an example:
我会用try / catch来处理弹出窗口的缺失。这是一个例子:
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");
}
#2
1
If possible, the ideal thing to do would be to have a look through the source to work out whether the popup window will appear, however if this isn't achievable you could take the following approach:
如果可能的话,理想的做法是查看源代码以确定是否会出现弹出窗口,但是如果无法实现这一点,您可以采取以下方法:
// 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);
}
#3
0
In case you don't get the required window, the code will throw a TimeoutException
. So, put wait.until(expectation)
inside a try block and catch the exception. In code,
如果您没有获得所需的窗口,代码将抛出TimeoutException。因此,将wait.until(expectation)放在try块中并捕获异常。在代码中,
try {
wait.until(expectation);
} catch (TimeoutException ex) {
System.out.println("Nowindow This Time");
}
#1
1
I would handle the absence of popup window with a try/catch. Here is an example:
我会用try / catch来处理弹出窗口的缺失。这是一个例子:
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");
}
#2
1
If possible, the ideal thing to do would be to have a look through the source to work out whether the popup window will appear, however if this isn't achievable you could take the following approach:
如果可能的话,理想的做法是查看源代码以确定是否会出现弹出窗口,但是如果无法实现这一点,您可以采取以下方法:
// 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);
}
#3
0
In case you don't get the required window, the code will throw a TimeoutException
. So, put wait.until(expectation)
inside a try block and catch the exception. In code,
如果您没有获得所需的窗口,代码将抛出TimeoutException。因此,将wait.until(expectation)放在try块中并捕获异常。在代码中,
try {
wait.until(expectation);
} catch (TimeoutException ex) {
System.out.println("Nowindow This Time");
}