|
| 1 | +import os |
1 | 2 | from selenium import webdriver |
| 3 | +from browserstack.local import Local |
2 | 4 |
|
3 | 5 | # Edit these to match your credentials |
4 | 6 | USERNAME = None |
5 | 7 | BROWSERSTACK_KEY = None |
| 8 | +test_type = 'single' |
| 9 | +bs_local = None |
| 10 | + |
| 11 | +if "BROWSERSTACK_USERNAME" in os.environ: |
| 12 | + USERNAME = os.environ["BROWSERSTACK_USERNAME"] |
| 13 | +if "BROWSERSTACK_ACCESS_KEY" in os.environ: |
| 14 | + BROWSERSTACK_KEY = os.environ["BROWSERSTACK_ACCESS_KEY"] |
| 15 | +if "TEST_TYPE" in os.environ: |
| 16 | + test_type = os.environ["TEST_TYPE"] |
6 | 17 |
|
7 | 18 | if not (USERNAME and BROWSERSTACK_KEY): |
8 | 19 | raise Exception("Please provide your BrowserStack username and access key") |
9 | 20 | sys.exit(1) |
10 | 21 |
|
11 | | -def before_all(context): |
12 | | - desired_capabilities = webdriver.DesiredCapabilities.FIREFOX |
13 | | - desired_capabilities['platform'] = 'WINDOWS' |
| 22 | +def start_local(): |
| 23 | + global bs_local |
| 24 | + bs_local = Local() |
| 25 | + bs_local_args = { "key": BROWSERSTACK_KEY, "forcelocal": "true" } |
| 26 | + bs_local.start(**bs_local_args) |
| 27 | + |
| 28 | +def stop_local(): |
| 29 | + global bs_local |
| 30 | + if bs_local is not None: |
| 31 | + bs_local.stop() |
| 32 | + |
| 33 | +def get_caps(): |
| 34 | + desired_capabilities = {} |
| 35 | + desired_capabilities['os'] = 'OS X' |
| 36 | + desired_capabilities['os_version'] = 'El Capitan' |
| 37 | + desired_capabilities['browser'] = 'firefox' |
| 38 | + desired_capabilities['browser_version'] = '46' |
| 39 | + desired_capabilities['build'] = 'Sample behave tests' |
| 40 | + desired_capabilities['name'] = 'Sample behave test' |
| 41 | + if 'local' in test_type.lower(): |
| 42 | + desired_capabilities['browserstack.local'] = True |
| 43 | + return desired_capabilities |
| 44 | + |
| 45 | +def before_feature(context, feature): |
| 46 | + if 'parallel' in test_type.lower(): |
| 47 | + context.browser = create_driver() |
14 | 48 |
|
15 | | - context.browser = webdriver.Remote( |
16 | | - desired_capabilities=desired_capabilities, |
17 | | - command_executor="http://%s:%s@hub.browserstack.com/wd/hub" % ( |
18 | | - USERNAME, BROWSERSTACK_KEY |
19 | | - ) |
20 | | - ) |
| 49 | +def after_feature(context, feature): |
| 50 | + if 'parallel' in test_type.lower(): |
| 51 | + context.browser.quit() |
| 52 | + |
| 53 | +def before_all(context): |
| 54 | + if 'local' in test_type.lower(): |
| 55 | + start_local() |
| 56 | + if 'parallel' not in test_type.lower(): |
| 57 | + context.browser = create_driver() |
21 | 58 |
|
22 | | - def after_all(context): |
| 59 | +def after_all(context): |
| 60 | + stop_local() |
| 61 | + if 'parallel' not in test_type.lower(): |
23 | 62 | context.browser.quit() |
| 63 | + |
| 64 | +def create_driver(): |
| 65 | + desired_capabilities = get_caps() |
| 66 | + |
| 67 | + return webdriver.Remote( |
| 68 | + desired_capabilities=desired_capabilities, |
| 69 | + command_executor="http://%s:%s@hub.browserstack.com/wd/hub" % ( |
| 70 | + USERNAME, BROWSERSTACK_KEY |
| 71 | + ) |
| 72 | + ) |
0 commit comments