在同一浏览器Selenium(JAVA)中同时打开多个URL

时间:2019-01-04 06:51:48

标签: java multithreading selenium

我知道如何在不同的浏览器(Selenium parallel testing on multiple browsers (JAVA))上同时打开多个URL。但是,我的要求是在不同的选项卡中打开多个URL,但只能使用一个浏览器。有办法吗?
这是我尝试过的方法,但是无法打开新标签。

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

浏览器同时打开选项卡。但是一次仅一个选项卡处于活动状态。也就是说,只有在标签1完成加载google.com后,标签2才开始加载zoho.com。

2 个答案:

答案 0 :(得分:0)

打开新标签页并切换到新标签页时,需要小的Thread.sleep()

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

答案 1 :(得分:0)

您可以尝试使用javascript在新标签页中打开您的网址:

driver.executescript("window.open(\"your url\", '_blank');")