Using selenium web driver to run test on multiple browsers

后端 未结 7 972
时光说笑
时光说笑 2021-02-04 19:42

I\'m trying to run a same test across multiple browsers through for loop but it always run only on Firefox.

bros = [\'FIREFOX\',\'CHROME\',\'INTERNET EXPLORER\']         


        
7条回答
  •  借酒劲吻你
    2021-02-04 20:14

    I actually have done this in java, the following works well for me:

    ...
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    ...
    
    DesiredCapabilities[] browsers = {DesiredCapabilities.firefox(),DesiredCapabilities.chrome(),DesiredCapabilities.internetExplorer()};
        for(DesiredCapabilities browser : browsers)
        {
            try{
                System.out.println("Testing in Browser: "+browser.getBrowserName());
                driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), browser);
                ...
    

    You will need to adapt this of course if you're writing your tests in a different language, I know it's possible in Java, not sure about otherwise.

    Also, I agree with what you're trying to do, I think it is much better to have a class that runs the same tests with different browsers, instead of duplicating code many times over and being inelegant. If you are doing this in Java/other codes, I also highly suggest using a Page Object.

    Good luck!

提交回复
热议问题