test_element.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import pytest
  2. from selenium.webdriver.common.by import By
  3. from nicegui import ui
  4. from nicegui.testing 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. # pylint: disable=protected-access
  25. assert ui.element._parse_style(None) == {} # pylint: disable=use-implicit-booleaness-not-comparison
  26. assert ui.element._parse_style('color: red; background-color: blue') == {'color': 'red', 'background-color': 'blue'}
  27. assert ui.element._parse_style('width:12em;height:34.5em') == {'width': '12em', 'height': '34.5em'}
  28. assert ui.element._parse_style('transform: translate(120.0px, 50%)') == {'transform': 'translate(120.0px, 50%)'}
  29. assert ui.element._parse_style('box-shadow: 0 0 0.5em #1976d2') == {'box-shadow': '0 0 0.5em #1976d2'}
  30. def test_props_parsing():
  31. # pylint: disable=protected-access
  32. assert ui.element._parse_props(None) == {} # pylint: disable=use-implicit-booleaness-not-comparison
  33. assert ui.element._parse_props('one two=1 three="abc def"') == {'one': True, 'two': '1', 'three': 'abc def'}
  34. assert ui.element._parse_props('loading percentage=12.5') == {'loading': True, 'percentage': '12.5'}
  35. assert ui.element._parse_props('size=50%') == {'size': '50%'}
  36. assert ui.element._parse_props('href=http://192.168.42.100/') == {'href': 'http://192.168.42.100/'}
  37. assert ui.element._parse_props('hint="Your \\"given\\" name"') == {'hint': 'Your "given" name'}
  38. assert ui.element._parse_props('input-style="{ color: #ff0000 }"') == {'input-style': '{ color: #ff0000 }'}
  39. assert ui.element._parse_props('empty=""') == {'empty': ''}
  40. assert ui.element._parse_props("empty=''") == {'empty': ''}
  41. assert ui.element._parse_props("""hint='Your \\"given\\" name'""") == {'hint': 'Your "given" name'}
  42. assert ui.element._parse_props("one two=1 three='abc def'") == {'one': True, 'two': '1', 'three': 'abc def'}
  43. assert ui.element._parse_props('''three='abc def' four="hhh jjj"''') == {'three': 'abc def', 'four': 'hhh jjj', }
  44. assert ui.element._parse_props('''foo="quote'quote"''') == {'foo': "quote'quote"}
  45. assert ui.element._parse_props("""foo='quote"quote'""") == {'foo': 'quote"quote'}
  46. assert ui.element._parse_props("""foo="single '" bar='double "'""") == {'foo': "single '", 'bar': 'double "'}
  47. assert ui.element._parse_props("""foo="single '" bar='double \\"'""") == {'foo': "single '", 'bar': 'double "'}
  48. assert ui.element._parse_props("input-style='{ color: #ff0000 }'") == {'input-style': '{ color: #ff0000 }'}
  49. assert ui.element._parse_props("""input-style='{ myquote: "quote" }'""") == {'input-style': '{ myquote: "quote" }'}
  50. def test_style(screen: Screen):
  51. label = ui.label('Some label')
  52. def assert_style(style: str) -> None:
  53. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  54. screen.open('/')
  55. screen.wait(0.5)
  56. assert_style('')
  57. label.style('color: red')
  58. assert_style('color: red;')
  59. label.style('color: red')
  60. assert_style('color: red;')
  61. label.style('color: blue')
  62. assert_style('color: blue;')
  63. label.style('font-weight: bold')
  64. assert_style('color: blue; font-weight: bold;')
  65. label.style(remove='color: blue')
  66. assert_style('font-weight: bold;')
  67. label.style(replace='text-decoration: underline')
  68. assert_style('text-decoration: underline;')
  69. label.style('color: blue;')
  70. assert_style('text-decoration: underline; color: blue;')
  71. def test_props(screen: Screen):
  72. input_ = ui.input()
  73. def assert_props(*props: str) -> None:
  74. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  75. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  76. screen.open('/')
  77. screen.wait(0.5)
  78. assert_props('standard')
  79. input_.props('dark')
  80. assert_props('standard', 'dark')
  81. input_.props('dark')
  82. assert_props('standard', 'dark')
  83. input_.props(remove='dark')
  84. assert_props('standard')
  85. def test_move(screen: Screen):
  86. with ui.card() as a:
  87. ui.label('A')
  88. x = ui.label('X')
  89. with ui.card() as b:
  90. ui.label('B')
  91. ui.button('Move X to A', on_click=lambda: x.move(a))
  92. ui.button('Move X to B', on_click=lambda: x.move(b))
  93. ui.button('Move X to top', on_click=lambda: x.move(target_index=0))
  94. screen.open('/')
  95. assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y']
  96. screen.click('Move X to B')
  97. screen.wait(0.5)
  98. assert screen.find('A').location['y'] < screen.find('B').location['y'] < screen.find('X').location['y']
  99. screen.click('Move X to A')
  100. screen.wait(0.5)
  101. assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y']
  102. screen.click('Move X to top')
  103. screen.wait(0.5)
  104. assert screen.find('X').location['y'] < screen.find('A').location['y'] < screen.find('B').location['y']
  105. def test_xss(screen: Screen):
  106. ui.label('</script><script>alert(1)</script>')
  107. ui.label('<b>Bold 1</b>, `code`, copy&paste, multi\nline')
  108. ui.button('Button', on_click=lambda: (
  109. ui.label('</script><script>alert(2)</script>'),
  110. ui.label('<b>Bold 2</b>, `code`, copy&paste, multi\nline'),
  111. ))
  112. screen.open('/')
  113. screen.click('Button')
  114. screen.should_contain('</script><script>alert(1)</script>')
  115. screen.should_contain('</script><script>alert(2)</script>')
  116. screen.should_contain('<b>Bold 1</b>, `code`, copy&paste, multi\nline')
  117. screen.should_contain('<b>Bold 2</b>, `code`, copy&paste, multi\nline')
  118. def test_default_props():
  119. ui.button.default_props('rounded outline')
  120. button_a = ui.button('Button A')
  121. button_b = ui.button('Button B')
  122. assert button_a._props.get('rounded') is True, 'default props are set'
  123. assert button_a._props.get('outline') is True
  124. assert button_b._props.get('rounded') is True
  125. assert button_b._props.get('outline') is True
  126. ui.button.default_props(remove='outline')
  127. button_c = ui.button('Button C')
  128. assert button_c._props.get('outline') is None, '"outline" prop was removed'
  129. assert button_c._props.get('rounded') is True, 'other props are still there'
  130. ui.input.default_props('filled')
  131. input_a = ui.input()
  132. assert input_a._props.get('filled') is True
  133. assert input_a._props.get('rounded') is None, 'default props of ui.button do not affect ui.input'
  134. class MyButton(ui.button):
  135. pass
  136. MyButton.default_props('flat')
  137. button_d = MyButton()
  138. button_e = ui.button()
  139. assert button_d._props.get('flat') is True
  140. assert button_d._props.get('rounded') is True, 'default props are inherited'
  141. assert button_e._props.get('flat') is None, 'default props of MyButton do not affect ui.button'
  142. assert button_e._props.get('rounded') is True
  143. ui.button.default_props('no-caps').default_props('no-wrap')
  144. button_f = ui.button()
  145. assert button_f._props.get('no-caps') is True
  146. assert button_f._props.get('no-wrap') is True
  147. def test_default_classes():
  148. ui.button.default_classes('bg-white text-green')
  149. button_a = ui.button('Button A')
  150. button_b = ui.button('Button B')
  151. assert 'bg-white' in button_a._classes, 'default classes are set'
  152. assert 'text-green' in button_a._classes
  153. assert 'bg-white' in button_b._classes
  154. assert 'text-green' in button_b._classes
  155. ui.button.default_classes(remove='text-green')
  156. button_c = ui.button('Button C')
  157. assert 'text-green' not in button_c._classes, '"text-green" class was removed'
  158. assert 'bg-white' in button_c._classes, 'other classes are still there'
  159. ui.input.default_classes('text-black')
  160. input_a = ui.input()
  161. assert 'text-black' in input_a._classes
  162. assert 'bg-white' not in input_a._classes, 'default classes of ui.button do not affect ui.input'
  163. class MyButton(ui.button):
  164. pass
  165. MyButton.default_classes('w-full')
  166. button_d = MyButton()
  167. button_e = ui.button()
  168. assert 'w-full' in button_d._classes
  169. assert 'bg-white' in button_d._classes, 'default classes are inherited'
  170. assert 'w-full' not in button_e._classes, 'default classes of MyButton do not affect ui.button'
  171. assert 'bg-white' in button_e._classes
  172. ui.button.default_classes('h-40').default_classes('max-h-80')
  173. button_f = ui.button()
  174. assert 'h-40' in button_f._classes
  175. assert 'max-h-80' in button_f._classes
  176. def test_default_style():
  177. ui.button.default_style('color: green; font-size: 200%')
  178. button_a = ui.button('Button A')
  179. button_b = ui.button('Button B')
  180. assert button_a._style.get('color') == 'green', 'default style is set'
  181. assert button_a._style.get('font-size') == '200%'
  182. assert button_b._style.get('color') == 'green'
  183. assert button_b._style.get('font-size') == '200%'
  184. ui.button.default_style(remove='color: green')
  185. button_c = ui.button('Button C')
  186. assert button_c._style.get('color') is None, '"color" style was removed'
  187. assert button_c._style.get('font-size') == '200%', 'other style are still there'
  188. ui.input.default_style('font-weight: 300')
  189. input_a = ui.input()
  190. assert input_a._style.get('font-weight') == '300'
  191. assert input_a._style.get('font-size') is None, 'default style of ui.button does not affect ui.input'
  192. class MyButton(ui.button):
  193. pass
  194. MyButton.default_style('font-family: courier')
  195. button_d = MyButton()
  196. button_e = ui.button()
  197. assert button_d._style.get('font-family') == 'courier'
  198. assert button_d._style.get('font-size') == '200%', 'default style is inherited'
  199. assert button_e._style.get('font-family') is None, 'default style of MyButton does not affect ui.button'
  200. assert button_e._style.get('font-size') == '200%'
  201. ui.button.default_style('border: 2px').default_style('padding: 30px')
  202. button_f = ui.button()
  203. assert button_f._style.get('border') == '2px'
  204. assert button_f._style.get('padding') == '30px'
  205. def test_invalid_tags(screen: Screen):
  206. good_tags = ['div', 'div-1', 'DIV', 'däv', 'div_x', '🙂']
  207. bad_tags = ['<div>', 'hi hi', 'hi/ho', 'foo$bar']
  208. for tag in good_tags:
  209. ui.element(tag)
  210. for tag in bad_tags:
  211. with pytest.raises(ValueError):
  212. ui.element(tag)
  213. screen.open('/')
  214. def test_bad_characters(screen: Screen):
  215. ui.label(r'& <test> ` ${foo}')
  216. screen.open('/')
  217. screen.should_contain(r'& <test> ` ${foo}')