test_element.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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(on_move=lambda e: coordinates.set_text(f'move {e.data.vector.x:.3f}, {e.data.vector.y:.3f}'),
  16. on_end=lambda e: coordinates.set_text('end 0, 0'))
  17. coordinates = ui.label('start 0, 0')
  18. screen.open('/')
  19. assert any(s.endswith('keyboard.js') for s in screen.get_attributes('script', 'src'))
  20. joystick = screen.selenium.find_element(By.XPATH, '//div[@data-nicegui="joystick"]')
  21. assert joystick
  22. screen.should_contain('start 0, 0')
  23. ActionChains(screen.selenium).move_to_element_with_offset(joystick, 25, 25)\
  24. .click_and_hold().pause(1).move_by_offset(20, 20).pause(1).perform()
  25. screen.should_contain('move 0.400, -0.400')
  26. ActionChains(screen.selenium).move_to_element_with_offset(joystick, 25, 25).click().perform()
  27. screen.should_contain('end 0, 0')
  28. def test_styling_joystick(screen: Screen):
  29. ui.joystick().style('background-color: gray;').classes('shadow-lg')
  30. screen.open('/')
  31. joystick = screen.selenium.find_element(By.XPATH, '//div[@data-nicegui="joystick"]')
  32. assert 'background-color: gray;' in joystick.get_attribute('style')
  33. assert 'shadow-lg' in joystick.get_attribute('class')
  34. def test_input_with_multi_word_error_message(screen: Screen):
  35. input = ui.input(label='some input')
  36. ui.button('set error', on_click=lambda: input.props('error error-message="Some multi word error message"'))
  37. screen.open('/')
  38. screen.should_not_contain('Some multi word error message')
  39. screen.click('set error')
  40. screen.should_contain('Some multi word error message')
  41. def test_classes(screen: Screen):
  42. label = ui.label('Some label')
  43. def assert_classes(classes: str) -> None:
  44. assert screen.selenium.find_element(By.XPATH,
  45. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  46. screen.open('/')
  47. assert_classes('')
  48. label.classes('one')
  49. assert_classes('one')
  50. label.classes('one')
  51. assert_classes('one')
  52. label.classes('two three')
  53. assert_classes('one two three')
  54. label.classes(remove='two')
  55. assert_classes('one three')
  56. label.classes(replace='four')
  57. assert_classes('four')
  58. def test_style(screen: Screen):
  59. label = ui.label('Some label')
  60. def assert_style(style: str) -> None:
  61. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  62. screen.open('/')
  63. assert_style('')
  64. label.style('color: red')
  65. assert_style('color: red;')
  66. label.style('color: red')
  67. assert_style('color: red;')
  68. label.style('color: blue')
  69. assert_style('color: blue;')
  70. label.style('font-weight: bold')
  71. assert_style('color: blue; font-weight: bold;')
  72. label.style(remove='color: blue')
  73. assert_style('font-weight: bold;')
  74. label.style(replace='text-decoration: underline')
  75. assert_style('text-decoration: underline;')
  76. label.style('color: blue;')
  77. assert_style('text-decoration: underline; color: blue;')
  78. def test_props(screen: Screen):
  79. input = ui.input()
  80. def assert_props(*props: str) -> None:
  81. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  82. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  83. screen.open('/')
  84. assert_props('standard', 'labeled')
  85. input.props('dark')
  86. assert_props('standard', 'labeled', 'dark')
  87. input.props('dark')
  88. assert_props('standard', 'labeled', 'dark')
  89. input.props(remove='dark')
  90. assert_props('standard', 'labeled')