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

Commit 6489c54

Browse files
authored
Merge pull request #1244 from bjones1/bookserver
2 parents 6e3654b + 02752dc commit 6489c54

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

runestone/shared_conftest.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#
2020
# Standard library
2121
# ----------------
22-
# None.
23-
#
22+
import logging
23+
2424
# Third-party imports
2525
# -------------------
2626
import pytest
@@ -48,15 +48,21 @@ def selenium_driver(selenium_driver_session):
4848
yield driver
4949

5050
# Print the logs -- see the setup in `selenium_logging <selenium_logging>`.
51-
print(
52-
"\n"
53-
"JavaScript console logs\n"
54-
"======================="
55-
)
56-
logs = driver.get_log('browser')
57-
for log in logs:
58-
print(f'{log["level"]}: {log["message"]}')
59-
print("\n")
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"])
6066

6167
# 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.)
6268
driver.execute_script("window.localStorage.clear();")
@@ -81,9 +87,7 @@ def __init__(
8187

8288
# A helper function to attach to the Selenium driver: get from a URL relative to the Runestone application.
8389
def get(self, relative_url):
84-
return self.driver.get(
85-
"{}/{}".format(self.host_address, relative_url)
86-
)
90+
return self.driver.get("{}/{}".format(self.host_address, relative_url))
8791

8892
# Scroll to the top of the window. A button can sometimes be scrolled to the top of the screen, where it's hidden by the navigation bar. In this case, we can't click it, since Selenium will complain ``Message: element click intercepted: Element <button class="btn btn-success run-button" type="button">...</button> is not clickable at point (460, 17). Other element would receive the click: <div class="navbar-collapse collapse navbar-ex1-collapse">...</div>``. To avoid this, scroll to the top of the document, guaranteeing that the navbar won't be hiding the run button.
8993
def scroll_to_top(self):
@@ -92,9 +96,7 @@ def scroll_to_top(self):
9296
# Wait until a Runestone component has finished rendering itself, given the ID of the component.
9397
def wait_until_ready(self, id):
9498
# The component is ready when it has the class below.
95-
self.wait.until(
96-
element_has_css_class((By.ID, id), "runestone-component-ready")
97-
)
99+
self.wait.until(element_has_css_class((By.ID, id), "runestone-component-ready"))
98100

99101

100102
# An expectation for Selenium, used for checking that an element has a particular css class. From the `Selenium docs <https://selenium-python.readthedocs.io/waits.html#explicit-waits>`_, under the "Custom wait conditions" subheading.

0 commit comments

Comments
 (0)