test_element.py 4.2 KB

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