Selenium WebDriver: Open Multiple Windows Or Tabs

There are three methods to open multiple windows in selenium. 

  1. Use different webdriver object, each object open one browser.
  2. Use one webdriver to open multiple browser window.
  3. Use one webdriver to open multiple tab.

The first method will cost too much resource. So we will only use the last two methods. You can download the attachment for detail java code example.

Open Multiple Window Use One WebDriver

  1. Execute javascript window.open(url) to open a new one.
  2. We will open 6 window in below java example code.
  3. Handle all the opened window in java code.
  4. Use WebDriver.switchTo().window(String windowHandler) to switch between them.
  5. Change the second one’s url to http://www.google.com
  6. Close the first one.
    	  @Test
    	  public void openSameUrlInMultipleWindow() throws InterruptedException {
    		  
    		  String pageUrl = "http://www.bing.com";
    		  this.ffDriver.get(pageUrl);
    		  
    		  /* Cast webdriver object to Javascript Executor object. */
    		  JavascriptExecutor jsExecutor = (JavascriptExecutor)this.ffDriver;
    		  
    		  /* Javascript that will create new FirefoxWindow. */
    		  String jsOpenNewWindow = "window.open('"+pageUrl+"');";
    		  /* Run above javascript. */
    		  for(int i=0;i<6;i++)
    		  {
    			  jsExecutor.executeScript(jsOpenNewWindow);
    			  Thread.sleep(1000);
    			  System.out.println("One opennd.");
    		  }
    
    		  Set<String> windowHandleSet = this.ffDriver.getWindowHandles();
    		  if(windowHandleSet!=null)
    		  {
    			  Iterator<String> it = windowHandleSet.iterator();
    			  
    			  int i = 0;
    			  while(it.hasNext())
    			  {
    				  String windowHandleStr = it.next();
    				  System.out.println("WindowHandler String : " + windowHandleStr);
    				  
    				  /* Change the third FirefoxWindow page url to http://www.quora.com*/
    				  if(i==2)
    				  {
    					  this.ffDriver.switchTo().window(windowHandleStr);
    					  this.ffDriver.get("http://www.google.com");
    					  System.out.println("Switch FirefoxWindow " + windowHandleStr + " to http://www.google.com");
    					  Thread.sleep(3000);
    				  }
    				  i++;
    				  
    				  /* Close the first window. */
    				  if(i==1)
    				  {
    					  this.ffDriver.switchTo().window(windowHandleStr);
    					  /*Only close current window.*/
    					  this.ffDriver.close();
    					  System.out.println("Close " + windowHandleStr);
    					  Thread.sleep(3000);
    				  }
    			  }  
    		  }
    	  }

Open Multiple Tab

  1. Find html body web element.
  2. Send “Ctrl+t” command to body element to open new tab.
  3. Send “Ctrl+2” to navigate to the second.
  4. Change the url to google.com for the second.
  5. Get the body web element in the second tab.
  6. Send “Ctrl+3” to navigate to third.
  7. Send “Ctrl+w” to close the third tab.
    	  @Test
    	  public void openSameUrlInMultipleTab() throws InterruptedException {
    		  
    		  String pageUrl = "http://www.bing.com";
    		  this.ffDriver.get(pageUrl);
    		  
    		  String currWindowHandleStr = this.ffDriver.getWindowHandle();
    		  System.out.println("Current windowhandler before create tab : " + currWindowHandleStr);
    		  
    		  /* Get html body webelement. */
    		  WebElement bodyElement = this.ffDriver.findElement(By.tagName("body"));
    		 
    		  /* Send Ctrl+t to create newtab. */
    		  for(int i=0;i<6;i++)
    		  {
    			  bodyElement.sendKeys(Keys.CONTROL + "t");
    			  Thread.sleep(1000);
    			  System.out.println("Open a newtab.");
    		  }
    		  
    		 
    		  /* Change tab 2 's page url tp http://www.google.com */
    		  bodyElement.sendKeys(Keys.CONTROL+"2");
    		  this.ffDriver.get("http://www.google.com");
    		  Thread.sleep(3000);
    		  
    		  currWindowHandleStr = this.ffDriver.getWindowHandle();
    		  System.out.println("Current windowhandler after go to tab 2 : " + currWindowHandleStr);
    		  
    		  /* Close tab 3.*/
    		  /* First need to find the body web element in currenttab. */
    		  bodyElement = this.ffDriver.findElement(By.tagName("body"));
    		  /* Go to tab 3. */
    		  bodyElement.sendKeys(Keys.CONTROL+"3");
    		  this.ffDriver.get("http://www.twitter.com");
    		  Thread.sleep(3000);
    		  /* Close currenttab. */
    		  bodyElement.sendKeys(Keys.CONTROL+"w");
    		  Thread.sleep(3000);
    	  }

The firefox browser’s initiate and destroy will be implemented in @BeforeClass method and @AfterClass method. The java code can be run in TestNG.

READ :   Selenium Webdriver Handle Firefox Or Chrome Download Popup

Download “TestOpenSamePageUrlInMultipleWindow.zip” TestOpenSamePageUrlInMultipleWindow.zip – Downloaded 82 times – 1 KB

(Visited 5,001 times, 3 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.