test_element.py 10 KB

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