0

I know how to open multiple URLs concurrently on different browsers (Selenium parallel testing on multiple browsers (JAVA)). However my requirement is to open multiple URLs in different tabs but using only one browser. Is there a way to do it?
This is what i tried, but I am unable to open a new tab.

//mainclass
public static void main(String a[])
{
    WebDriver wd = BrowserClass.getWebDriver();
    Thread t1 = new Thread(new ParallelOpenUrl(wd));
    t1.setName("https://www.google.com");
    Thread t2 = new Thread(new ParallelOpenUrl(wd));
    t2.setName("https://www.manageengine.com");
    Thread t3 = new Thread(new ParallelOpenUrl(wd));
    t3.setName("https://www.zoho.com");
    t1.start();
    t2.start();
    t3.start();
}

public class ParallelOpenUrl implements Runnable 
{
    private WebDriver wd = null;
    public ParallelOpenUrl(WebDriver wd)
    {
        this.wd = wd;
    }
    @Override
    public void run() 
    {
        try
        {
            BrowserClass.openTab(wd);
            Thread.sleep(1000);
            wd.switchTo().window(BrowserClass.getCurrentTab());
            wd.get(Thread.currentThread().getName());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        finally
        {
            wd.close();
        }
    }
}


//BrowserClass
public static String getCurrentTab(WebDriver wd)
{   
    Set<String> allTabs = wd.getWindowHandles();
    ArrayList<String> tabs = Lists.newArrayList(allTabs);
    return tabs.get(tabs.size()-1);
}
public static WebDriver getWebDriver()
{
    private WebDriver wd = null;
    if(wd == null)
    {
        System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
        wd = new ChromeDriver();
    }
    return wd;
}
public static void openTab() throws Exception
{
    Robot rb = new Robot();
    rb.keyPress(KeyEvent.VK_CONTROL);
    rb.keyPress(KeyEvent.VK_T);
    rb.keyRelease(KeyEvent.VK_CONTROL);
    rb.keyRelease(KeyEvent.VK_T);       
}

The browser opens the tabs concurrently. But only one tab is active at a time. That is, only after tab 1 has finished loading google.com, tab 2 starts loading zoho.com.

Prasanth Ganesan
  • 541
  • 4
  • 14
  • From my knowledge(of Selenium) you can't done this with only one instance of WebDriver. – KunLun Jan 04 '19 at 06:55
  • What's the meaning of "it is not working"? – pburgr Jan 04 '19 at 07:19
  • @pburgr I am unable to open a new tab. – Prasanth Ganesan Jan 04 '19 at 09:10
  • Why would you need multithreading to open different tabs in the same browser window? – SnR Jan 04 '19 at 09:22
  • @SnR I am running selenium on a remote grid. Remote grid allows me to open only 5 browsers at a time. To over come this, I am trying to do the operation with only one browser. – Prasanth Ganesan Jan 04 '19 at 09:31
  • @PrasanthGanesan If you use multithreading on multiple tabs of same browser, then how your test case will run properly at the same time? meaning each thread will try to switch to its tab & a complete test case can't be completed right? – SnR Jan 04 '19 at 09:42
  • I am not using this for testing. I am using this to measure response time of the URLs. – Prasanth Ganesan Jan 04 '19 at 10:09

2 Answers2

1

You can try using javascript for opening your url in the new tab :

driver.executescript("window.open(\"your url\", '_blank');")
SnR
  • 319
  • 1
  • 7
0

The small Thread.sleep() is needed when opening new tab and when switching to the new tab.

public void openMultipleURLs() throws InterruptedException, AWTException {
            for (String url: URLs) {
                openNewTab(driver);
                switchToNewTab();
                driver.get(url);
                // do something on each web
            }
        }

    public String[] URLs = {
            "https://www.google.com", "https://www.stackoverflow.com", "https://www.microsoft.com"
    };

    public void switchToNewTab() throws AWTException, InterruptedException {
        ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
        Thread.sleep(500);
        int newTabIndex = winHandles.size();
        driver.switchTo().window(winHandles.get(newTabIndex - 1));
    }

    public void openNewTab(WebDriver driver) throws AWTException, InterruptedException {
        Robot rob = new Robot();
        rob.keyPress(KeyEvent.VK_CONTROL);
        rob.keyPress(KeyEvent.VK_T);
        rob.keyRelease(KeyEvent.VK_CONTROL);
        rob.keyRelease(KeyEvent.VK_T);
        Thread.sleep(100);
    }
pburgr
  • 1,722
  • 1
  • 11
  • 26