test_element.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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')
  106. def test_default_props():
  107. ui.button.default_props('rounded outline')
  108. button_a = ui.button('Button A')
  109. button_b = ui.button('Button B')
  110. assert button_a._props.get('rounded') is True, 'default props are set'
  111. assert button_a._props.get('outline') is True
  112. assert button_b._props.get('rounded') is True
  113. assert button_b._props.get('outline') is True
  114. ui.button.default_props(remove='outline')
  115. button_c = ui.button('Button C')
  116. assert button_c._props.get('outline') is None, '"outline" prop was removed'
  117. assert button_c._props.get('rounded') is True, 'other props are still there'
  118. ui.input.default_props('filled')
  119. input_a = ui.input()
  120. assert input_a._props.get('filled') is True
  121. assert input_a._props.get('rounded') is None, 'default props of ui.button do not affect ui.input'
  122. class MyButton(ui.button):
  123. pass
  124. MyButton.default_props('flat')
  125. button_d = MyButton()
  126. button_e = ui.button()
  127. assert button_d._props.get('flat') is True
  128. assert button_d._props.get('rounded') is True, 'default props are inherited'
  129. assert button_e._props.get('flat') is None, 'default props of MyButton do not affect ui.button'
  130. assert button_e._props.get('rounded') is True
  131. ui.button.default_props('no-caps').default_props('no-wrap')
  132. button_f = ui.button()
  133. assert button_f._props.get('no-caps') is True
  134. assert button_f._props.get('no-wrap') is True
  135. def test_default_classes():
  136. ui.button.default_classes('bg-white text-green')
  137. button_a = ui.button('Button A')
  138. button_b = ui.button('Button B')
  139. assert 'bg-white' in button_a._classes, 'default classes are set'
  140. assert 'text-green' in button_a._classes
  141. assert 'bg-white' in button_b._classes
  142. assert 'text-green' in button_b._classes
  143. ui.button.default_classes(remove='text-green')
  144. button_c = ui.button('Button C')
  145. assert 'text-green' not in button_c._classes, '"text-green" class was removed'
  146. assert 'bg-white' in button_c._classes, 'other classes are still there'
  147. ui.input.default_classes('text-black')
  148. input_a = ui.input()
  149. assert 'text-black' in input_a._classes
  150. assert 'bg-white' not in input_a._classes, 'default classes of ui.button do not affect ui.input'
  151. class MyButton(ui.button):
  152. pass
  153. MyButton.default_classes('w-full')
  154. button_d = MyButton()
  155. button_e = ui.button()
  156. assert 'w-full' in button_d._classes
  157. assert 'bg-white' in button_d._classes, 'default classes are inherited'
  158. assert 'w-full' not in button_e._classes, 'default classes of MyButton do not affect ui.button'
  159. assert 'bg-white' in button_e._classes
  160. ui.button.default_classes('h-40').default_classes('max-h-80')
  161. button_f = ui.button()
  162. assert 'h-40' in button_f._classes
  163. assert 'max-h-80' in button_f._classes
  164. def test_default_style():
  165. ui.button.default_style('color: green; font-size: 200%')
  166. button_a = ui.button('Button A')
  167. button_b = ui.button('Button B')
  168. assert button_a._style.get('color') == 'green', 'default style is set'
  169. assert button_a._style.get('font-size') == '200%'
  170. assert button_b._style.get('color') == 'green'
  171. assert button_b._style.get('font-size') == '200%'
  172. ui.button.default_style(remove='color: green')
  173. button_c = ui.button('Button C')
  174. assert button_c._style.get('color') is None, '"color" style was removed'
  175. assert button_c._style.get('font-size') == '200%', 'other style are still there'
  176. ui.input.default_style('font-weight: 300')
  177. input_a = ui.input()
  178. assert input_a._style.get('font-weight') == '300'
  179. assert input_a._style.get('font-size') is None, 'default style of ui.button does not affect ui.input'
  180. class MyButton(ui.button):
  181. pass
  182. MyButton.default_style('font-family: courier')
  183. button_d = MyButton()
  184. button_e = ui.button()
  185. assert button_d._style.get('font-family') == 'courier'
  186. assert button_d._style.get('font-size') == '200%', 'default style is inherited'
  187. assert button_e._style.get('font-family') is None, 'default style of MyButton does not affect ui.button'
  188. assert button_e._style.get('font-size') == '200%'
  189. ui.button.default_style('border: 2px').default_style('padding: 30px')
  190. button_f = ui.button()
  191. assert button_f._style.get('border') == '2px'
  192. assert button_f._style.get('padding') == '30px'