Selenium parallel testing on multiple browsers (JA

2019-08-01 15:44发布

I was surprised not to find any intelligent solution how run Selenium webdriver tests using Selenium Grid but running each test with multiple browsers. Preferably I'd like to have some kind of configuration (file, or hard coded) where I can specify all browsers I want my tests to run. And then each test would be run on each of those browsers.

I assume it is possible to write your own testrunner and place a loop iterating each test rung through all the browsers. But maybe someone knows a more elegant solution? Anyone has done this?

P.S. I have found solutions which advise duplicating the tests and specifying browser parameters for each test. I don't want that.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-01 16:02

What I do is run my test at class level then create a TestNG.xml then inside there specify what classes I wish to run and what browsers they should run on. So my TestNG file would look something like:

    <?xml version="1.0" encoding="UTF-8"?>
<suite name = "suite1" verbose = "6" preserve-order="true" parallel = "false" thread-count="1">

    <test name = "Any Test">
    <parameter name = "browser" value ="chrome">
    <parameter name = "port" value = "5555">
    </parameter>
    </parameter>
        <classes>
             <class name = "name of class to run"/>      
        </classes>
    </test>
</suite>

Then because I'm running on Selenium Grid I pass parameters for browser and port in my code like so:

@BeforeMethod()
    @Parameters({"browser","port"})
    public void launchBrowsers(String browser, String port) throws Exception {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setBrowserName(browser);
        capabilities.setJavascriptEnabled(true);

        setSelenium(new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capabilities));

        getSelenium().get(baseUrl); 
        getSelenium().manage().window().maximize();             
    }

Hope this helps

查看更多
Explosion°爆炸
3楼-- · 2019-08-01 16:06

I have solved this in a way that I specify different browser parameter for each TestNG suite.

查看更多
看我几分像从前
4楼-- · 2019-08-01 16:17

I'm unfamiliar with Selenium Grid, but I know you can have Selenium open multiple browsers simultaneously by running each test on a different thread. You may want to look in to this.

查看更多
登录 后发表回答