|
3 | 3 |
|
4 | 4 | import os |
5 | 5 |
|
6 | | -from .utils import TREE_PAGE |
| 6 | +from .utils import TREE_PAGE, EDITOR_PAGE |
7 | 7 | from jupyter_server.utils import url_path_join |
8 | 8 | pjoin = os.path.join |
9 | 9 |
|
10 | 10 |
|
11 | 11 | def url_in_tree(nb, url=None): |
| 12 | + """Ensure we're in the tree page somewhere (the file browser page)""" |
12 | 13 | if url is None: |
13 | 14 | url = nb.get_page_url(page=TREE_PAGE) |
14 | 15 |
|
15 | 16 | tree_url = url_path_join(nb.get_server_info(), 'tree') |
16 | 17 | return True if tree_url in url else False |
17 | 18 |
|
18 | 19 |
|
19 | | -def get_list_items(nb): |
20 | | - """ |
21 | | - Gets list items from a directory listing page |
22 | | - """ |
23 | | - |
24 | | - nb.wait_for_selector('#notebook_list .item_link', page=TREE_PAGE) |
25 | | - notebook_list = nb.locate('#notebook_list', page=TREE_PAGE) |
26 | | - link_items = notebook_list.locate_all('.item_link') |
27 | | - |
28 | | - return [{ |
29 | | - 'link': a.get_attribute('href'), |
30 | | - 'label': a.get_inner_text(), |
31 | | - 'element': a, |
32 | | - } for a in link_items if a.get_inner_text() != '..'] |
| 20 | +def navigate_to_link(nb, item_text): |
| 21 | + print(f'[Test] Now navigating to "{item_text}"') |
| 22 | + notebook_frontend = nb |
| 23 | + |
| 24 | + # Define a match finder func (this is easier |
| 25 | + # to hook into for debugging than a listcomp) |
| 26 | + def get_matches(): |
| 27 | + print(f'[Test] Attempt to find matches...') |
| 28 | + tree_page_items = [item for item in notebook_frontend.locate_all('#notebook_list .item_link', TREE_PAGE)] |
| 29 | + if tree_page_items: |
| 30 | + print(f'[Test] Found!') |
| 31 | + return [elem for elem in tree_page_items if elem.is_visible() and elem.get_inner_text().strip() == item_text] |
| 32 | + # Get tree page item matching the item_text (the dir name/link element to navigate to) |
| 33 | + matches = notebook_frontend.wait_for_condition( |
| 34 | + lambda: get_matches(), |
| 35 | + period=3 |
| 36 | + ) |
| 37 | + target_element = matches[0] |
| 38 | + target_link = target_element.get_attribute("href") |
| 39 | + target_element.click() |
| 40 | + notebook_frontend.wait_for_condition( |
| 41 | + lambda: not notebook_frontend.locate( |
| 42 | + f'#notebook_list .item_link >> text="{item_text}"', |
| 43 | + page=EDITOR_PAGE).is_visible() |
| 44 | + ) |
| 45 | + print(f'[Test] Check URL in tree') |
| 46 | + notebook_frontend.wait_for_condition( |
| 47 | + lambda: url_in_tree(notebook_frontend), |
| 48 | + timeout=60, |
| 49 | + period=5 |
| 50 | + ) |
| 51 | + print(f'[Test] Check URL matches link') |
| 52 | + print(f'[Test] Item link: "{target_link}"') |
| 53 | + print(f'[Test] Current URL is: "{notebook_frontend.get_page_url(page=TREE_PAGE)}"') |
| 54 | + notebook_frontend.wait_for_condition( |
| 55 | + lambda: target_link in notebook_frontend.get_page_url(page=TREE_PAGE), |
| 56 | + timeout=60, |
| 57 | + period=5 |
| 58 | + ) |
| 59 | + print(f'[Test] Passed!') |
| 60 | + return notebook_frontend.get_page_url(page=TREE_PAGE) |
33 | 61 |
|
34 | 62 |
|
35 | 63 | def test_navigation(notebook_frontend): |
36 | | - print('[Test] [test_dashboard_nav] Start!') |
37 | | - |
38 | | - print('[Test] Obtain list of elements') |
39 | | - link_elements = get_list_items(notebook_frontend) |
40 | | - |
41 | | - # Recursively traverse and check folder in the Jupyter root dir |
42 | | - def check_links(nb, list_of_link_elements): |
43 | | - print('[Test] Check links') |
44 | | - if len(list_of_link_elements) < 1: |
45 | | - return |
46 | | - |
47 | | - for item in list_of_link_elements: |
48 | | - print(f'[Test] Check "{item["label"]}"') |
49 | | - if 'Untitled.ipynb' in item["label"]: |
50 | | - # Skip notebook files in the temp dir |
51 | | - continue |
52 | | - |
53 | | - item["element"].click() |
54 | | - |
55 | | - notebook_frontend.wait_for_condition( |
56 | | - lambda: url_in_tree(notebook_frontend), |
57 | | - timeout=600, |
58 | | - period=5 |
59 | | - ) |
60 | | - notebook_frontend.wait_for_condition( |
61 | | - lambda: item["link"] in nb.get_page_url(page=TREE_PAGE), |
62 | | - timeout=600, |
63 | | - period=5 |
64 | | - ) |
65 | | - |
66 | | - new_links = get_list_items(nb) |
67 | | - if len(new_links) > 0: |
68 | | - check_links(nb, new_links) |
69 | | - |
70 | | - nb.go_back(page=TREE_PAGE) |
71 | | - |
72 | | - return |
| 64 | + print('[Test] [test_dashboard_nav] Start! y2') |
| 65 | + |
| 66 | + # NOTE: The paths used here are currently define in |
| 67 | + # the server setup fixture (dirs/names are created there) |
| 68 | + # TODO: Refactor to define those dirs/names here |
| 69 | + |
| 70 | + SUBDIR1 = 'sub ∂ir1' |
| 71 | + SUBDIR2 = 'sub ∂ir2' |
| 72 | + SUBDIR1A = 'sub ∂ir 1a' |
| 73 | + SUBDIR1B = 'sub ∂ir 1b' |
| 74 | + |
| 75 | + # Start at the root (tree) URL |
| 76 | + print(f'[Test] Start at tree (root) URL') |
| 77 | + tree_url = notebook_frontend.get_page_url(page=TREE_PAGE) |
| 78 | + print(f'[Test] URL is now at "{tree_url}"') |
| 79 | + |
| 80 | + # Test navigation to first folder from root |
| 81 | + print(f'[Test] Navigate to subdir1') |
| 82 | + subdir1_url = navigate_to_link(notebook_frontend, SUBDIR1) |
| 83 | + print(f'[Test] URL is now at "{subdir1_url}"') |
| 84 | + |
| 85 | + # Test navigation to first subfolder (1a) |
| 86 | + print(f'[Test] Navigate to subdir1a') |
| 87 | + subdir1a_url = navigate_to_link(notebook_frontend, SUBDIR1A) |
| 88 | + print(f'[Test] URL is now at "{subdir1a_url}"') |
| 89 | + notebook_frontend.go_back(page=TREE_PAGE) |
| 90 | + notebook_frontend.wait_for_condition( |
| 91 | + lambda: notebook_frontend.get_page_url(page=TREE_PAGE) == subdir1_url, |
| 92 | + timeout=300, |
| 93 | + period=5 |
| 94 | + ) |
| 95 | + notebook_frontend.go_back(page=TREE_PAGE) |
| 96 | + notebook_frontend.wait_for_condition( |
| 97 | + lambda: notebook_frontend.get_page_url(page=TREE_PAGE) == tree_url, |
| 98 | + timeout=300, |
| 99 | + period=5 |
| 100 | + ) |
| 101 | + # Wait for the links to update (subdir1a link should NOT be showing anymore) |
| 102 | + notebook_frontend.wait_for_condition( |
| 103 | + lambda: not notebook_frontend.locate( |
| 104 | + f'#notebook_list .item_link >> text="{SUBDIR1A}"', |
| 105 | + page=EDITOR_PAGE).is_visible() |
| 106 | + ) |
| 107 | + |
| 108 | + # Test navigation to second folder from root |
| 109 | + print(f'[Test] Navigate to subdir2') |
| 110 | + subdir2_url = navigate_to_link(notebook_frontend, SUBDIR2) |
| 111 | + print(f'[Test] URL is now at "{subdir2_url}"') |
| 112 | + # Wait for the links to update (subdir2 link should NOT be showing anymore) |
| 113 | + notebook_frontend.wait_for_condition( |
| 114 | + lambda: not notebook_frontend.locate( |
| 115 | + f'#notebook_list .item_link >> text="{SUBDIR2}"', |
| 116 | + page=EDITOR_PAGE).is_visible() |
| 117 | + ) |
| 118 | + |
| 119 | + # Test navigation to second subfolder (1b) |
| 120 | + print(f'[Test] Navigate to subdir1b') |
| 121 | + subdir1b_url = navigate_to_link(notebook_frontend, SUBDIR1B) |
| 122 | + print(f'[Test] URL is now at "{subdir1b_url}"') |
| 123 | + notebook_frontend.go_back(page=TREE_PAGE) |
| 124 | + notebook_frontend.wait_for_condition( |
| 125 | + lambda: notebook_frontend.get_page_url(page=TREE_PAGE) == subdir2_url, |
| 126 | + timeout=300, |
| 127 | + period=5 |
| 128 | + ) |
| 129 | + notebook_frontend.go_back(page=TREE_PAGE) |
| 130 | + notebook_frontend.wait_for_condition( |
| 131 | + lambda: notebook_frontend.get_page_url(page=TREE_PAGE) == tree_url, |
| 132 | + timeout=300, |
| 133 | + period=5 |
| 134 | + ) |
73 | 135 |
|
74 | | - check_links(notebook_frontend, link_elements) |
75 | 136 | print('[Test] [test_dashboard_nav] Finished!') |
0 commit comments