There are situations in which we get browser popups or multiple browser windows, when we open an URL or click on an element on browser.
Note – popups can be blocked in browser using some browser add-ons, but for testing purpose, sometimes we need to allow the popups or multiple windows.
A single instance of selenium web-driver can handle multiple popups and browser windows.
Let’s see how web-driver handles these above mentioned popups or browser windows.
Selenium provides 2 methods getWindowHandle() and getWindowHandles() to deal with the multiple browser windows.
getWindowHandle() -
This method returns the current browser window handle id that the web-driver is currently holding.
getWindowHandles() -
This method returns set of browser window handle ids that are invoked/opened by the web-driver, each browser window is corresponds to one id, so we can loop over the Set and work on each browser and close.
and we use switchTo().window(window_handle_id)
to switch to particular browser window.
Like we can keep the main windows handle id in a string, so we can switch back to main window after doing some testing on other browser windows opened.
String mainWindow = driver.getWindowHandle();
After working on all other windows, we can switch back to main window as –
driver.switchTo().window(mainTab);
Below is the code implementation and comments of each line explanation
import java.util.Iterator; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class WindowHandling { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "/Users/userName/sel/chromedriver"); WebDriver driver=new ChromeDriver(); driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS); driver.get("https://skpatro.github.io/demo/links/"); driver.findElement(By.name("NewWindow")).click(); Set<String> windows = driver.getWindowHandles(); //s holds the set of all browser windows System.out.println("No. of browser windows opened - " + windows.size()); for(String window : windows){ if(!mainWindow.equals(window)) { //skip main window switching now driver.switchTo().window(window); System.out.println(driver.getCurrentUrl()); System.out.println(driver.getTitle()); } } driver.switchTo().window(mainWindow); //return back to main window //do some testing on main window driver.quit(); } }
So in above scenario, we have 2 windows one is actual browser screen while launching app, and the extra browser popped up on new tab.
Sometimes we can have more than 2 windows as well.
We skipped switching to main window and switched to other windows and fetched the URL and title of browser window [you can test anything on the screen]
then switched back to main window.
Another way also we can handle using Iterator –
Iterator<String> iter = windows.iterator(); String maintab=(String) iter.next();//maintab holds the 1st browser window String childtab=(String) iter.next();//childtab holds the next browser window driver.switchTo().window(childtab); //move to child browser window System.out.println("childtab " + driver.getTitle()); driver.close(); driver.switchTo().window(maintab); //move to parent browser window System.out.println("maintab " + d.getTitle());
In the above case, we have 2 browser popups / windows, so we handled as above,
Let’s say we have multiple windows like 2 or more, then you can loop the Set and perform some action in each iteration.
Iterator<String> iter = windows.iterator(); while(iter.hasNext()) { if(!mainWindow.equals(iter.next()){ driver.switchTo().window(iter.next()); System.out.println("window - " + driver.getTitle()); //do any other actions for this browser window } } driver.switchTo().window(maintab); //move to parent browser window System.out.println("maintab " + d.getTitle());
Hope that helps!