test_element.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. def test_styling_joystick(screen: Screen):
  30. ui.joystick().style('background-color: gray;').classes('shadow-lg')
  31. screen.open('/')
  32. joystick = screen.selenium.find_element(By.XPATH, '//div[@data-nicegui="joystick"]')
  33. assert 'background-color: gray;' in joystick.get_attribute('style')
  34. assert 'shadow-lg' in joystick.get_attribute('class')
  35. @pytest.mark.skip(reason='not jet fixed; see https://github.com/zauberzeug/nicegui/issues/98')
  36. def test_input_with_multi_word_error_message(screen: Screen):
  37. input = ui.input(label='some input')
  38. ui.button('set error', on_click=lambda: input.props('error-message="Some multi word error message" error=error'))
  39. screen.open('/')
  40. screen.should_not_contain('Some multi word error message')
  41. screen.click('set error')
  42. screen.should_contain('Some multi word error message')
  43. def test_classes(screen: Screen):
  44. label = ui.label('Some label')
  45. def assert_classes(classes: str) -> None:
  46. assert screen.selenium.find_element(By.XPATH,
  47. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  48. screen.open('/')
  49. assert_classes('')
  50. label.classes('one')
  51. assert_classes('one')
  52. label.classes('one')
  53. assert_classes('one')
  54. label.classes('two three')
  55. assert_classes('one two three')
  56. label.classes(remove='two')
  57. assert_classes('one three')
  58. label.classes(replace='four')
  59. assert_classes('four')
  60. def test_style(screen: Screen):
  61. label = ui.label('Some label')
  62. def assert_style(style: str) -> None:
  63. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  64. screen.open('/')
  65. assert_style('')
  66. label.style('color: red')
  67. assert_style('color: red;')
  68. label.style('color: red')
  69. assert_style('color: red;')
  70. label.style('color: blue')
  71. assert_style('color: blue;')
  72. label.style('font-weight: bold')
  73. assert_style('color: blue; font-weight: bold;')
  74. label.style(remove='color: blue')
  75. assert_style('font-weight: bold;')
  76. label.style(replace='text-decoration: underline')
  77. assert_style('text-decoration: underline;')
  78. label.style('color: blue;')
  79. assert_style('text-decoration: underline; color: blue;')
  80. def test_props(screen: Screen):
  81. input = ui.input()
  82. def assert_props(*props: str) -> None:
  83. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  84. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  85. screen.open('/')
  86. assert_props('standard', 'labeled')
  87. input.props('dark')
  88. assert_props('standard', 'labeled', 'dark')
  89. input.props('dark')
  90. assert_props('standard', 'labeled', 'dark')
  91. input.props(remove='dark')
  92. assert_props('standard', 'labeled')