3

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/" 
1
  • What is a "good way" really depends on the developer's opinion. What is a "good way" for one is a bad way for another. And it can be done in many dozen different ways. So the question you currently have is not a good fit for SO. Commented Mar 24, 2014 at 22:56

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think your self.driver always be the last item in your desired_cap tuple as the setUp() method finishes - all your test be run with that last item driver only.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.