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.