test_element.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from selenium.webdriver.common.action_chains import ActionChains
  2. from selenium.webdriver.common.by import By
  3. from nicegui import ui
  4. from nicegui.element import Element
  5. from .screen import Screen
  6. def test_keyboard(screen: Screen):
  7. result = ui.label()
  8. keyboard = ui.keyboard(on_key=lambda e: result.set_text(f'{e.key, e.action}'))
  9. screen.open('/')
  10. assert screen.selenium.find_element(By.ID, keyboard.id)
  11. ActionChains(screen.selenium).send_keys('t').perform()
  12. screen.should_contain('t, KeyboardAction(keydown=False, keyup=True, repeat=False)')
  13. def test_joystick(screen: Screen):
  14. j = ui.joystick(on_move=lambda e: coordinates.set_text(f'move {e.x:.3f}, {e.y:.3f}'),
  15. on_end=lambda _: coordinates.set_text('end 0, 0'))
  16. coordinates = ui.label('start 0, 0')
  17. screen.open('/')
  18. joystick = screen.selenium.find_element(By.ID, j.id)
  19. assert joystick
  20. screen.should_contain('start 0, 0')
  21. ActionChains(screen.selenium).move_to_element_with_offset(joystick, 25, 25)\
  22. .click_and_hold().pause(1).move_by_offset(20, 20).pause(1).perform()
  23. screen.should_contain('move 0.400, -0.400')
  24. ActionChains(screen.selenium).move_to_element_with_offset(joystick, 25, 25).click().perform()
  25. screen.should_contain('end 0, 0')
  26. def test_styling_joystick(screen: Screen):
  27. j = ui.joystick().style('background-color: gray;').classes('shadow-lg')
  28. screen.open('/')
  29. joystick = screen.selenium.find_element(By.ID, j.id)
  30. assert 'background-color: gray;' in joystick.get_attribute('style')
  31. assert 'shadow-lg' in joystick.get_attribute('class')
  32. def test_input_with_multi_word_error_message(screen: Screen):
  33. input = ui.input(label='some input')
  34. ui.button('set error', on_click=lambda: input.props('error error-message="Some multi word error message"'))
  35. screen.open('/')
  36. screen.should_not_contain('Some multi word error message')
  37. screen.click('set error')
  38. screen.should_contain('Some multi word error message')
  39. def test_classes(screen: Screen):
  40. label = ui.label('Some label')
  41. def assert_classes(classes: str) -> None:
  42. assert screen.selenium.find_element(By.XPATH,
  43. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  44. screen.open('/')
  45. assert_classes('')
  46. label.classes('one')
  47. assert_classes('one')
  48. label.classes('one')
  49. assert_classes('one')
  50. label.classes('two three')
  51. assert_classes('one two three')
  52. label.classes(remove='two')
  53. assert_classes('one three')
  54. label.classes(replace='four')
  55. assert_classes('four')
  56. def test_style_parsing():
  57. assert Element._parse_style('color: red; background-color: green') == {'color': 'red', 'background-color': 'green'}
  58. assert Element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'}
  59. assert Element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'}
  60. assert Element._parse_style('box-shadow: 0 0 0.5em #1976d2') == {'box-shadow': '0 0 0.5em #1976d2'}
  61. def test_props_parsing():
  62. assert Element._parse_props('one two=1 three="abc def"') == {'one': True, 'two': '1', 'three': 'abc def'}
  63. assert Element._parse_props('loading percentage=12.5') == {'loading': True, 'percentage': '12.5'}
  64. assert Element._parse_props('size=50%') == {'size': '50%'}
  65. def test_style(screen: Screen):
  66. label = ui.label('Some label')
  67. def assert_style(style: str) -> None:
  68. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  69. screen.open('/')
  70. assert_style('')
  71. label.style('color: red')
  72. assert_style('color: red;')
  73. label.style('color: red')
  74. assert_style('color: red;')
  75. label.style('color: blue')
  76. assert_style('color: blue;')
  77. label.style('font-weight: bold')
  78. assert_style('color: blue; font-weight: bold;')
  79. label.style(remove='color: blue')
  80. assert_style('font-weight: bold;')
  81. label.style(replace='text-decoration: underline')
  82. assert_style('text-decoration: underline;')
  83. label.style('color: blue;')
  84. assert_style('text-decoration: underline; color: blue;')
  85. def test_props(screen: Screen):
  86. input = ui.input()
  87. def assert_props(*props: str) -> None:
  88. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  89. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  90. screen.open('/')
  91. assert_props('standard', 'labeled')
  92. input.props('dark')
  93. assert_props('standard', 'labeled', 'dark')
  94. input.props('dark')
  95. assert_props('standard', 'labeled', 'dark')
  96. input.props(remove='dark')
  97. assert_props('standard', 'labeled')
  98. def test_adding_toggle_options(screen: Screen):
  99. label = ui.label()
  100. toggle = ui.toggle(['A', 'B', 'C'], on_change=lambda e: label.set_text(f'Choice: {e.value}'))
  101. def add_option():
  102. toggle.options.append('D')
  103. toggle.update()
  104. ui.button('Add option', on_click=add_option)
  105. screen.open('/')
  106. screen.click('A')
  107. screen.should_contain('Choice: A')
  108. screen.should_not_contain('D')
  109. screen.click('Add option')
  110. screen.should_contain('D')
  111. screen.click('D')
  112. screen.should_contain('Choice: D')
  113. def test_base64_image(screen: Screen):
  114. ui.image('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABmJLR0QA/wD/AP+gvaeTAAAEHElEQVRoge2ZXUxbZRjHf6enH3QtBQ7paIFlMO2AMXTGqZE40bCpiRdzF06Nsu3O6G68MH5MnYkk3vhx4cWCJppFlvgZ74wXbsZdLCYaQMeWUWM30EJZgVM+WtpS2uNFoQzTU3pKu2O0v8v38//Pe57ned8cKFOmTBk9EVR7vrxsRlJ6gR7AfdMUrWcC6EcWTnK4fSnbAKPqVEl5C3ipRMLypR54GUkBeCXbAEOOyUdKoahAjqp15DKg12eTDZdaRy4DN43p+1s55HTwVF0Vk/taNM3V3UCDxUStSWQ4HKPDXsFwOK5pvm4GTILADquZbslGPKUAsNdRwXg8wQ6rOe911NPo2UvKplXmYOcWM957Par9wrnL6xv2786qVbcT8EUTSOdH+Co4T//kLE0XfgfgwcFRpPMjea+jm4GkohBaTuKxmhlaiNFoMZFS4Jf5KKHlZN7rqBeyEvPF7kYO11UBsKdyLUuGH2jjNV+Qt0en8lpHtxN41RfkyUt+APYPjfJNcJ7v5TB7f77KJxOhvNfRzcDVaPpqM51Ick6O4DQbuTC7yMBClMml5bzX0bUOdNgtXAzHAGi3WRiOaKsBoGMa1cy/LY0Wi7IBvfl/GhCAJ+qq+HbPdgL7Whi8+5YN59zjsOLr9ODr9PB6s7OQbbOiuRI7jAa+7tjGAcmeaQtukLdNgsBHbfWZW2atSdS6rSqaDAjAp7saOSDZSSoKpwOznJmcw7uYO3+/uL2W2+wVm9GpiiYD3ZKNg85KAI57A3w4vnHJv9Vq5o1mJ9FUCqMgYBLUS08haIqBY+4aAK5E4lyJxDnV4ub0rgaOuasRswgTgL7WeqwGA73XpjIPl2Ki6QQ6q6wAbDUb+fHO5kwZP+qu5qDTwaGLf64bf8RdTbdkYzgc492xGU40FS94V9F0Ai5L2q9kEunzyxz3BhhYiALwmLOSh24IbKfZyHseFykFnh0JkFBKczPRZMBqSA//eCLE894Ap/wyDw+NsZhMAWTiA+B9Tx21JpG+cZmf5haLKHk9mgysCp1bTmXaZhJJvIvpq3HTSpq83V7BM65qAHrc1chdrchdrdjE9HbPNUjIXa2bV49GA6tC22yWTJsoCLhXPq3ZRHKlbW1OpWigxihSYxQzMWMxCNQYi1MLNAXxZ9fnuKOygkckO0+7qjgrR3hhWy0uc3qZ72bCAPwWjmd9mPvv28kW0UDfuMyJP4JFkK/RwAd/zfD4Vgd3OaycaW9c1/dDKMLn1+eAtQf7P1kN41gqe38haPqE4imF7sFR3hmbZiyWIKEo+KJL9F6b4tFfx1jeINMMLcQYWIjijyU2JfpG/tMvsokSSSkAYVytJ5eB/hIoKQxBUdWiHsSycHLlz0gP6T8lepD+xTQjvKnT/mXKlCmzAX8Dl7JCqRHaepQAAAAASUVORK5CYII=').style('width: 50px;')
  115. screen.open('/')
  116. screen.wait(0.2)
  117. image = screen.selenium.find_element(By.CLASS_NAME, 'q-img__image')
  118. assert 'data:image/png;base64,iVB' in image.get_attribute('src')