Handling Pop-ups in Web Driver
In the web application there will be different types of pop-ups such as:
- Alerts pop-up.
- Confirmation pop-up.
- Hidden division pop-up.
- Page on-load pop-up.
- File download pop-up.
- Child browser pop-up.
Depending upon the type of pop-up the web driver code changes.
Alert Pop-up
In order to check whether the pop-up is alert or not open the web application through "WebDriver" and if you can move the pop-up and the pop-up contains only one button then it will be alert pop-up.
By default control will be in the web page. In order to handle the alert we should make the webdriver to switch from browser to the alert pop-up. This can be done using following statement:
Alert alert = driver.switchTo(0.alert();
In order to get the information displayed on the alert we use getText().
Example:
string aMsg = alert.getText();
System.out.println(aMsg);
When the alert is closed the control will be automatically transfer to main browser.
Handling Confirmation pop-up
If the pop-up displayed in the browser which is opened by the "WebDriver" has two buttons(OK & Cancel) and we can move that pop-up, then it is called as Confirmation pop-up.
Handling Confirmation pop-up is same as the alert pop-up and we also use dismis() to click on cancel button as shown below:
Alert alert = driver.switchTo().alert();
String aMsg = alert.getText();
System.out.println(aMsg);
alert.dismis();
Hidden division pop-up
If the pop-up displayed in the browser opened by the "WebDriver" is colorful, we can't move it and also we can't inspect it then it is a hidden pop-up.
It is created by the developer by using driver, by default it will be hidden. It will be made visible whenever it is required by changing value of display property from none to block. Since hidden division pop-up is a part of web page only so we don't use switchTo() to identify the buttons present on the hidden division using findElement() only.
Example:
String divMsg = driver.findElement(By.id("exitconfirmDiv")).getText();
System.out.println(divMsg);
Handling Page on-load pop-up
The pop-up which is displayed when we enter the URL(http://192.168.1.1) which will ask for authentication(User name & Password). When we specify the URL for "get()" it will try to open that web page where it will display the page on load pop-up. Till we enter the user name and password and click "OK" the page will not be loaded and get() will not allow us to execute any other statement.
In order to handle page on load pop-up we specify the user name and password as a part of URL itself:
Example:
WebDriver driver = new FireFoxDriver();
driver.get("http://admin:admin@192.168.1.1");
Handling File download pop-up
Web driver will not identify the file download pop-up and we can't perform any operation on the file download pop-up but by writing the code we can change the setting of Mozilla Firefox so that it will not display the file download pop-up instead of that it will save the required file in specified location.
Steps to change the setting manually:
- Open Mozilla Firefox browser.
- Type above:config in the address bar.
- Then click on I will be careful, I Promise.
- In the search field type "Save to disk" which will display the following preference "browser.helperApps.neverAsk.SaveToDisk". Double click it which will display a pop-up where we can change the setting. It accepts a string value which is of type "MIME"(Multipurpose Internet Mail Extension).
Child browser pop-up
If the child browser is displayed in order to perform any operation on the child browser we need to make the web driver to switch from parent browser to child browser.
In order to do this we use "driver.switchTo.Window()" which takes window handle of the child browser. In order to get window handle of the child browser first we get window handle of all the browsers using "getWindowHandles()" of WebDriver.
After switching to child browser if we use any method such as findElement(), close() etc, it will be executed on the child browser. If we close the child browser then we must switch back to parent browser by specifying window handle of the parent browser as a argument to "driver.switchTo().window()".
0 Comment(s)