@@ -50,11 +50,22 @@ def job_access(self):
50
50
51
51
@pytest .mark .optionalhook
52
52
def pytest_selenium_runtest_makereport (item , report , summary , extra ):
53
+ if report .when in ["setup" , "teardown" ]:
54
+ return
53
55
provider = BrowserStack ()
54
56
if not provider .uses_driver (item .config .getoption ("driver" )):
55
57
return
56
58
57
59
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
+ )
58
69
session_id = item ._driver .session_id
59
70
api_url = provider .API .format (session = session_id )
60
71
@@ -75,22 +86,39 @@ def pytest_selenium_runtest_makereport(item, report, summary, extra):
75
86
)
76
87
77
88
try :
78
- # Update the job result
89
+ # Update the session status
79
90
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
+ )
92
120
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 ))
94
122
95
123
96
124
def driver_kwargs (request , test , capabilities , ** kwargs ):
0 commit comments