|
4 | 4 | from selenium.webdriver.support.ui import WebDriverWait
|
5 | 5 | from selenium.webdriver.support import expected_conditions as EC
|
6 | 6 |
|
| 7 | +from notebook.utils import url_path_join |
7 | 8 | pjoin = os.path.join
|
8 | 9 |
|
| 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 | + |
9 | 26 | 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 | + |
10 | 37 | return [{
|
11 | 38 | 'link': a.get_attribute('href'),
|
12 | 39 | 'label': a.find_element_by_class_name('item_name').text,
|
| 40 | + 'element': a, |
13 | 41 | } for a in browser.find_elements_by_class_name('item_link')]
|
14 | 42 |
|
15 | 43 |
|
| 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 | + |
16 | 53 | def wait_for_selector(browser, selector, timeout=10):
|
17 | 54 | wait = WebDriverWait(browser, timeout)
|
18 | 55 | return wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, selector)))
|
19 | 56 |
|
20 | 57 |
|
| 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() |
21 | 79 |
|
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