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.

7
  • From my knowledge(of Selenium) you can't done this with only one instance of WebDriver. Commented Jan 4, 2019 at 6:55
  • What's the meaning of "it is not working"? Commented Jan 4, 2019 at 7:19
  • @pburgr I am unable to open a new tab. Commented Jan 4, 2019 at 9:10
  • Why would you need multithreading to open different tabs in the same browser window? Commented Jan 4, 2019 at 9: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. Commented Jan 4, 2019 at 9:31

2 Answers 2

1

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

driver.executescript("window.open(\"your url\", '_blank');")
Sign up to request clarification or add additional context in comments.

Comments

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);
    }

4 Comments

Yeah this works, thank you . But all the 3 URLs are opening in the same tab. I to open them in different tabs.
I'm using existing browser profile. In my case I have 4 tabs in total, first is blank.
In my case, The tabs are opening concurrently. But the control reaches 2nd tab, waits for the URL to load. Only then the control reaches the 3rd tab. It is happening sequentially. Not concurrently.
check all the {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.