We will discuss about working with frames and popup windows in a separate post.
You will have to explicitly close the popup boxes before proceeding to normal Selenium commands in your test scripts.
When an alert box is pops up you have to click the OK button to proceed to the next command.
When a confirm box popup you will have to click either Cancel or OK button before proceeding to next Selenium command.
We can run simple code like
System.out.println("Test"); System.out.println(new Date());
But we cant run any input output code while alert is present
Scanner scanner=new Scanner(System.in); System.out.println("Enter your name"); String name=sc.nextLine(); System.out.println(name); scanner.close();
When a prompt box popup you can enter (type) an input value into the input box [Optional] and click the OK button or Cancel button to proceed.
Managing Popup boxes
You will have to switch to the popup box to working with them.
You can switch to the alert and have control over it.
Alert alert = webDriver.switchTo.alert();
Clicking the OK button
You can call the accept method in the Alert interface to click the OK button.
Clicking the Cancel button
You can call the dismiss method to click the Cancel button.
Typing text into an input box
You can call the sendKeys method to type text into the input box in a prompt.
Getting the message in a popup
String msgJSPopup = webDriver.switchTo.alert().getText();
Wait for a Popup box
A popup box may not be available immediately when you try to work with them. You may have to wait till the popup box is present in the webpage. Following could be used for waiting 15 seconds until a popup box is present.
WebDriverWait wait = new WebDriverWait(webDriver, 15); wait.until(ExpectedConditions.alertIsPresent());
Timing Alert example
Following code will show how to close a timing alert.
WebDriverWait wait = new WebDriverWait(driver, 9000); wait.until(ExpectedConditions.alertIsPresent()); Alert timingAlert = driver.switchTo().alert(); timingAlert.dismiss();
Handling unexpected Popup boxes
You can configure the chrome capabilities to handle them by default.
To ACCEPT the unexpected
To IGNORE the unexpected
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.INGORE );
To DISMISS the unexpected
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.DISMISS );
When an Alert (or popup box) is not present
You may get the following error message when you try to execute Alert methods when the expected popup window is not present yet. Please see the wait for a popup box section for a solution.
When an Alert is open and try to open another alert or click button
When an Alert is open and try to close webdriver (Only Firefox)
2.Alert. 2018. Alert. [ONLINE] Available at: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/Alert.html. [Accessed 05 February 2018].
3.WebDriverWait. 2018. WebDriverWait. [ONLINE] Available at: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html. [Accessed 05 February 2018].