mndrbbr
mndrbbr

Reputation: 105

How to run one python webdriver test in multiple browsers

I am testing out BrowserStack and have a small suite of Selenium WebDriver tests written in Python. My goal is to run the tests in several different browsers. Currently I am using desired_capabilities to specify the browser, version, os, etc.

What would be a good way to repeat the test with a different browser without having a bunch of different py files?

Here's how the tests are setup:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import unittest, time, re


desired_cap = {'browser': 'Chrome', 'browser_version': '33.0', 'os': 'OS X', 'os_version': 'Mavericks', 'resolution': '1600x1200'}
desired_cap['browserstack.debug'] = True

class RegWD(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Remote(
        command_executor='http://browserstackstuff.com',
        desired_capabilities=desired_cap)
        self.base_url = "http://blahtestsite.com/"

Upvotes: 3

Views: 7731

Answers (1)

ExperimentsWithCode
ExperimentsWithCode

Reputation: 1184

You could try it like this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import unittest, time, re


desired_cap = []
desired_cap.append({'browser': 'Chrome', 'browser_version': '33.0', 'os': 'OS X', 'os_version': 'Mavericks', 'resolution': '1600x1200'})
desired_cap.append({'browser': 'Firefox', 'browser_version': '27.0', 'os': 'OS X', 'os_version': 'Mavericks', 'resolution': '1600x1200'})

class RegWD(unittest.TestCase):
    def setUp(self):
        for driver_instance in desired_cap:
            driver_instance['browserstack.debug'] = True
            self.driver = webdriver.Remote(
            command_executor='http://browserstackstuff.com',
            desired_capabilities=driver_instance)
            self.base_url = "http://blahtestsite.com/"

Just make desired_cap a tuple, and append in all the browser versions you want into it. The add a loop that cycles through each browser instance. I had to move

desired_cap['browserstack.debug'] = True

from outside of class, to the following inside of class

driver_instance['browserstack.debug'] = True

because the brackets make it weird. It needs an integer between the [] to call the specific instance. Rather than making a loop outside of class to set each instance to True, I just moved the line into the class so it runs for each instance of a browser.

Upvotes: 3

Related Questions