test_element.py 4.3 KB

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