admin 管理员组文章数量: 1086019
I'm trying to automate a scenario where I click on an action that launches a pop up window, but this new window doesn't load, because it throws a browser alert (that's expected).
Alert
I need to verify that this alert is presented. This is my code:
WebDriver driver = SeleniumSession.get().getWrappedDriver();
List<String> handles = new ArrayList<String>(driver.getWindowHandles());
int i = 0;
for (String handle : handles) {
if (i == 1) {
driver.switchTo().window(handle);
Alert alert = driver.switchTo().alert();
alert.accept();
}
i++;
}
The problem I have is that I'm getting a TimeoutException on this line:
List<String> handles = new ArrayList<String>(driver.getWindowHandles());
Exception:
.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
The same problem was described in this thread. Unfortunately, there was no solution to this issue and due to the lack of reputation I couldn't comment there.
I tried multiple solutions.
- I inserted implicit wait before getting window handles to make sure the content is there.
- I tried try-catch block on the getWindowHandles to repeat the operation in the catch block.
- I tried to retrieve the alert without switching to the new window.
Unfortunately each time I got the TimeoutException.
I'm trying to automate a scenario where I click on an action that launches a pop up window, but this new window doesn't load, because it throws a browser alert (that's expected).
Alert
I need to verify that this alert is presented. This is my code:
WebDriver driver = SeleniumSession.get().getWrappedDriver();
List<String> handles = new ArrayList<String>(driver.getWindowHandles());
int i = 0;
for (String handle : handles) {
if (i == 1) {
driver.switchTo().window(handle);
Alert alert = driver.switchTo().alert();
alert.accept();
}
i++;
}
The problem I have is that I'm getting a TimeoutException on this line:
List<String> handles = new ArrayList<String>(driver.getWindowHandles());
Exception:
.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException
The same problem was described in this thread. Unfortunately, there was no solution to this issue and due to the lack of reputation I couldn't comment there.
I tried multiple solutions.
- I inserted implicit wait before getting window handles to make sure the content is there.
- I tried try-catch block on the getWindowHandles to repeat the operation in the catch block.
- I tried to retrieve the alert without switching to the new window.
Unfortunately each time I got the TimeoutException.
Share Improve this question edited Mar 27 at 17:00 JeffC 25.9k5 gold badges34 silver badges55 bronze badges asked Mar 27 at 16:42 TisajaTisaja 112 bronze badges 2- since the default unHandledPromptBehaviour is "Dismiss and Notify" (dismiss closes the alert, notify throws an exception) you can simply try/catch the next driver call. (in this case it's switching to the new tab/window) If you catch the unexpected alert exception, you know that there was an alert there. You can then switch to it again. You could also change the unHandledPromptBehaviour of the driver so that it doesn't "notify" or "dismiss" if you wanted to interact with the alert instead. – browsermator Commented Mar 27 at 20:19
- I guess that's the only option :/ I don't like this solution, because it takes a lot of time until the TimeoutException is thrown. But I can make the test pass with this try-catch. – Tisaja Commented Mar 28 at 9:02
2 Answers
Reset to default 0From what you are describing, the new window never shows up so you can't switch to a new window if it doesn't exist. You should be able to confirm that the correct alert dialog is up, dismiss it, and end the test or whatever.
Something like the below should work...
WebDriver driver = SeleniumSession.get().getWrappedDriver();
// click the action
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
// Assert alert.getText() to validate the alert message
// alert.accept() the alert
I was able to make the test pass with this code:
boolean result = false;
try {
WebDriver driver = SeleniumSession.get().getWrappedDriver();
ArrayList<String> handles = new ArrayList<String>(driver.getWindowHandles());
int i = 0;
for (String handle : handles) {
if(i == 1 ) {
driver.switchTo().window(handle);
Alert alert = driver.switchTo().alert();
alert.accept();
result = true;
}
i++;
}
} catch(TimeoutException | UnhandledAlertException e) {
result = true;
}
I don't like it, because I have to wait 160 seconds for the timeout to be thrown, but so far that's the only way to handle it.
本文标签: javaHow to switch to the new window which immediately throws an alertStack Overflow
版权声明:本文标题:java - How to switch to the new window which immediately throws an alert? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744079131a2529909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论