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.