Skip to content

Commit 9f31717

Browse files
committed
add: use javascript executor to mark the session instead of API call
remove multiple api calls, use driver to execute js to mark status for better annotation on browserstack dashboard
1 parent 47f4ef3 commit 9f31717

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

src/pytest_selenium/drivers/browserstack.py

+42-14
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,22 @@ def job_access(self):
5050

5151
@pytest.mark.optionalhook
5252
def pytest_selenium_runtest_makereport(item, report, summary, extra):
53+
if report.when in ["setup", "teardown"]:
54+
return
5355
provider = BrowserStack()
5456
if not provider.uses_driver(item.config.getoption("driver")):
5557
return
5658

5759
passed = report.passed or (report.failed and hasattr(report, "wasxfail"))
60+
# set test failure reason if available
61+
fail_reason = ""
62+
if not passed:
63+
try:
64+
fail_reason = report.longrepr.reprcrash.message
65+
except Exception as e:
66+
summary.append(
67+
"WARNING: Failed to determine {0} job URL: {1}".format(provider.name, e)
68+
)
5869
session_id = item._driver.session_id
5970
api_url = provider.API.format(session=session_id)
6071

@@ -75,22 +86,39 @@ def pytest_selenium_runtest_makereport(item, report, summary, extra):
7586
)
7687

7788
try:
78-
# Update the job result
89+
# Update the session status
7990
job_status = job_info["automation_session"]["status"]
80-
status = "running" if passed else "error"
81-
if report.when == "teardown" and passed:
82-
status = "completed"
83-
if job_status not in ("error", status):
84-
# Only update the result if it's not already marked as failed
85-
requests.put(
86-
api_url,
87-
headers={"Content-Type": "application/json"},
88-
params={"status": status},
89-
auth=provider.auth,
90-
timeout=10,
91-
)
91+
if job_status not in ("failed", "passed"):
92+
# Only update the status if it's not already marked (by user via script)
93+
if passed:
94+
item._driver.execute_script(
95+
'browserstack_executor: { \
96+
"action": "setSessionStatus", \
97+
"arguments": { "status":"passed" } \
98+
}'
99+
)
100+
else:
101+
if fail_reason:
102+
item._driver.execute_script(
103+
'browserstack_executor: {{ \
104+
"action": "setSessionStatus", \
105+
"arguments": {{ \
106+
"status":"failed", \
107+
"reason": "{}" \
108+
}} \
109+
}}'.format(
110+
fail_reason
111+
)
112+
)
113+
else:
114+
item._driver.execute_script(
115+
'browserstack_executor: { \
116+
"action": "setSessionStatus", \
117+
"arguments": { "status":"failed" } \
118+
}'
119+
)
92120
except Exception as e:
93-
summary.append("WARNING: Failed to update job status: {0}".format(e))
121+
summary.append("WARNING: Failed to update session status: {0}".format(e))
94122

95123

96124
def driver_kwargs(request, test, capabilities, **kwargs):

0 commit comments

Comments
 (0)