test_element.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from selenium.webdriver.common.by import By
  2. from nicegui import ui
  3. from .screen import Screen
  4. def test_classes(screen: Screen):
  5. label = ui.label('Some label')
  6. def assert_classes(classes: str) -> None:
  7. assert screen.selenium.find_element(By.XPATH,
  8. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  9. screen.open('/')
  10. screen.wait(0.5)
  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. # pylint: disable=protected-access
  24. assert ui.element._parse_style(None) == {} # pylint: disable=use-implicit-booleaness-not-comparison
  25. assert ui.element._parse_style('color: red; background-color: blue') == {'color': 'red', 'background-color': 'blue'}
  26. assert ui.element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'}
  27. assert ui.element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'}
  28. assert ui.element._parse_style('box-shadow: 0 0 0.5em #1976d2') == {'box-shadow': '0 0 0.5em #1976d2'}
  29. def test_props_parsing():
  30. # pylint: disable=protected-access
  31. assert ui.element._parse_props(None) == {} # pylint: disable=use-implicit-booleaness-not-comparison
  32. assert ui.element._parse_props('one two=1 three="abc def"') == {'one': True, 'two': '1', 'three': 'abc def'}
  33. assert ui.element._parse_props('loading percentage=12.5') == {'loading': True, 'percentage': '12.5'}
  34. assert ui.element._parse_props('size=50%') == {'size': '50%'}
  35. assert ui.element._parse_props('href=http://192.168.42.100/') == {'href': 'http://192.168.42.100/'}
  36. assert ui.element._parse_props('hint="Your \\"given\\" name"') == {'hint': 'Your "given" name'}
  37. assert ui.element._parse_props('input-style="{ color: #ff0000 }"') == {'input-style': '{ color: #ff0000 }'}
  38. def test_style(screen: Screen):
  39. label = ui.label('Some label')
  40. def assert_style(style: str) -> None:
  41. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  42. screen.open('/')
  43. screen.wait(0.5)
  44. assert_style('')
  45. label.style('color: red')
  46. assert_style('color: red;')
  47. label.style('color: red')
  48. assert_style('color: red;')
  49. label.style('color: blue')
  50. assert_style('color: blue;')
  51. label.style('font-weight: bold')
  52. assert_style('color: blue; font-weight: bold;')
  53. label.style(remove='color: blue')
  54. assert_style('font-weight: bold;')
  55. label.style(replace='text-decoration: underline')
  56. assert_style('text-decoration: underline;')
  57. label.style('color: blue;')
  58. assert_style('text-decoration: underline; color: blue;')
  59. def test_props(screen: Screen):
  60. input_ = ui.input()
  61. def assert_props(*props: str) -> None:
  62. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  63. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  64. screen.open('/')
  65. screen.wait(0.5)
  66. assert_props('standard')
  67. input_.props('dark')
  68. assert_props('standard', 'dark')
  69. input_.props('dark')
  70. assert_props('standard', 'dark')
  71. input_.props(remove='dark')
  72. assert_props('standard')
  73. def test_move(screen: Screen):
  74. with ui.card() as a:
  75. ui.label('A')
  76. x = ui.label('X')
  77. with ui.card() as b:
  78. ui.label('B')
  79. ui.button('Move X to A', on_click=lambda: x.move(a))
  80. ui.button('Move X to B', on_click=lambda: x.move(b))
  81. ui.button('Move X to top', on_click=lambda: x.move(target_index=0))
  82. screen.open('/')
  83. assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y']
  84. screen.click('Move X to B')
  85. screen.wait(0.5)
  86. assert screen.find('A').location['y'] < screen.find('B').location['y'] < screen.find('X').location['y']
  87. screen.click('Move X to A')
  88. screen.wait(0.5)
  89. assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y']
  90. screen.click('Move X to top')
  91. screen.wait(0.5)
  92. assert screen.find('X').location['y'] < screen.find('A').location['y'] < screen.find('B').location['y']
  93. def test_xss(screen: Screen):
  94. ui.label('</script><script>alert(1)</script>')
  95. ui.label('<b>Bold 1</b>, `code`, copy&paste, multi\nline')
  96. ui.button('Button', on_click=lambda: (
  97. ui.label('</script><script>alert(2)</script>'),
  98. ui.label('<b>Bold 2</b>, `code`, copy&paste, multi\nline'),
  99. ))
  100. screen.open('/')
  101. screen.click('Button')
  102. screen.should_contain('</script><script>alert(1)</script>')
  103. screen.should_contain('</script><script>alert(2)</script>')
  104. screen.should_contain('<b>Bold 1</b>, `code`, copy&paste, multi\nline')
  105. screen.should_contain('<b>Bold 2</b>, `code`, copy&paste, multi\nline')