test_element.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import pytest
  2. from nicegui import ui
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from selenium.webdriver.common.by import By
  5. from .screen import Screen
  6. def test_keyboard(screen: Screen):
  7. result = ui.label()
  8. ui.keyboard(on_key=lambda e: result.set_text(f'{e.key, e.action}'))
  9. screen.open('/')
  10. assert screen.selenium.find_element(By.TAG_NAME, 'base')
  11. assert any(s.endswith('keyboard.js') for s in screen.get_attributes('script', 'src'))
  12. assert screen.selenium.find_element(By.XPATH, '//span[@data-nicegui="keyboard"]')
  13. ActionChains(screen.selenium).send_keys('t').perform()
  14. screen.should_contain('t, KeyboardAction(keydown=False, keyup=True, repeat=False)')
  15. def test_joystick(screen: Screen):
  16. ui.joystick(on_move=lambda e: coordinates.set_text(f'move {e.data.vector.x:.3f}, {e.data.vector.y:.3f}'),
  17. on_end=lambda e: coordinates.set_text('end 0, 0'))
  18. coordinates = ui.label('start 0, 0')
  19. screen.open('/')
  20. assert any(s.endswith('keyboard.js') for s in screen.get_attributes('script', 'src'))
  21. joystick = screen.selenium.find_element(By.XPATH, '//div[@data-nicegui="joystick"]')
  22. assert joystick
  23. screen.should_contain('start 0, 0')
  24. ActionChains(screen.selenium).move_to_element_with_offset(joystick, 25, 25)\
  25. .click_and_hold().pause(1).move_by_offset(20, 20).pause(1).perform()
  26. screen.should_contain('move 0.400, -0.400')
  27. ActionChains(screen.selenium).move_to_element_with_offset(joystick, 25, 25).click().perform()
  28. screen.should_contain('end 0, 0')
  29. @pytest.mark.skip(reason='not jet fixed; see https://github.com/zauberzeug/nicegui/issues/98')
  30. def test_input_with_multi_word_error_message(screen: Screen):
  31. input = ui.input(label='some input')
  32. ui.button('set error', on_click=lambda: input.props('error-message="Some multi word error message" error=error'))
  33. screen.open('/')
  34. screen.should_not_contain('Some multi word error message')
  35. screen.click('set error')
  36. screen.should_contain('Some multi word error message')
  37. def test_classes(screen: Screen):
  38. label = ui.label('Some label')
  39. def assert_classes(classes: str) -> None:
  40. assert screen.selenium.find_element(By.XPATH,
  41. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  42. screen.open('/')
  43. assert_classes('')
  44. label.classes('one')
  45. assert_classes('one')
  46. label.classes('one')
  47. assert_classes('one')
  48. label.classes('two three')
  49. assert_classes('one two three')
  50. label.classes(remove='two')
  51. assert_classes('one three')
  52. label.classes(replace='four')
  53. assert_classes('four')
  54. def test_style(screen: Screen):
  55. label = ui.label('Some label')
  56. def assert_style(style: str) -> None:
  57. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  58. screen.open('/')
  59. assert_style('')
  60. label.style('color: red')
  61. assert_style('color: red;')
  62. label.style('color: red')
  63. assert_style('color: red;')
  64. label.style('color: blue')
  65. assert_style('color: blue;')
  66. label.style('font-weight: bold')
  67. assert_style('color: blue; font-weight: bold;')
  68. label.style(remove='color: blue')
  69. assert_style('font-weight: bold;')
  70. label.style(replace='text-decoration: underline')
  71. assert_style('text-decoration: underline;')
  72. label.style('color: blue;')
  73. assert_style('text-decoration: underline; color: blue;')
  74. def test_props(screen: Screen):
  75. input = ui.input()
  76. def assert_props(*props: str) -> None:
  77. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  78. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  79. screen.open('/')
  80. assert_props('standard', 'labeled')
  81. input.props('dark')
  82. assert_props('standard', 'labeled', 'dark')
  83. input.props('dark')
  84. assert_props('standard', 'labeled', 'dark')
  85. input.props(remove='dark')
  86. assert_props('standard', 'labeled')