-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathenvironment.py
More file actions
52 lines (41 loc) · 2.11 KB
/
environment.py
File metadata and controls
52 lines (41 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from selenium import webdriver
from browserstack.local import Local
import os, json
CONFIG_FILE = os.environ['CONFIG_FILE'] if 'CONFIG_FILE' in os.environ else 'config/single.json'
TASK_ID = int(os.environ['TASK_ID']) if 'TASK_ID' in os.environ else 0
with open(CONFIG_FILE) as data_file:
CONFIG = json.load(data_file)
bs_local = None
BROWSERSTACK_USERNAME = os.environ['BROWSERSTACK_USERNAME'] if 'BROWSERSTACK_USERNAME' in os.environ else CONFIG['user']
BROWSERSTACK_ACCESS_KEY = os.environ['BROWSERSTACK_ACCESS_KEY'] if 'BROWSERSTACK_ACCESS_KEY' in os.environ else CONFIG['key']
def start_local():
"""Code to start browserstack local before start of test."""
global bs_local
bs_local = Local()
bs_local_args = { "key": BROWSERSTACK_ACCESS_KEY, "forcelocal": "true" }
bs_local.start(**bs_local_args)
def stop_local():
"""Code to stop browserstack local after end of test."""
global bs_local
if bs_local is not None:
bs_local.stop()
def before_feature(context, feature):
desired_capabilities = CONFIG['environments'][TASK_ID]
desired_capabilities['browserstack.source'] = 'behave:sample-master:v1.1'
for key in CONFIG["capabilities"]:
if key not in desired_capabilities:
desired_capabilities[key] = CONFIG["capabilities"][key]
if "browserstack.local" in desired_capabilities and desired_capabilities["browserstack.local"]:
start_local()
context.browser = webdriver.Remote(
desired_capabilities=desired_capabilities,
command_executor="https://%s:%s@hub.browserstack.com/wd/hub" % (BROWSERSTACK_USERNAME, BROWSERSTACK_ACCESS_KEY),
keep_alive=True
)
def after_feature(context, feature):
if context.failed is True:
context.browser.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "At least 1 assertion failed"}}')
if context.failed is not True:
context.browser.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "All assertions passed"}}')
context.browser.quit()
stop_local()