Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit b1cd76e

Browse files
committed
Fix: Log failures in Selenium test setup.
1 parent 6489c54 commit b1cd76e

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

runestone/shared_conftest.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,27 @@ def selenium_driver(selenium_driver_session):
4545
# Copied from the Runestone Components test framework.
4646
driver.implicitly_wait(10)
4747

48-
yield driver
49-
50-
# Print the logs -- see the setup in `selenium_logging <selenium_logging>`.
51-
#
52-
# Transform Chrome log levels to `Python log levels <https://docs.python.org/3/library/logging.html#logging-levels>`_.
53-
chrome_to_py_loglevels = {
54-
"NOTSET": 0,
55-
"DEBUG": 10,
56-
"INFO": 20,
57-
"WARNING": 30,
58-
"ERROR": 40,
59-
"SEVERE": 40,
60-
"CRITICAL": 50,
61-
}
62-
py_logger = logging.getLogger("Chrome.JavaScript.console")
63-
chrome_logs = driver.get_log("browser")
64-
for log in chrome_logs:
65-
py_logger.log(chrome_to_py_loglevels[log["level"]], log["message"])
48+
try:
49+
yield driver
50+
finally:
51+
# Print the logs -- see the setup in `selenium_logging <selenium_logging>`. Capture them if setup fails, since the logs may help understand the failure. However, pytest ignore any generated logs; see `my bug report <https://github.com/pytest-dev/pytest/issues/9021>`_. As a workaround, uncomment the print statement below to dump unfiltered logs during a test run.
52+
#
53+
# Transform Chrome log levels to `Python log levels <https://docs.python.org/3/library/logging.html#logging-levels>`_.
54+
chrome_to_py_loglevels = {
55+
"NOTSET": 0,
56+
"DEBUG": 10,
57+
"INFO": 20,
58+
"WARNING": 30,
59+
"ERROR": 40,
60+
"SEVERE": 40,
61+
"CRITICAL": 50,
62+
}
63+
py_logger = logging.getLogger("Chrome.JavaScript.console")
64+
chrome_logs = driver.get_log("browser")
65+
for log in chrome_logs:
66+
py_logger.log(chrome_to_py_loglevels[log["level"]], log["message"])
67+
# Uncomment this to print raw log-like data as a workaround, as described above.
68+
##print(f"{log['level']} - {log['message']}")
6669

6770
# Clear as much as possible, to present an almost-fresh instance of a browser for the next test. (Shutting down then starting up a browser is very slow.)
6871
driver.execute_script("window.localStorage.clear();")

0 commit comments

Comments
 (0)