test_dashboard_nav.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import os
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from notebook.utils import url_path_join
  6. from notebook.tests.selenium.utils import wait_for_selector
  7. pjoin = os.path.join
  8. class PageError(Exception):
  9. """Error for an action being incompatible with the current jupyter web page.
  10. """
  11. def __init__(self, message):
  12. self.message = message
  13. def url_in_tree(browser, url=None):
  14. if url is None:
  15. url = browser.current_url
  16. tree_url = url_path_join(browser.jupyter_server_info['url'], 'tree')
  17. return url.startswith(tree_url)
  18. def get_list_items(browser):
  19. """Gets list items from a directory listing page
  20. Raises PageError if not in directory listing page (url has tree in it)
  21. """
  22. if not url_in_tree(browser):
  23. raise PageError("You are not in the notebook's file tree view."
  24. "This function can only be used the file tree context.")
  25. # we need to make sure that at least one item link loads
  26. wait_for_selector(browser, '.item_link')
  27. return [{
  28. 'link': a.get_attribute('href'),
  29. 'label': a.find_element_by_class_name('item_name').text,
  30. 'element': a,
  31. } for a in browser.find_elements_by_class_name('item_link')]
  32. def only_dir_links(browser):
  33. """Return only links that point at other directories in the tree
  34. """
  35. items = get_list_items(browser)
  36. return [i for i in items
  37. if url_in_tree(browser, i['link']) and i['label'] != '..']
  38. def test_items(authenticated_browser):
  39. visited_dict = {}
  40. # Going down the tree to collect links
  41. while True:
  42. wait_for_selector(authenticated_browser, '.item_link')
  43. current_url = authenticated_browser.current_url
  44. items = visited_dict[current_url] = only_dir_links(authenticated_browser)
  45. try:
  46. item = items[0]
  47. item["element"].click()
  48. assert authenticated_browser.current_url == item['link']
  49. except IndexError:
  50. break
  51. # Going back up the tree while we still have unvisited links
  52. while visited_dict:
  53. current_items = only_dir_links(authenticated_browser)
  54. current_items_links = [item["link"] for item in current_items]
  55. stored_items = visited_dict.pop(authenticated_browser.current_url)
  56. stored_items_links = [item["link"] for item in stored_items]
  57. assert stored_items_links == current_items_links
  58. authenticated_browser.back()