test_element.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. from nicegui import ui
  2. from nicegui.elements.element import Element
  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. def test_input_with_multi_word_error_message(screen: Screen):
  36. input = ui.input(label='some input')
  37. ui.button('set error', on_click=lambda: input.props('error error-message="Some multi word error message"'))
  38. screen.open('/')
  39. screen.should_not_contain('Some multi word error message')
  40. screen.click('set error')
  41. screen.should_contain('Some multi word error message')
  42. def test_classes(screen: Screen):
  43. label = ui.label('Some label')
  44. def assert_classes(classes: str) -> None:
  45. assert screen.selenium.find_element(By.XPATH,
  46. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  47. screen.open('/')
  48. assert_classes('')
  49. label.classes('one')
  50. assert_classes('one')
  51. label.classes('one')
  52. assert_classes('one')
  53. label.classes('two three')
  54. assert_classes('one two three')
  55. label.classes(remove='two')
  56. assert_classes('one three')
  57. label.classes(replace='four')
  58. assert_classes('four')
  59. def test_style_parsing():
  60. assert Element._parse_style('color: red; background-color: green') == {'color': 'red', 'background-color': 'green'}
  61. assert Element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'}
  62. assert Element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'}
  63. def test_props_parsing():
  64. assert Element._parse_props('one two=1 three="abc def"') == {'one': True, 'two': '1', 'three': 'abc def'}
  65. assert Element._parse_props('loading percentage=12.5') == {'loading': True, 'percentage': '12.5'}
  66. assert Element._parse_props('size=50%') == {'size': '50%'}
  67. def test_style(screen: Screen):
  68. label = ui.label('Some label')
  69. def assert_style(style: str) -> None:
  70. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  71. screen.open('/')
  72. assert_style('')
  73. label.style('color: red')
  74. assert_style('color: red;')
  75. label.style('color: red')
  76. assert_style('color: red;')
  77. label.style('color: blue')
  78. assert_style('color: blue;')
  79. label.style('font-weight: bold')
  80. assert_style('color: blue; font-weight: bold;')
  81. label.style(remove='color: blue')
  82. assert_style('font-weight: bold;')
  83. label.style(replace='text-decoration: underline')
  84. assert_style('text-decoration: underline;')
  85. label.style('color: blue;')
  86. assert_style('text-decoration: underline; color: blue;')
  87. def test_props(screen: Screen):
  88. input = ui.input()
  89. def assert_props(*props: str) -> None:
  90. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  91. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  92. screen.open('/')
  93. assert_props('standard', 'labeled')
  94. input.props('dark')
  95. assert_props('standard', 'labeled', 'dark')
  96. input.props('dark')
  97. assert_props('standard', 'labeled', 'dark')
  98. input.props(remove='dark')
  99. assert_props('standard', 'labeled')