test_element.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from selenium.webdriver.common.by import By
  2. from nicegui import ui
  3. from nicegui.element import Element
  4. from .screen import Screen
  5. def test_classes(screen: Screen):
  6. label = ui.label('Some label')
  7. def assert_classes(classes: str) -> None:
  8. assert screen.selenium.find_element(By.XPATH,
  9. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  10. screen.open('/')
  11. screen.wait(0.5)
  12. assert_classes('')
  13. label.classes('one')
  14. assert_classes('one')
  15. label.classes('one')
  16. assert_classes('one')
  17. label.classes('two three')
  18. assert_classes('one two three')
  19. label.classes(remove='two')
  20. assert_classes('one three')
  21. label.classes(replace='four')
  22. assert_classes('four')
  23. def test_style_parsing():
  24. assert Element._parse_style(None) == {}
  25. assert Element._parse_style('color: red; background-color: green') == {'color': 'red', 'background-color': 'green'}
  26. assert Element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'}
  27. assert Element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'}
  28. assert Element._parse_style('box-shadow: 0 0 0.5em #1976d2') == {'box-shadow': '0 0 0.5em #1976d2'}
  29. def test_props_parsing():
  30. assert Element._parse_props(None) == {}
  31. assert Element._parse_props('one two=1 three="abc def"') == {'one': True, 'two': '1', 'three': 'abc def'}
  32. assert Element._parse_props('loading percentage=12.5') == {'loading': True, 'percentage': '12.5'}
  33. assert Element._parse_props('size=50%') == {'size': '50%'}
  34. assert Element._parse_props('href=http://192.168.42.100/') == {'href': 'http://192.168.42.100/'}
  35. assert Element._parse_props('hint="Your \\"given\\" name"') == {'hint': 'Your "given" name'}
  36. assert Element._parse_props('input-style="{ color: #ff0000 }"') == {'input-style': '{ color: #ff0000 }'}
  37. def test_style(screen: Screen):
  38. label = ui.label('Some label')
  39. def assert_style(style: str) -> None:
  40. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  41. screen.open('/')
  42. screen.wait(0.5)
  43. assert_style('')
  44. label.style('color: red')
  45. assert_style('color: red;')
  46. label.style('color: red')
  47. assert_style('color: red;')
  48. label.style('color: blue')
  49. assert_style('color: blue;')
  50. label.style('font-weight: bold')
  51. assert_style('color: blue; font-weight: bold;')
  52. label.style(remove='color: blue')
  53. assert_style('font-weight: bold;')
  54. label.style(replace='text-decoration: underline')
  55. assert_style('text-decoration: underline;')
  56. label.style('color: blue;')
  57. assert_style('text-decoration: underline; color: blue;')
  58. def test_props(screen: Screen):
  59. input = ui.input()
  60. def assert_props(*props: str) -> None:
  61. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  62. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  63. screen.open('/')
  64. screen.wait(0.5)
  65. assert_props('standard')
  66. input.props('dark')
  67. assert_props('standard', 'dark')
  68. input.props('dark')
  69. assert_props('standard', 'dark')
  70. input.props(remove='dark')
  71. assert_props('standard')
  72. def test_remove_and_clear(screen: Screen):
  73. with ui.row() as row:
  74. ui.label('Label A')
  75. b = ui.label('Label B')
  76. ui.label('Label C')
  77. ui.button('Remove B', on_click=lambda: row.remove(b))
  78. ui.button('Remove 0', on_click=lambda: row.remove(0))
  79. ui.button('Clear', on_click=lambda: row.clear())
  80. screen.open('/')
  81. screen.should_contain('Label A')
  82. screen.should_contain('Label B')
  83. screen.should_contain('Label C')
  84. screen.click('Remove B')
  85. screen.wait(0.5)
  86. screen.should_contain('Label A')
  87. screen.should_not_contain('Label B')
  88. screen.should_contain('Label C')
  89. screen.click('Remove 0')
  90. screen.wait(0.5)
  91. screen.should_not_contain('Label A')
  92. screen.should_not_contain('Label B')
  93. screen.should_contain('Label C')
  94. screen.click('Clear')
  95. screen.wait(0.5)
  96. screen.should_not_contain('Label A')
  97. screen.should_not_contain('Label B')
  98. screen.should_not_contain('Label C')