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 

3 comments:

  1. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.



    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete

Selenium4: Working with two parallel windows in Selenium Automation

A new method newWindow() is introduced in Selenium 4 (Alpha). Using this method we can open a new tab or new window from the current window...