quick_selenium.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Utilities for driving Selenium interactively to develop tests.
  2. These are not used in the tests themselves - rather, the developer writing tests
  3. can use them to experiment with Selenium.
  4. """
  5. from selenium.webdriver import Firefox
  6. from notebook.tests.selenium.utils import Notebook
  7. from notebook.notebookapp import list_running_servers
  8. class NoServerError(Exception):
  9. def __init__(self, message):
  10. self.message = message
  11. def quick_driver(lab=False):
  12. """Quickly create a selenium driver pointing at an active noteboook server.
  13. Usage example:
  14. from notebook.tests.selenium.quick_selenium import quick_driver
  15. driver = quick_driver
  16. Note: you need to manually close the driver that opens with driver.quit()
  17. """
  18. try:
  19. server = list(list_running_servers())[0]
  20. except IndexError as e:
  21. raise NoServerError('You need a server running before you can run '
  22. 'this command')
  23. driver = Firefox()
  24. auth_url = '{url}?token={token}'.format(**server)
  25. driver.get(auth_url)
  26. # If this redirects us to a lab page and we don't want that;
  27. # then we need to redirect ourselves to the classic notebook view
  28. if driver.current_url.endswith('/lab') and not lab:
  29. driver.get(driver.current_url.rstrip('lab')+'tree')
  30. return driver
  31. def quick_notebook():
  32. """Quickly create a new classic notebook in a selenium driver
  33. Usage example:
  34. from notebook.tests.selenium.quick_selenium import quick_notebook
  35. nb = quick_notebook()
  36. Note: you need to manually close the driver that opens with nb.browser.quit()
  37. """
  38. return Notebook.new_notebook(quick_driver())