Friday, 6 January 2017

How to Open New Tab with Selenium WebDriver

Selenium WebDriver  tool do not have any built In API using which we can directly use to open new tab. Normally we are using CTRL + t Keys to open new tab In Browser and cltr + tab to switch between the tabs. We will use this in our selenium test case to open another tab inside a browser and switch to tab. 

Check out the Example below in details to understand how to open a new tab, how to open particular url in the new tab, how to switch between two tabs  and how to close the tabs.




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

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class BromiumTests {

 WebDriver driver;
 Utils util;

 @BeforeTest
 public void setUp() {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
 }

 
 
 @Test
 public void testOpenYoutube() {
  driver.get("https://www.youtube.com");
  
  driver.get("https://www.youtube.com/watch?v=9NXEnGiOeUU");
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 }
        @Test
 public void testOpenGmail() {
  switchToNewTab();
  driver.get("http://www.gmail.com");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }
       public void openNewTab() {
  ((JavascriptExecutor) driver).executeScript("window.open('about:blank','_blank');");
 }

 public void switchToNewTab() {
  openNewTab();
  String subWindowHandler = null;

  Set<String> handles = driver.getWindowHandles();
  Iterator<String> iterator = handles.iterator();
  while (iterator.hasNext()) {
   subWindowHandler = iterator.next();
  }
  driver.switchTo().window(subWindowHandler);
 }

 @AfterClass
 public void tearDown() {
  driver.quit();
 }
 

}

14 comments:

  1. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

    rpa Training in Bangalore

    ReplyDelete
  2. Nice Article, Please do post this kind of posts more, thanks.
    I wish you might like to have a look at my article http://javaseleniumworld.com/robot-class/

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

    ReplyDelete
  4. UiPath Training in Bangalore by myTectra is one the best UiPath Training. myTectra is the market leader in providing Robotic Process Automation on UiPath
    ui path training in bangalore

    ReplyDelete
  5. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Sql&plsql Online Training

    workday Online Training

    SAP GTS Online Training

    Oracle BPM Online Training

    Oracle SCM Online Training

    ReplyDelete
  6. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Splunk Classes

    Tableau Classes

    ReplyDelete
  7. This is good piece of writing and pleasant urging commented
    here, I am really enjoying by these.

    ReplyDelete
  8. Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks for sharing.

    Custom Web Application Development

    ReplyDelete
  9. Do not think! Just come with us to BGAOC and play with us. best casino games come to us and win soon.

    ReplyDelete
  10. buy soma pill 350mg
    Soma pill 350mg has carisoprodol that effectively treats acute pain in people. Pain is felt by almost everyone at some point of their life. This can at times make the life of the person very difficult. Soma pill 350mg has helped people all over the world. So use soma pill 350mg and get rid of all your body pain. Buy soma pill online and have a healthy pain free life.

    ReplyDelete