Skip to content

Commit 961a5b6

Browse files
author
Brandon Duffany
committed
Add logging for libraries test
1 parent 553783e commit 961a5b6

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

codebender_testing/config.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
# test case(s).
1111
COMPILE_TESTER_URL = "/user/cb_compile_tester"
1212

13-
# Path to the logfile for the COMPILE_TESTER.
13+
# The prefix for all filenames of log files.
1414
# Note that it is given as a time format string, which will
1515
# be formatted appropriately.
16-
COMPILE_TESTER_LOGFILE = "logs/%Y-%m-%d_%H-%M-%S-cb_compile_tester.json"
16+
LOGFILE_PREFIX = os.path.join("logs", "%Y-%m-%d_%H-%M-%S-{log_name}.json")
17+
18+
# Logfile for COMPILE_TESTER compilation results
19+
COMPILE_TESTER_LOGFILE = LOGFILE_PREFIX.format(log_name="cb_compile_tester")
20+
21+
# Logfile for /libraries compilation results
22+
LIBRARIES_TEST_LOGFILE = LOGFILE_PREFIX.format(log_name="libraries_test")
1723

1824
# URL of the actual Codebender website
1925
LIVE_SITE_URL = "http://codebender.cc"

codebender_testing/utils.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@
2929
"return $('{selector}').map(function() {{ return this.href; }}).toArray();"
3030

3131
# JavaScript snippet to verify the code on the current page.
32-
_VERIFY_SCRIPT = "compilerflasher.verify()"
32+
_VERIFY_SCRIPT = """
33+
if (window.compilerflasher !== undefined) {
34+
compilerflasher.verify();
35+
} else {
36+
// BACHELOR
37+
verify();
38+
}
39+
"""
3340

3441
# How long (in seconds) to wait before assuming that an example
3542
# has failed to compile
@@ -136,19 +143,22 @@ def compile_sketch(self, url, iframe=False):
136143
# giving this iframe a meaningful name in the HTML (TODO?)
137144
self.driver.switch_to_frame(0)
138145
self.execute_script(_VERIFY_SCRIPT)
146+
# In the BACHELOR site the id is 'operation_output', but in the live
147+
# site the id is 'cb_cf_operation_output'. The [id$=operation_output]
148+
# here selects an id that _ends_ with 'operation_output'.
139149
compile_result = WebDriverWait(self.driver, VERIFY_TIMEOUT).until(
140-
any_text_to_be_present_in_element((By.ID, "cb_cf_operation_output"),
150+
any_text_to_be_present_in_element((By.CSS_SELECTOR, "[id$=operation_output]"),
141151
VERIFICATION_SUCCESSFUL_MESSAGE, VERIFICATION_FAILED_MESSAGE))
142152
if compile_result != VERIFICATION_SUCCESSFUL_MESSAGE:
143153
raise VerificationError(compile_result)
144154

145155

146-
def compile_all_sketches(self, url, selector, iframe=False, log_file=None):
156+
def compile_all_sketches(self, url, selector, iframe=False, logfile=None):
147157
"""Compiles all projects on the page at `url`. `selector` is a CSS selector
148158
that should select all relevant <a> tags containing links to sketches.
149-
`log_file` specifies a path to a file to which test results will be
159+
`logfile` specifies a path to a file to which test results will be
150160
logged. If it is not `None`, compile errors will not cause the test
151-
to halt, but rather be logged to the given file. `log_file` may be a time
161+
to halt, but rather be logged to the given file. `logfile` may be a time
152162
format string, which will be formatted appropriately.
153163
`iframe` specifies whether the urls pointed to by `selector` are contained
154164
within an iframe.
@@ -157,11 +167,11 @@ def compile_all_sketches(self, url, selector, iframe=False, log_file=None):
157167
sketches = self.execute_script(_GET_SKETCHES_SCRIPT.format(selector=selector))
158168
assert len(sketches) > 0
159169

160-
if log_file is None:
170+
if logfile is None:
161171
for sketch in sketches:
162172
self.compile_sketch(sketch, iframe=iframe)
163173
else:
164-
log_entry = {'succeeded': [], 'failed': []}
174+
log_entry = {'url': self.site_url, 'succeeded': [], 'failed': []}
165175
for sketch in sketches:
166176
try:
167177
self.compile_sketch(sketch, iframe=iframe)
@@ -172,8 +182,8 @@ def compile_all_sketches(self, url, selector, iframe=False, log_file=None):
172182
'exception': "%s; %s" % (type(e).__name__, str(e))
173183
# TODO?: is it possible to get the actual compiler error?
174184
})
175-
# Dump the test results to `log_file`.
176-
f = open(strftime(log_file, gmtime()), 'w')
185+
# Dump the test results to `logfile`.
186+
f = open(strftime(logfile, gmtime()), 'w')
177187
json.dump(log_entry, f)
178188
f.close()
179189

tests/libraries/test_libraries.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from codebender_testing.config import LIBRARIES_TEST_LOGFILE
12
from codebender_testing.config import LIVE_SITE_URL
23
from codebender_testing.utils import SeleniumTestCase
34

@@ -9,5 +10,6 @@ class TestLibraryExamples(SeleniumTestCase):
910

1011
def test_compile_all_libraries(self):
1112
"""Tests that all library examples compile successfully."""
12-
self.compile_all_sketches('/libraries', '.accordion li a')
13+
self.compile_all_sketches('/libraries', '.accordion li a',
14+
logfile=LIBRARIES_TEST_LOGFILE)
1315

0 commit comments

Comments
 (0)