Saturday, February 9, 2019

How to switch to new tab in Selenium automation?

You might have seen an application in which clicking on a link opens another web page/application in the new tab, and performing some activity on the application that opened in the new tab.

We will see automating above activity using Selenium WebDriver. Consider below application (QA BABU Demo Page) which as "Google" link clicking on this link will open Google search page in the new tab.



Lets automate it now.

Step 1: Launch "file:///D:/html/newTabScript.html" (this is a local file) in chrome browser using Selenium Web Driver. You can download the file from here

//set browser driver path 
  System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe");
  //instantiate ChromeDriver
  driver = new ChromeDriver();
  //maximize the browser
  driver.manage().window().maximize();
  //implicit wait
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  //launching the application
  driver.get("file:///D:/html/newTabScript.html");

Step 2: Get the parent window handle to String variable using getWindowHandle()

//getting parent window
String parentWindow = driver.getWindowHandle();

Step 3: Now click on the "Google" link, Google search page should be opened in the new tab

Step 4: Get all the window handles to a Set<String> using getWindowHandles(). Now get number of windows present Set<String>

//get all the windows
Set<String> allWindows = driver.getWindowHandles();
//No of windows=2
System.out.println("No of windows or tabs after clicking: " + allWindows.size());

Step 5: Remove parent window handle from the all the window handles, so that Set<> contains only new tab window only in it.

Then switch to the new tab window as shown below.

//removing parent window, 
allWindows.remove(parentWindow);

Iterator<String> ite = allWindows.iterator();
//So now Set contains only new tab window only,so switch to it
driver.switchTo().window((String) ite.next());

Step 6: Enter some text to Google search text field and search, if the driver switched to new tab we will be able to search

driver.findElement(By.name("q")).sendKeys("QABABU");

driver.findElement(By.xpath("//div[@class='FPdoLc VlcLAe']//input[@value='Google Search']")).click();

This is one of the way to switch to new tab in Selenium automation. Please check the demo video below (please watch in full screen mode).



Please check complete program below:

package Tests;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class SwitchingToNewTab {

 WebDriver driver;
 
 @Test
 public void GoogleSearchInNewTab() throws Exception {
  
  //set browser driver path 
  System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\drivers\\chromedriver.exe");
  //instantiate ChromeDriver
  driver = new ChromeDriver();
  //maximize the browser
  driver.manage().window().maximize();
  //implicit wait
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  //launching the application
  driver.get("file:///D:/html/newTabScript.html");
  //getting parent window
  String parentWindow = driver.getWindowHandle();
  //Clicking on the Google link
  driver.findElement(By.name("link")).click();
  //get all the windows
  Set<String> allWindows = driver.getWindowHandles();
  //No of windows=2
  System.out.println("No of windows or tabs after clicking: " + allWindows.size());
  //removing parent window, 
  allWindows.remove(parentWindow);

  Iterator<String> ite = allWindows.iterator();
  //So now Set contains only new tab window only,so switch to it
  driver.switchTo().window((String) ite.next());

  driver.findElement(By.name("q")).sendKeys("QABABU");

  driver.findElement(By.xpath("//div[@class='FPdoLc VlcLAe']//input[@value='Google Search']")).click();
  
  driver.quit();

 }
 
}
Thanks for reading.
#HappyLearning #HappyTesting 

2 comments:

Java Program 5: How to reverse given string and each word of it?

Let us understand the problem with an example, suppose "Learn Core Java" is the given string then the expected output should be &...