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/"
mndrbbr
  • 105
  • 3
  • 6
  • 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. – Louis Mar 24 '14 at 22:56

1 Answers1

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.

ExperimentsWithCode
  • 1,174
  • 4
  • 14
  • 30
  • 2
    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. – Nam G VU May 26 '17 at 08:05