Olga Ch
Olga Ch

Reputation: 1

how to run more than 1 unittest through BrowserStack remote driver?

I'm learning to write autotests through Selenium using Python to run tests remotely with BrowserStack. I faced the problem, that code which I got on my course works only for 1 test with no functions and classes. When I create a class with multiple unit tests inside, my code does not work.

So on BrowserStack website I found an example for my needs, but it also failed. This version below does not work:

class BrowserStackTests(unittest.TestCase):
    def setUp(self):
        desired_cap = {
            'os_version': 'Sonoma',
            'os': 'OS X',
            'browser': 'Safari',
            'browser_version': '17.3',
            'name': 'BStack Safari OS X Sonoma',
            'build': 'browserstack-build-1'
        }
        url = my_key.key
        desired_cap['acceptSslCerts'] = True
        self.driver = webdriver.Remote(command_executor=url,
                                       desired_capabilities=desired_cap)
        self.driver.maximize_window()

    def test_01_pos_link_leads_to_the_right_website(self):
        driver = self.driver
        driver.get(hp.main_url)
        hp.delay()
        print(f"Page has '{driver.title}' as Page title")
        driver.close() 

...

TypeError: WebDriver.__init__() got an unexpected keyword argument 'desired_capabilities'

This command from BrowserStack website's guide:

elf.driver = webdriver.Remote(command_executor=url,desired_capabilities=desired_cap)

When I put 'options' iso 'desired_capabilities' this one below does work, but I feel it's not really correct, there are too many lines and it could be written shorter. And if I want to add an additional browser, the code again got broken and tests failed.

class BrowserStack(unittest.TestCase):

def get_browser_option(browser):
    switcher = {
        "safari": SafariOptions(),
    }
    return switcher.get(browser,SafariOptions())

def setUp(self):
    desired_cap = {
        'os_version': 'Sonoma',
        'os': 'OS X',
        'browser': 'Safari',
        'browser_version': '17.3',
        'name': 'BStack  parallel Safari OS X Sonoma',  # test name
        'build': 'browserstack-build-1'
    }

    bstack_options = {
        "osVersion": desired_cap["os_version"],
        "buildName": desired_cap["build"],
        "sessionName": desired_cap["name"],
        "userName": BROWSERSTACK_USERNAME,
        "accessKey": BROWSERSTACK_ACCESS_KEY
    }

    bstack_options["os"] = desired_cap["os"]
    options = self.get_browser_option()
    options.browser_version = desired_cap["browser_version"]
    options.set_capability('bstack:options', bstack_options)
    desired_cap['acceptSslCerts'] = True
    self.driver = webdriver.Remote(command_executor=URL,
                                   options = options)
    self.driver.maximize_window()

def test_01_pos_link_leads_to_the_right_website(self):
    driver = self.driver
    driver.get(hp.main_url)
    hp.delay()
    print(f"Page has '{driver.title}' as Page title")
    driver.close()  

How can I make it shorter and for multiple tests and multiple browsers?

Upvotes: 0

Views: 64

Answers (0)

Related Questions