user.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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-card', '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, with_extras: bool = False) -> str:
  46. return f'Title: {self.selenium.title}\n\n' + \
  47. self.content(self.selenium.find_element_by_tag_name('body'), with_extras=with_extras)
  48. def content(self, element: WebElement, indent: str = '', with_extras: bool = False) -> str:
  49. content = ''
  50. classes: list[str] = []
  51. for child in element.find_elements_by_xpath('./*'):
  52. is_element = False
  53. is_group = False
  54. render_children = True
  55. assert isinstance(child, WebElement)
  56. if not child.find_elements_by_xpath('./*') and child.text:
  57. is_element = True
  58. content += f'{indent}{child.text}'
  59. classes = child.get_attribute('class').strip().split()
  60. if classes:
  61. if classes[0] in ['row', 'column', 'q-card']:
  62. content += classes[0].removeprefix('q-')
  63. is_element = True
  64. is_group = True
  65. if classes[0] == 'q-field':
  66. try:
  67. name = child.find_element_by_class_name('q-field__label').text
  68. except NoSuchElementException:
  69. name = ''
  70. input = child.find_element_by_tag_name('input')
  71. value = input.get_attribute('value') or input.get_attribute('placeholder')
  72. content += f'{indent}{name}: {value}'
  73. render_children = False
  74. is_element = True
  75. [classes.remove(c) for c in IGNORED_CLASSES if c in classes]
  76. for i, c in enumerate(classes):
  77. classes[i] = c.removeprefix('q-field--')
  78. if is_element and with_extras:
  79. content += f' [class: {" ".join(classes)}]'
  80. if is_element:
  81. content += '\n'
  82. if render_children:
  83. content += self.content(child, indent + (' ' if is_group else ''), with_extras)
  84. return content
  85. def get_tags(self, name: str) -> list[WebElement]:
  86. return self.selenium.find_elements_by_tag_name(name)
  87. def get_attributes(self, tag: str, attribute: str) -> list[str]:
  88. return [t.get_attribute(attribute) for t in self.get_tags(tag)]
  89. def sleep(self, t: float) -> None:
  90. time.sleep(t)