-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathenvironment.py
More file actions
92 lines (78 loc) · 3.25 KB
/
environment.py
File metadata and controls
92 lines (78 loc) · 3.25 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from behave.model_core import Status
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.edge.options import Options as EdgeOptions
import os
import json
INDEX = int(os.environ['INDEX']) if 'INDEX' in os.environ else 0
if os.environ.get("env") == "jenkins":
desired_cap_dict = os.environ["LT_BROWSERS"]
CONFIG = json.loads(desired_cap_dict)
else:
json_file = "config/config.json"
with open(json_file) as data_file:
CONFIG = json.load(data_file)
username = os.environ["LT_USERNAME"]
authkey = os.environ["LT_ACCESS_KEY"]
def before_scenario(context, scenario):
try:
desired_cap = setup_desired_cap(CONFIG[INDEX])
if 'Chrome' in scenario.tags:
options = ChromeOptions()
options.browser_version = desired_cap.get("version", "latest")
options.platform_name = "Windows 11"
elif 'Firefox' in scenario.tags:
options = FirefoxOptions()
options.browser_version = desired_cap.get("version", "latest")
options.platform_name = "Windows 10"
elif 'Edge' in scenario.tags:
options = EdgeOptions()
options.browser_version = desired_cap.get("version", "latest")
options.platform_name = "Windows 8"
elif 'SmartUI' in scenario.tags:
options = ChromeOptions()
options.browser_version = desired_cap.get("version", "latest")
options.set_capability('smartUI.project','Python-Behave-sample')
else:
raise ValueError("Unsupported browser tag")
options.set_capability('build', desired_cap.get('build'))
options.set_capability('name', desired_cap.get('name'))
# Print options for debugging
print("Browser Options:", options.to_capabilities())
context.browser = webdriver.Remote(
command_executor=f"https://{username}:{authkey}@hub.lambdatest.com/wd/hub",
options=options
)
except Exception as e:
print(f"Error in before_scenario: {str(e)}")
context.scenario.skip(reason=f"Failed to initialize browser: {str(e)}")
def after_scenario(context, scenario):
if hasattr(context, 'browser'):
try:
if scenario.status == Status.failed:
context.browser.execute_script("lambda-status=failed")
else:
context.browser.execute_script("lambda-status=passed")
except Exception as e:
print(f"Error setting lambda status: {str(e)}")
finally:
context.browser.quit()
def setup_desired_cap(desired_cap):
"""
Sets the capability according to LT
:param desired_cap:
:return:
"""
# Create a new dictionary to avoid modifying the original
cleaned_cap = {}
for key, value in desired_cap.items():
if key == 'connect':
# Force 'connect' to be None if it's not a valid timeout value
if not isinstance(value, (int, float)) or value is None:
cleaned_cap[key] = None
else:
cleaned_cap[key] = value
else:
cleaned_cap[key] = value
return cleaned_cap