1
0

test_element.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. from typing import Dict, Optional
  2. import pytest
  3. from selenium.webdriver.common.by import By
  4. from nicegui import background_tasks, ui
  5. from nicegui.props import Props
  6. from nicegui.style import Style
  7. from nicegui.testing import Screen
  8. def test_classes(screen: Screen):
  9. label = ui.label('Some label')
  10. def assert_classes(classes: str) -> None:
  11. assert screen.selenium.find_element(By.XPATH,
  12. f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  13. screen.open('/')
  14. screen.wait(0.5)
  15. assert_classes('')
  16. label.classes('one')
  17. assert_classes('one')
  18. label.classes('one')
  19. assert_classes('one')
  20. label.classes('two three')
  21. assert_classes('one two three')
  22. label.classes(remove='two')
  23. assert_classes('one three')
  24. label.classes(replace='four')
  25. assert_classes('four')
  26. @pytest.mark.parametrize('value,expected', [
  27. (None, {}),
  28. ('color: red; background-color: blue', {'color': 'red', 'background-color': 'blue'}),
  29. ('width:12em;height:34.5em', {'width': '12em', 'height': '34.5em'}),
  30. ('transform: translate(120.0px, 50%)', {'transform': 'translate(120.0px, 50%)'}),
  31. ('box-shadow: 0 0 0.5em #1976d2', {'box-shadow': '0 0 0.5em #1976d2'}),
  32. ])
  33. def test_style_parsing(value: Optional[str], expected: Dict[str, str]):
  34. assert Style.parse(value) == expected
  35. @pytest.mark.parametrize('value,expected', [
  36. (None, {}),
  37. ('one two=1 three="abc def"', {'one': True, 'two': '1', 'three': 'abc def'}),
  38. ('loading percentage=12.5', {'loading': True, 'percentage': '12.5'}),
  39. ('size=50%', {'size': '50%'}),
  40. ('href=http://192.168.42.100/', {'href': 'http://192.168.42.100/'}),
  41. ('hint="Your \\"given\\" name"', {'hint': 'Your "given" name'}),
  42. ('input-style="{ color: #ff0000 }"', {'input-style': '{ color: #ff0000 }'}),
  43. ('accept=.jpeg,.jpg,.png', {'accept': '.jpeg,.jpg,.png'}),
  44. ('empty=""', {'empty': ''}),
  45. ("empty=''", {'empty': ''}),
  46. ("""hint='Your \\"given\\" name'""", {'hint': 'Your "given" name'}),
  47. ("one two=1 three='abc def'", {'one': True, 'two': '1', 'three': 'abc def'}),
  48. ('''three='abc def' four="hhh jjj"''', {'three': 'abc def', 'four': 'hhh jjj', }),
  49. ('''foo="quote'quote"''', {'foo': "quote'quote"}),
  50. ("""foo='quote"quote'""", {'foo': 'quote"quote'}),
  51. ("""foo="single '" bar='double "'""", {'foo': "single '", 'bar': 'double "'}),
  52. ("""foo="single '" bar='double \\"'""", {'foo': "single '", 'bar': 'double "'}),
  53. ("input-style='{ color: #ff0000 }'", {'input-style': '{ color: #ff0000 }'}),
  54. ("""input-style='{ myquote: "quote" }'""", {'input-style': '{ myquote: "quote" }'}),
  55. ('filename=foo=bar.txt', {'filename': 'foo=bar.txt'}),
  56. ])
  57. def test_props_parsing(value: Optional[str], expected: Dict[str, str]):
  58. assert Props.parse(value) == expected
  59. def test_style(screen: Screen):
  60. label = ui.label('Some label')
  61. def assert_style(style: str) -> None:
  62. assert screen.selenium.find_element(By.XPATH, f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  63. screen.open('/')
  64. screen.wait(0.5)
  65. assert_style('')
  66. label.style('color: red')
  67. assert_style('color: red;')
  68. label.style('color: red')
  69. assert_style('color: red;')
  70. label.style('color: blue')
  71. assert_style('color: blue;')
  72. label.style('font-weight: bold')
  73. assert_style('color: blue; font-weight: bold;')
  74. label.style(remove='color: blue')
  75. assert_style('font-weight: bold;')
  76. label.style(replace='text-decoration: underline')
  77. assert_style('text-decoration: underline;')
  78. label.style('color: blue;')
  79. assert_style('text-decoration: underline; color: blue;')
  80. def test_props(screen: Screen):
  81. input_ = ui.input()
  82. def assert_props(*props: str) -> None:
  83. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  84. assert screen.selenium.find_element(By.XPATH, f'//label[{" and ".join(class_conditions)}]')
  85. screen.open('/')
  86. screen.wait(0.5)
  87. assert_props('standard')
  88. input_.props('dark')
  89. assert_props('standard', 'dark')
  90. input_.props('dark')
  91. assert_props('standard', 'dark')
  92. input_.props(remove='dark')
  93. assert_props('standard')
  94. def test_move(screen: Screen):
  95. with ui.card() as a:
  96. ui.label('A')
  97. x = ui.label('X')
  98. with ui.card() as b:
  99. ui.label('B')
  100. ui.button('Move X to A', on_click=lambda: x.move(a))
  101. ui.button('Move X to B', on_click=lambda: x.move(b))
  102. ui.button('Move X to top', on_click=lambda: x.move(target_index=0))
  103. screen.open('/')
  104. assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y']
  105. screen.click('Move X to B')
  106. screen.wait(0.5)
  107. assert screen.find('A').location['y'] < screen.find('B').location['y'] < screen.find('X').location['y']
  108. screen.click('Move X to A')
  109. screen.wait(0.5)
  110. assert screen.find('A').location['y'] < screen.find('X').location['y'] < screen.find('B').location['y']
  111. screen.click('Move X to top')
  112. screen.wait(0.5)
  113. assert screen.find('X').location['y'] < screen.find('A').location['y'] < screen.find('B').location['y']
  114. def test_move_slots(screen: Screen):
  115. with ui.expansion(value=True) as a:
  116. with a.add_slot('header'):
  117. ui.label('A')
  118. x = ui.label('X')
  119. with ui.expansion(value=True) as b:
  120. with b.add_slot('header'):
  121. ui.label('B')
  122. ui.button('Move X to header', on_click=lambda: x.move(target_slot='header'))
  123. ui.button('Move X to B', on_click=lambda: x.move(b))
  124. ui.button('Move X to top', on_click=lambda: x.move(target_index=0))
  125. screen.open('/')
  126. assert screen.find('A').location['y'] < screen.find('X').location['y'], 'X is in A.default'
  127. screen.click('Move X to header')
  128. screen.wait(0.5)
  129. assert screen.find('A').location['y'] == screen.find('X').location['y'], 'X is in A.header'
  130. screen.click('Move X to top')
  131. screen.wait(0.5)
  132. assert screen.find('A').location['y'] < screen.find('X').location['y'], 'X is in A.default'
  133. screen.click('Move X to B')
  134. screen.wait(0.5)
  135. assert screen.find('B').location['y'] < screen.find('X').location['y'], 'X is in B.default'
  136. def test_xss(screen: Screen):
  137. ui.label('</script><script>alert(1)</script>')
  138. ui.label('<b>Bold 1</b>, `code`, copy&paste, multi\nline')
  139. ui.button('Button', on_click=lambda: (
  140. ui.label('</script><script>alert(2)</script>'),
  141. ui.label('<b>Bold 2</b>, `code`, copy&paste, multi\nline'),
  142. ))
  143. screen.open('/')
  144. screen.click('Button')
  145. screen.should_contain('</script><script>alert(1)</script>')
  146. screen.should_contain('</script><script>alert(2)</script>')
  147. screen.should_contain('<b>Bold 1</b>, `code`, copy&paste, multi\nline')
  148. screen.should_contain('<b>Bold 2</b>, `code`, copy&paste, multi\nline')
  149. def test_default_props(nicegui_reset_globals):
  150. ui.button.default_props('rounded outline')
  151. button_a = ui.button('Button A')
  152. button_b = ui.button('Button B')
  153. assert button_a.props.get('rounded') is True, 'default props are set'
  154. assert button_a.props.get('outline') is True
  155. assert button_b.props.get('rounded') is True
  156. assert button_b.props.get('outline') is True
  157. ui.button.default_props(remove='outline')
  158. button_c = ui.button('Button C')
  159. assert button_c.props.get('outline') is None, '"outline" prop was removed'
  160. assert button_c.props.get('rounded') is True, 'other props are still there'
  161. ui.input.default_props('filled')
  162. input_a = ui.input()
  163. assert input_a.props.get('filled') is True
  164. assert input_a.props.get('rounded') is None, 'default props of ui.button do not affect ui.input'
  165. class MyButton(ui.button):
  166. pass
  167. MyButton.default_props('flat')
  168. button_d = MyButton()
  169. button_e = ui.button()
  170. assert button_d.props.get('flat') is True
  171. assert button_d.props.get('rounded') is True, 'default props are inherited'
  172. assert button_e.props.get('flat') is None, 'default props of MyButton do not affect ui.button'
  173. assert button_e.props.get('rounded') is True
  174. ui.button.default_props('no-caps').default_props('no-wrap')
  175. button_f = ui.button()
  176. assert button_f.props.get('no-caps') is True
  177. assert button_f.props.get('no-wrap') is True
  178. def test_default_classes(nicegui_reset_globals):
  179. ui.button.default_classes('bg-white text-green')
  180. button_a = ui.button('Button A')
  181. button_b = ui.button('Button B')
  182. assert 'bg-white' in button_a.classes, 'default classes are set'
  183. assert 'text-green' in button_a.classes
  184. assert 'bg-white' in button_b.classes
  185. assert 'text-green' in button_b.classes
  186. ui.button.default_classes(remove='text-green')
  187. button_c = ui.button('Button C')
  188. assert 'text-green' not in button_c.classes, '"text-green" class was removed'
  189. assert 'bg-white' in button_c.classes, 'other classes are still there'
  190. ui.input.default_classes('text-black')
  191. input_a = ui.input()
  192. assert 'text-black' in input_a.classes
  193. assert 'bg-white' not in input_a.classes, 'default classes of ui.button do not affect ui.input'
  194. class MyButton(ui.button):
  195. pass
  196. MyButton.default_classes('w-full')
  197. button_d = MyButton()
  198. button_e = ui.button()
  199. assert 'w-full' in button_d.classes
  200. assert 'bg-white' in button_d.classes, 'default classes are inherited'
  201. assert 'w-full' not in button_e.classes, 'default classes of MyButton do not affect ui.button'
  202. assert 'bg-white' in button_e.classes
  203. ui.button.default_classes('h-40').default_classes('max-h-80')
  204. button_f = ui.button()
  205. assert 'h-40' in button_f.classes
  206. assert 'max-h-80' in button_f.classes
  207. def test_default_style(nicegui_reset_globals):
  208. ui.button.default_style('color: green; font-size: 200%')
  209. button_a = ui.button('Button A')
  210. button_b = ui.button('Button B')
  211. assert button_a.style.get('color') == 'green', 'default style is set'
  212. assert button_a.style.get('font-size') == '200%'
  213. assert button_b.style.get('color') == 'green'
  214. assert button_b.style.get('font-size') == '200%'
  215. ui.button.default_style(remove='color: green')
  216. button_c = ui.button('Button C')
  217. assert button_c.style.get('color') is None, '"color" style was removed'
  218. assert button_c.style.get('font-size') == '200%', 'other style are still there'
  219. ui.input.default_style('font-weight: 300')
  220. input_a = ui.input()
  221. assert input_a.style.get('font-weight') == '300'
  222. assert input_a.style.get('font-size') is None, 'default style of ui.button does not affect ui.input'
  223. class MyButton(ui.button):
  224. pass
  225. MyButton.default_style('font-family: courier')
  226. button_d = MyButton()
  227. button_e = ui.button()
  228. assert button_d.style.get('font-family') == 'courier'
  229. assert button_d.style.get('font-size') == '200%', 'default style is inherited'
  230. assert button_e.style.get('font-family') is None, 'default style of MyButton does not affect ui.button'
  231. assert button_e.style.get('font-size') == '200%'
  232. ui.button.default_style('border: 2px').default_style('padding: 30px')
  233. button_f = ui.button()
  234. assert button_f.style.get('border') == '2px'
  235. assert button_f.style.get('padding') == '30px'
  236. def test_invalid_tags(screen: Screen):
  237. good_tags = ['div', 'div-1', 'DIV', 'däv', 'div_x', '🙂']
  238. bad_tags = ['<div>', 'hi hi', 'hi/ho', 'foo$bar']
  239. for tag in good_tags:
  240. ui.element(tag)
  241. for tag in bad_tags:
  242. with pytest.raises(ValueError):
  243. ui.element(tag)
  244. screen.open('/')
  245. def test_bad_characters(screen: Screen):
  246. ui.label(r'& <test> ` ${foo}')
  247. screen.open('/')
  248. screen.should_contain(r'& <test> ` ${foo}')
  249. def test_update_before_client_connection(screen: Screen):
  250. @ui.page('/')
  251. def page():
  252. label = ui.label('Hello world!')
  253. async def update():
  254. label.text = 'Hello again!'
  255. background_tasks.create(update())
  256. screen.open('/')
  257. screen.should_contain('Hello again!')