vendredi 8 avril 2016

Execute Test on Two Different Browsers in Pytest via Command Line

I want to execute a test from command line in PyTest that will run on multiple browsers, either in Series or Parallel. I have a conftest.py file setup that allows me to specify which browser I would like to run my test on. However, I don't know how to specify to run on both browsers.

See below my code example:

conftest.py

profile = FirefoxProfile(common.firefoxProfile)
browsers = {'firefox': 'firefox', 'internet explorer': 'internet explorer'}

def pytest_addoption(parser):
    parser.addoption("--browserName", default='',
        type='choice', choices=sorted(browsers),
        help='Runs tests only for given browser')

@pytest.yield_fixture(params=browsers.keys())
def webdriver(request):
    selectedBrowser = request.config.getoption('--browserName')

     if selectedBrowser and selectedBrowser != request.param:
         pytest.skip('browser {} selected in the command line'.format(selectedBrowser))

    desired_caps = {}
    desired_caps['browserName'] = selectedBrowser
     desired_caps['platform'] = request.config.getoption("--platform")

    driver = Remote('http://ift.tt/1PY0n4u', desired_caps, browser_profile=profile)

    driver.get(common.appUrl)   
    request.addfinalizer(driver.quit)
    yield driver

The above code allows me to execute a test case on a specific browser using commandline:

py.test test_SampleTest.py --browserName=firefox

As you can see, in my fixture I take this argument and then create a webdriver with that name. How do I go about taking an argument of, for example, --browserName=bothBrowsers and create a webdriver for each and then run the test twice?

When I run the --collect-only command I can see there are two items collected, the test with each parameter:

plugins: allure-adaptor-1.6.5, cov-2.2.0, pythonpath-0.7, xdist-1.13.1
collected 2 items
<Module 'test_SampleTest.py'>
  <Function 'test_sample[internet explorer]'>
  <Function 'test_sample[firefox]'>

I don't know where to go from here and would appreciate any help.

Thanks.

Aucun commentaire:

Enregistrer un commentaire