Skip to content

Commit dc1eeab

Browse files
authored
Merge pull request #3412 from mpacer/update_selenium_tests
Update selenium tests
2 parents e433b81 + e688774 commit dc1eeab

File tree

2 files changed

+72
-34
lines changed

2 files changed

+72
-34
lines changed

notebook/tests/selenium/conftest.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def notebook_server():
6363
requests.post(urljoin(info['url'], 'api/shutdown'),
6464
headers={'Authorization': 'token '+info['token']})
6565

66-
67-
def _get_selenium_driver():
66+
@pytest.fixture(scope='session')
67+
def selenium_driver():
6868
if os.environ.get('SAUCE_USERNAME'):
6969
username = os.environ["SAUCE_USERNAME"]
7070
access_key = os.environ["SAUCE_ACCESS_KEY"]
@@ -81,16 +81,20 @@ def _get_selenium_driver():
8181
capabilities['version'] = '57.0'
8282
hub_url = "%s:%s@localhost:4445" % (username, access_key)
8383
print("Connecting remote driver on Sauce Labs")
84-
return Remote(desired_capabilities=capabilities,
84+
driver = Remote(desired_capabilities=capabilities,
8585
command_executor="http://%s/wd/hub" % hub_url)
8686
elif os.environ.get('JUPYTER_TEST_BROWSER') == 'chrome':
87-
return Chrome()
87+
driver = Chrome()
8888
else:
89-
return Firefox()
89+
driver = Firefox()
90+
91+
yield driver
92+
93+
# Teardown
94+
driver.quit()
9095

9196
@pytest.fixture
92-
def browser(notebook_server):
93-
b = _get_selenium_driver()
94-
b.get("{url}?token={token}".format(**notebook_server))
95-
yield b
96-
b.quit()
97+
def authenticated_browser(selenium_driver, notebook_server):
98+
selenium_driver.jupyter_server_info = notebook_server
99+
selenium_driver.get("{url}?token={token}".format(**notebook_server))
100+
return selenium_driver

notebook/tests/selenium/test_dashboard_nav.py

+58-24
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,76 @@
44
from selenium.webdriver.support.ui import WebDriverWait
55
from selenium.webdriver.support import expected_conditions as EC
66

7+
from notebook.utils import url_path_join
78
pjoin = os.path.join
89

10+
11+
class PageError(Exception):
12+
"""Error for an action being incompatible with the current jupyter web page.
13+
14+
"""
15+
def __init__(self, message):
16+
self.message = message
17+
18+
19+
def url_in_tree(browser, url=None):
20+
if url is None:
21+
url = browser.current_url
22+
tree_url = url_path_join(browser.jupyter_server_info['url'], 'tree')
23+
return url.startswith(tree_url)
24+
25+
926
def get_list_items(browser):
27+
"""Gets list items from a directory listing page
28+
29+
Raises PageError if not in directory listing page (url has tree in it)
30+
"""
31+
if not url_in_tree(browser):
32+
raise PageError("You are not in the notebook's file tree view."
33+
"This function can only be used the file tree context.")
34+
# we need to make sure that at least one item link loads
35+
wait_for_selector(browser, '.item_link')
36+
1037
return [{
1138
'link': a.get_attribute('href'),
1239
'label': a.find_element_by_class_name('item_name').text,
40+
'element': a,
1341
} for a in browser.find_elements_by_class_name('item_link')]
1442

1543

44+
def only_dir_links(browser):
45+
"""Return only links that point at other directories in the tree
46+
47+
"""
48+
items = get_list_items(browser)
49+
return [i for i in items
50+
if url_in_tree(browser, i['link']) and i['label'] != '..']
51+
52+
1653
def wait_for_selector(browser, selector, timeout=10):
1754
wait = WebDriverWait(browser, timeout)
1855
return wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
1956

2057

58+
def test_items(authenticated_browser):
59+
visited_dict = {}
60+
# Going down the tree to collect links
61+
while True:
62+
wait_for_selector(authenticated_browser, '.item_link')
63+
current_url = authenticated_browser.current_url
64+
items = visited_dict[current_url] = only_dir_links(authenticated_browser)
65+
try:
66+
item = items[0]
67+
item["element"].click()
68+
assert authenticated_browser.current_url == item['link']
69+
except IndexError:
70+
break
71+
# Going back up the tree while we still have unvisited links
72+
while visited_dict:
73+
current_items = only_dir_links(authenticated_browser)
74+
current_items_links = [item["link"] for item in current_items]
75+
stored_items = visited_dict.pop(authenticated_browser.current_url)
76+
stored_items_links = [item["link"] for item in stored_items]
77+
assert stored_items_links == current_items_links
78+
authenticated_browser.back()
2179

22-
def test_items(browser, visited=None):
23-
tree_root_url = browser.current_url
24-
if visited is None:
25-
visited = set()
26-
27-
wait_for_selector(browser, '.item_link')
28-
items = get_list_items(browser)
29-
print(browser.current_url, len(items))
30-
for item in items:
31-
print(item)
32-
url = item['link']
33-
if url.startswith(tree_root_url):
34-
print("Going to", url)
35-
if url in visited:
36-
continue
37-
visited.add(url)
38-
browser.get(url)
39-
wait_for_selector(browser, '.item_link')
40-
assert browser.current_url == url
41-
42-
test_items(browser, visited)
43-
#browser.back()
44-
45-
print()

0 commit comments

Comments
 (0)