Using selenium web driver to run test on multiple browsers

后端 未结 7 986
时光说笑
时光说笑 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:13

    Have you considered using the composite design pattern to create a CompositeWebDriver that actually runs multiple component WebDriver (such as chrome, gecko,...)? To this end, you would extend the WebDriver class with a new one (e.g. CompositeWebDriver) that just delegates his calls to all the actual WebDrivers.

    This could also be done with various instances of RemoteWebDriver as components.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-04 20:21

    So if I got you right, you have one testcase and want this to be tested against different browsers.

    I don't think a loop is a good idea even if it's possible (I don't know atm).

    The idea is to be able to test every testcase standalone on the run with a specific browser (thats the JUnit philosophy), not to run all in order to get to that specific browser .

    So you need to create a WebDriver with the specific browser and the specific testcase.

    I suggest you seperate testcases by creating a testcase-class file for each browser.

    Like: FirefoxTestOne.java, IeTestOne.java, ChromeTestOne.java .

    Note that you can add multiple firefox tests in the FirefoxTestOne without problems. Theres no guarantee that they will be executed in a particular order through (JUnit philosophy).

    For links and tutorials ask google. There are already looooots of examples written.

    0 讨论(0)
  • 2021-02-04 20:26

    This way (attached url) worked for me.

    http://blog.varunin.com/2011/07/running-selenium-tests-on-different.html

    The following point is different from the example.

    @Parameters
    public static List data() {
        return Arrays.asList(new Object[][]{{"firefox"},{"ie"}});
    }
    @Before
    public void setUp() throws Exception {
        System.out.println("browser: " + browser);
        if(browser.equalsIgnoreCase("ie")) {
            System.setProperty("webdriver.ie.driver", "IEDriverServer64.exe");
            driver = new InternetExplorerDriver();
        } else if(browser.equalsIgnoreCase("firefox")) {
            driver = new FirefoxDriver();
    
    0 讨论(0)
  • 2021-02-04 20:27

    As Coretek said you need multiple webdriver instances. You will need to run the selenium-server .jar file and provide each one with an argument specifying the browser you want that instance of the server to run.

    The argument for Internet Explorer is *iexplore, the argument for firefox is *firefox and the argument for chrome is *chrome. These are -forcedBrowserMode arguments. Otherwise selenium won't know what it should be running against. You may need to use *iexploreProxy for your tests, sometimes it works better than the *iexplore mode.

    Check out this link for more arguments that may be useful:

    http://seleniumforum.forumotion.net/t89-selenium-server-command-options-while-starting-server

    0 讨论(0)
  • You can use TestNG for this combination of selenium + testng gives you a batter result for this just by adding parameters attribute you can do this

    0 讨论(0)
提交回复
热议问题