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 

13 comments:

  1. Replies
    1. Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important. Project Center in Chennai It’s what organizations do with the data that matters. Big data can be analyzed for insights that lead to better decisions and strategic business moves.

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework 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


      The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. 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
  3. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  4. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    360Digitmg marketing analytics in hyderabad

    ReplyDelete
  5. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Data-science online course in chennai

    ReplyDelete
  6. Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.
    Data Science Institute in Bangalore

    ReplyDelete
  7. Hi buddies, it is great written piece entirely defined, continue the good work constantly.

    Data Science Course

    ReplyDelete
  8. It is extremely nice to see the greatest details presented in an easy and understanding manner.

    Data Science Training

    ReplyDelete
  9. Giving comments on a significant blog is a form of art. I am entirely moved by this piece of writing. I wish to interpret more from you.
    Data Science training in Mumbai
    Data Science course in Mumbai
    SAP training in Mumbai

    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...