Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using selenium web driver to run test on multiple browsers

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']

for bro in bros:
    print "Running "+bro+"\n"
    browser = webdriver.Remote(
                    command_executor='http://10.236.194.218:4444/wd/hub',
                    desired_capabilities={'browserName': bro,
                                          'javascriptEnabled': True})
    browser.implicitly_wait(60000)
    browser.get("http://10.236.194.156")

One interesting observation; when I include the parameter platform: WINDOWS it's running only on Internet Explorer.

Does Selenium Webdriver works this way or my understanding is wrong?

like image 749
Prakash Avatar asked Feb 26 '12 13:02

Prakash


2 Answers

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!

like image 196
Boccobrock Avatar answered Oct 14 '22 16:10

Boccobrock


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.

like image 39
arket Avatar answered Oct 14 '22 17:10

arket