1
0

test_element.py 12 KB

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