test_element.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from nicegui import ui
  2. from selenium.webdriver.common.action_chains import ActionChains
  3. from selenium.webdriver.common.by import By
  4. from .screen import Screen
  5. def test_keyboard(screen: Screen):
  6. result = ui.label('')
  7. ui.keyboard(on_key=lambda e: result.set_text(f'{e.key, e.action}'))
  8. screen.open('/')
  9. assert screen.selenium.find_element(By.TAG_NAME, 'base')
  10. assert any(s.endswith('keyboard.js') for s in screen.get_attributes('script', 'src'))
  11. assert screen.selenium.find_element(By.XPATH, '//span[@data-nicegui="keyboard"]')
  12. ActionChains(screen.selenium).send_keys('t').perform()
  13. screen.should_contain('t, KeyboardAction(keydown=False, keyup=True, repeat=False)')
  14. def test_joystick(screen: Screen):
  15. ui.joystick()
  16. screen.open('/')
  17. assert any(s.endswith('keyboard.js') for s in screen.get_attributes('script', 'src'))
  18. assert screen.selenium.find_element(By.XPATH, '//div[@data-nicegui="joystick"]')
  19. def test_classes(screen: Screen):
  20. label = ui.label('Some label')
  21. def assert_classes(classes: str) -> None:
  22. assert screen.selenium.find_element(By.XPATH,
  23. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  24. screen.open('/')
  25. assert_classes('')
  26. label.classes('one')
  27. assert_classes('one')
  28. label.classes('one')
  29. assert_classes('one')
  30. label.classes('two three')
  31. assert_classes('one two three')
  32. label.classes(remove='two')
  33. assert_classes('one three')
  34. label.classes(replace='four')
  35. assert_classes('four')
  36. def test_style(screen: Screen):
  37. label = ui.label('Some label')
  38. def assert_style(style: str) -> None:
  39. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  40. screen.open('/')
  41. assert_style('')
  42. label.style('color: red')
  43. assert_style('color: red;')
  44. label.style('color: red')
  45. assert_style('color: red;')
  46. label.style('color: blue')
  47. assert_style('color: blue;')
  48. label.style('font-weight: bold')
  49. assert_style('color: blue; font-weight: bold;')
  50. label.style(remove='color: blue')
  51. assert_style('font-weight: bold;')
  52. label.style(replace='text-decoration: underline')
  53. assert_style('text-decoration: underline;')
  54. label.style('color: blue;')
  55. assert_style('text-decoration: underline; color: blue;')
  56. def test_props(screen: Screen):
  57. input = ui.input()
  58. def assert_props(*props: str) -> None:
  59. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  60. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  61. screen.open('/')
  62. assert_props('standard', 'labeled')
  63. input.props('dark')
  64. assert_props('standard', 'labeled', 'dark')
  65. input.props('dark')
  66. assert_props('standard', 'labeled', 'dark')
  67. input.props(remove='dark')
  68. assert_props('standard', 'labeled')