user.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import threading
  2. import time
  3. from nicegui import globals, ui
  4. from selenium import webdriver
  5. from selenium.common.exceptions import NoSuchElementException
  6. from selenium.webdriver.remote.webelement import WebElement
  7. PORT = 3392
  8. IGNORED_CLASSES = ['row', 'column', 'q-field', 'q-field__label', 'q-input']
  9. class User():
  10. def __init__(self, selenium: webdriver.Chrome) -> None:
  11. self.selenium = selenium
  12. self.server_thread = None
  13. def start_server(self) -> None:
  14. '''Start the webserver in a separate thread. This is the equivalent of `ui.run()` in a normal script.'''
  15. self.server_thread = threading.Thread(target=ui.run, kwargs={'port': PORT, 'show': False, 'reload': False})
  16. self.server_thread.start()
  17. def stop_server(self) -> None:
  18. '''Stop the webserver.'''
  19. self.selenium.close()
  20. globals.server.should_exit = True
  21. self.server_thread.join()
  22. def open(self, path: str) -> None:
  23. if self.server_thread is None:
  24. self.start_server()
  25. start = time.time()
  26. while True:
  27. try:
  28. self.selenium.get(f'http://localhost:{PORT}{path}')
  29. break
  30. except Exception:
  31. if time.time() - start > 3:
  32. raise
  33. time.sleep(0.1)
  34. if not self.server_thread.is_alive():
  35. raise RuntimeError('The NiceGUI server has stopped running')
  36. def should_see(self, text: str) -> None:
  37. assert self.selenium.title == text or self.find(text).text == text
  38. def click(self, target_text: str) -> None:
  39. self.find(target_text).click()
  40. def find(self, text: str) -> WebElement:
  41. try:
  42. return self.selenium.find_element_by_xpath(f'//*[contains(text(),"{text}")]')
  43. except NoSuchElementException:
  44. raise AssertionError(f'Could not find "{text}" on:\n{self.page()}')
  45. def page(self) -> str:
  46. return f'Title: {self.selenium.title}\n\n' + self.content(self.selenium.find_element_by_tag_name('body'))
  47. def content(self, element: WebElement, indent: str = '') -> str:
  48. content = ''
  49. classes: list[str] = []
  50. for child in element.find_elements_by_xpath('./*'):
  51. is_element = False
  52. is_group = False
  53. render_children = True
  54. assert isinstance(child, WebElement)
  55. if not child.find_elements_by_xpath('./*') and child.text:
  56. is_element = True
  57. content += f'{indent}{child.text}'
  58. classes = child.get_attribute('class').strip().split()
  59. if classes:
  60. if classes[0] in ['row', 'column']:
  61. content += classes[0]
  62. is_element = True
  63. is_group = True
  64. if classes[0] == 'q-field':
  65. try:
  66. name = child.find_element_by_class_name('q-field__label').text
  67. except NoSuchElementException:
  68. name = ''
  69. input = child.find_element_by_tag_name('input')
  70. value = input.get_attribute('value') or input.get_attribute('placeholder')
  71. content += f'{indent}{name}: {value}'
  72. render_children = False
  73. is_element = True
  74. [classes.remove(c) for c in IGNORED_CLASSES if c in classes]
  75. for i, c in enumerate(classes):
  76. classes[i] = c.removeprefix('q-field--')
  77. if is_element:
  78. content += f' [class: {" ".join(classes)}]'
  79. if is_element:
  80. content += '\n'
  81. if render_children:
  82. content += self.content(child, indent + (' ' if is_group else ''))
  83. return content
  84. def get_tags(self, name: str) -> list[WebElement]:
  85. return self.selenium.find_elements_by_tag_name(name)
  86. def get_attributes(self, tag: str, attribute: str) -> list[str]:
  87. return [t.get_attribute(attribute) for t in self.get_tags(tag)]
  88. def sleep(self, t: float) -> None:
  89. time.sleep(t)