Skip to content

Commit 25f2009

Browse files
committed
Added test for libraries and examples fetch.
Tests that each library and example opens successfully.
1 parent 6cbde74 commit 25f2009

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

bin/test_libraries_fetch.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
source ./env_vars.sh
4+
export CAPABILITIES='capabilities_firefox.yaml'
5+
cd ..
6+
time tox tests/libraries_fetch -- --url=https://codebender.cc --source=codebender_cc -F
7+
RETVAL=$?
8+
cd -
9+
echo "tests return value: ${RETVAL}"
10+
exit ${RETVAL}

codebender_testing/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def jsondump(data):
4444
# Logfile for /libraries compilation results
4545
LIBRARIES_TEST_LOGFILE = LOGFILE_PREFIX.format(log_name="libraries_test")
4646

47+
# Logfile for /libraries fetch results
48+
LIBRARIES_FETCH_LOGFILE = LOGFILE_PREFIX.format(log_name="libraries_fetch")
49+
4750
_EXTENSIONS_DIR = _rel_path('..', 'extensions')
4851
_FIREFOX_EXTENSION_FNAME = 'codebender.xpi'
4952
_CHROME_EXTENSION_FNAME = 'codebendercc-extension.crx'

codebender_testing/utils.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def read_last_log(compile_type):
9696
logs_re = re.compile(r'.+cb_compile_tester.+')
9797
if compile_type == 'library':
9898
logs_re = re.compile(r'.+libraries_test.+')
99+
elif compile_type == 'fetch':
100+
logs_re = re.compile(r'.+libraries_fetch.+')
101+
99102
logs = sorted([x for x in logs if x != '.gitignore' and logs_re.match(x)])
100103

101104
log_timestamp_re = re.compile(r'(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-.+\.json')
@@ -122,6 +125,8 @@ def report_creator(compile_type, log_entry, log_file):
122125
logs_re = re.compile(r'.+cb_compile_tester.+')
123126
if compile_type == 'library':
124127
logs_re = re.compile(r'.+libraries_test.+')
128+
elif compile_type == 'fetch':
129+
logs_re = re.compile(r'.+libraries_fetch.+')
125130

126131
logs = sorted([x for x in logs if x != '.gitignore' and logs_re.match(x)])
127132
tail = logs[-2:]
@@ -145,6 +150,12 @@ def report_creator(compile_type, log_entry, log_file):
145150
changes += 1
146151
continue
147152

153+
if compile_type == 'fetch':
154+
if old_log[url] != new_log[url]:
155+
diff[url] = new_log[url]
156+
changes += 1
157+
continue
158+
148159
for result in new_log[url].keys():
149160
if result not in old_log[url]:
150161
if not url in diff:
@@ -384,6 +395,71 @@ def delete_project(self, project_name):
384395
except:
385396
pass
386397

398+
def open_all_libraries_and_examples(self, url, logfile):
399+
self.open(url)
400+
examples = self.execute_script(_GET_SKETCHES_SCRIPT.format(selector='.accordion li a'), '$')
401+
assert len(examples) > 0
402+
libraries = self.execute_script(_GET_SKETCHES_SCRIPT.format(selector='.library_link'), '$')
403+
assert len(libraries) > 0
404+
examples_libraries = examples + libraries
405+
406+
log_time = gmtime()
407+
log_file = strftime(logfile, log_time)
408+
log_entry = {}
409+
410+
urls_visited = {}
411+
last_log = read_last_log('fetch')
412+
if last_log['log']:
413+
# resume previous compile
414+
log_time = strptime(last_log['timestamp'], '%Y-%m-%d_%H-%M-%S')
415+
log_file = strftime(logfile, log_time)
416+
log_entry = last_log['log']
417+
for url in last_log['log']:
418+
urls_visited[url] = True
419+
420+
urls_to_visit = []
421+
for url in examples_libraries:
422+
if url not in urls_visited:
423+
urls_to_visit.append(url)
424+
425+
if len(urls_to_visit) == 0:
426+
urls_to_visit = examples_libraries
427+
log_entry = {}
428+
log_time = gmtime()
429+
log_file = strftime(logfile, log_time)
430+
431+
library_re = re.compile(r'^https://codebender.cc/library/.+$')
432+
example_re = re.compile(r'^https://codebender.cc/example/.+/.+$')
433+
434+
print '\nVisiting:', len(urls_to_visit), 'URLs'
435+
tic = time.time()
436+
for url in urls_to_visit:
437+
self.open(url)
438+
test_status = True
439+
if library_re.match(url) and self.driver.current_url == 'https://codebender.cc/libraries':
440+
test_status = False
441+
elif example_re.match(url) and 'Sorry! The example could not be fetched.' in self.driver.page_source:
442+
test_status = False
443+
log_entry[url] = test_status
444+
445+
progress = '.'
446+
if not test_status:
447+
progress = 'F'
448+
449+
sys.stdout.write(progress)
450+
sys.stdout.flush()
451+
452+
with open(log_file, 'w', 0) as f:
453+
f.write(jsondump(log_entry))
454+
455+
toc = time.time()
456+
if toc - tic >= SAUCELABS_TIMEOUT_SECONDS:
457+
print '\nStopping tests to avoid saucelabs timeout'
458+
print 'Test duration:', int(toc - tic), 'sec'
459+
return
460+
461+
report_creator('fetch', log_entry, log_file)
462+
387463
def compile_sketch(self, url, boards, iframe=False):
388464
"""Compiles the sketch located at `url`, or an iframe within the page
389465
referred to by `url`. Raises an exception if it does not compile.

tests/libraries_fetch/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from codebender_testing.config import LIBRARIES_FETCH_LOGFILE
2+
from codebender_testing.utils import SeleniumTestCase
3+
4+
5+
class TestLibraryExamples(SeleniumTestCase):
6+
7+
def test_open_all_libraries(self):
8+
"""Tests that all libraries and examples open successfully."""
9+
self.open_all_libraries_and_examples('/libraries', LIBRARIES_FETCH_LOGFILE)

0 commit comments

Comments
 (0)