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. 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
  2. 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
  3. Hi buddies, it is great written piece entirely defined, continue the good work constantly.

    Data Science Course

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

    Data Science Training

    ReplyDelete
  5. Good post found to be very impressive while going through this post which simplifies shifting from the current tab to the next tab in a very simple manner. Thanks for sharing and keep posting such an informative content.

    360DigiTMG Digital Marketing Course

    ReplyDelete
  6. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good work Sentiment Analysis

    ReplyDelete
  7. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. cable Mesh manufacturers in Pakistan

    ReplyDelete
  8. Good Post with a lot of useful questions, keep updating the list and thanks for sharing with us.
    Good know more about selenium

    ReplyDelete
  9. It is extremely nice to see the greatest details presented in an easy and understanding .
    selenium
    language.

    ReplyDelete
  10. Daftar Poker Online
    SENIKARTU merupakan situs poker online yang menyediakan berbagai permainan kartu dengan winrate tertinggi 99persen
    1 user bisa memainkan 9 permainan: poker online , bandar poker , domino99 , aduq , bandarq , capsasusun , sakong ,bandar66 , perang baccarat

    Nikmati BONUS TERBESAR
    -minimal depo / WD : Rp 10.000
    -Bonus Turnover : 0,3persen ( dibagikan setiap hari )
    -Bonus Refferal 20persen ( berlaku seumur hidup )

    Silahkan hubungi kami :
    link : 54.254.143.75
    link : http://poker-pool.com

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Excellent article, I really liked this article on software development which is indeed very helpful and knowledgeable. The information about How to switch to new tab in selenium Automation ? You provided was very helpful. I’m very grateful that I found this article at right time so that I can recommend it to others. I have been learning software engineering from Gayatri Mishra, who is also a coach and she truly is a inspiration for others, her classes was indeed helpful and useful and also learned about few software development from there.

    ReplyDelete
  13. Excellent article, I really liked this article on software development which is indeed very helpful and knowledgeable. The information about How to switch to new tab in selenium Automation ? You provided was very helpful. I’m very grateful that I found this article at right time so that I can recommend it to others. I have been learning software engineering from Gayatri Mishra, who is also a coach and she truly is a inspiration for others, her classes was indeed helpful and useful and also learned about few software development from there.

    ReplyDelete

DevOps 01: What is DevOps and How it benefits organizations?

DevOps is a culture in an organization, where the development team and operations team help each other by sharing information, process and t...