So you have a popup focused on creating a new user.
Your Selenium test fills in the new user details and then clicks the SAVE button.
Clicking the SAVE button closes the popup.
Before continuing to interact with the page, you check first that the popup is closed.
Your code may look like this:
WebElement saveButton = driver.findElement(SAVE_BUTTON_ID);
saveButton.click();
boolean isPopupDisplayed = true;
try {
isPopupDisplayed = driver.findElement(POPUP_TITLE_ID).isDisplayed();
}
catch (Exception e) {
isPopupDisplayed = false;
}
if (isPopupDisplayed)
throw new PopupNotHiddenException("the popup is still displayed!");
The code finds the save button and clicks it.
Then, it finds the popup title.
If the popup title can be found, isPopupDisplayed gets the true or false value. If the popup title cannot be found, an exception is generated and caught so that isPopupDisplayed becomes false.
If isPopupDisplayed is still true, an exception is generated since the popup is still visible.
There are multiple problems with this code:
it uses try/catch statement
it uses if statement
it does not work at all if the popup is closed slowly
Keep reading with a 7-day free trial
Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.