Tuesday, 22 August 2017

How to handle the windows, frames and pop up in Selenium WebDriver?

In this post, I will explain that how do you handle the multiple windows, frames, and pop-up of an application using Selenium WebDriver. You generally faced these types of a problem of frames, windows, and pop-up in an application. Different tabs are also treated as different windows. In software automation, you need to tell the Selenium WebDriver that where it will search for the element. In which frame window or pop up.


So to handle this situation selenium WebDriver has native methods. I have mentioned below different ways to handle the windows, frames, and pop-up.


How to handle Frames:-

1) Select frame by element
Ex:- driver.switchTo().frame(element);
2) Select frame by id:-
Ex:- driver.switchTo().frame(id);
3) Select frame by index:-
Ex:- driver.switchTo().frame(index);
How to handle Windows:-

1) Select Window From Index
Set<String> windows = driver.getWindowHandles();
System.out.println("Window Size::" + windows.size());
String wins[] = windows.toArray(new String[windows.size()]);
driver.switchTo().window(wins[WindowCountToSwitch]);


2) Close Window From Index
                      Set<String> windows = driver.getWindowHandles();
System.out.println("Window Size::" + windows.size());
String wins[] = windows.toArray(new String[windows.size()]);
driver.switchTo().window(wins[WindowCountToSwitch]);
driver.close();
Note:- WindowCountToSwitch is the count of the window to select or delete


How to handle the Pop Up:-


1)Handle pop Up:-
                       Alert alert = driver.switchTo().alert();
alert.accept();


2)Get Alert Text:-
                      Alert alert = driver.switchTo().alert();
            String message = alert.getText();


3)check for the visibility of Alert:-
                       WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.alertIsPresent());

Switch to Default/main window or frame:-
driver.switchTo().defaultContent();

Thank you for visiting my blog. Keep in touch.

Labels: , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home