Skip to content

Commit a0cb0ae

Browse files
committed
Merge pull request #14 from codebendercc/libraries-examples-fetch-test
Libraries examples fetch test
2 parents 6cbde74 + 3a2c07d commit a0cb0ae

File tree

7 files changed

+111
-2
lines changed

7 files changed

+111
-2
lines changed

bin/test_common.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22

33
source ./env_vars.sh
4+
export CAPABILITIES='capabilities_firefox.yaml'
45
cd ..
56
time tox tests/common -- --url=https://codebender.cc --source=codebender_cc
67
RETVAL=$?

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: 7 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'
@@ -76,6 +79,8 @@ def jsondump(data):
7679
'LOCATE_ELEMENT': 30
7780
}
7881

82+
TESTS_USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0 codebender-selenium'
83+
7984
# Set up Selenium Webdrivers to be used for selenium tests
8085
def _get_firefox_profile():
8186
"""Returns the Firefox profile to be used for the FF webdriver.
@@ -144,6 +149,8 @@ def create_webdriver(command_executor, desired_capabilities):
144149
desired_capabilities = DesiredCapabilities.FIREFOX.copy()
145150
desired_capabilities.update(_capabilities)
146151
browser_profile = _get_firefox_profile()
152+
browser_profile.set_preference("general.useragent.override", TESTS_USER_AGENT)
153+
desired_capabilities["firefox_profile"] = browser_profile.update_preferences()
147154
else:
148155
raise ValueError("Invalid webdriver %s (only chrome and firefox are supported)" % browser_name)
149156
return webdriver.Remote(

codebender_testing/utils.py

Lines changed: 82 additions & 1 deletion
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.
@@ -588,7 +664,12 @@ def create_sketch(self, name):
588664
headingInput.send_keys(Keys.ENTER)
589665
WebDriverWait(self.driver, VERIFY_TIMEOUT).until(
590666
expected_conditions.invisibility_of_element_located(
591-
(By.CSS_SELECTOR, "#editor_heading_project_name i")
667+
(By.CSS_SELECTOR, "#editor_heading_project_working")
668+
)
669+
)
670+
WebDriverWait(self.driver, VERIFY_TIMEOUT).until(
671+
expected_conditions.text_to_be_present_in_element(
672+
(By.ID, "operation_output"), 'Name successfully changed!'
592673
)
593674
)
594675

tests/common/sketch/test_sketch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def test_run_with_no_port(self):
7575
@pytest.mark.requires_extension
7676
def test_speeds_dropdown(self):
7777
"""Tests that the speeds dropdown exists."""
78+
self.get_element(By.ID, "serial_monitor_toggle").click()
7879
self.get_element(By.ID, "cb_cf_baud_rates")
7980

8081
@pytest.mark.requires_extension
@@ -108,7 +109,7 @@ def test_add_projectfile_direct(self):
108109
field """
109110
self.open_project()
110111

111-
add_button = self.get_element(By.CLASS_NAME, 'add-file-button')
112+
add_button = self.get_element(By.ID, 'newfile')
112113
add_button.click()
113114
WebDriverWait(self.driver, VERIFY_TIMEOUT).until(
114115
expected_conditions.visibility_of(

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)