test_element.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from nicegui import ui
  2. from selenium.webdriver.common.action_chains import ActionChains
  3. from .user import User
  4. def test_keyboard(user: User):
  5. result = ui.label('')
  6. ui.keyboard(on_key=lambda e: result.set_text(f'{e.key, e.action}'))
  7. user.open('/')
  8. assert any(s.endswith('keyboard.js') for s in user.get_attributes('script', 'src'))
  9. assert user.selenium.find_element_by_tag_name('span') # NOTE keyboard dom element is a span
  10. ActionChains(user.selenium).send_keys('t').perform()
  11. user.should_see('t, KeyboardAction(keydown=False, keyup=True, repeat=False)')
  12. def test_classes(user: User):
  13. label = ui.label('Some label')
  14. def assert_classes(classes: str) -> None:
  15. assert user.selenium.find_element_by_xpath(f'//*[normalize-space(@class)="{classes}" and text()="Some label"]')
  16. user.open('/')
  17. assert_classes('')
  18. label.classes('one')
  19. assert_classes('one')
  20. label.classes('one')
  21. assert_classes('one')
  22. label.classes('two three')
  23. assert_classes('one two three')
  24. label.classes(remove='two')
  25. assert_classes('one three')
  26. label.classes(replace='four')
  27. assert_classes('four')
  28. def test_style(user: User):
  29. label = ui.label('Some label')
  30. def assert_style(style: str) -> None:
  31. assert user.selenium.find_element_by_xpath(f'//*[normalize-space(@style)="{style}" and text()="Some label"]')
  32. user.open('/')
  33. assert_style('')
  34. label.style('color: red')
  35. assert_style('color: red;')
  36. label.style('color: red')
  37. assert_style('color: red;')
  38. label.style('color: blue')
  39. assert_style('color: blue;')
  40. label.style('font-weight: bold')
  41. assert_style('color: blue; font-weight: bold;')
  42. label.style(remove='color: blue')
  43. assert_style('font-weight: bold;')
  44. label.style(replace='text-decoration: underline')
  45. assert_style('text-decoration: underline;')
  46. def test_props(user: User):
  47. input = ui.input()
  48. def assert_props(*props: str) -> None:
  49. class_conditions = [f'contains(@class, "q-field--{prop}")' for prop in props]
  50. assert user.selenium.find_element_by_xpath(f'//label[{" and ".join(class_conditions)}]')
  51. user.open('/')
  52. assert_props('standard', 'labeled')
  53. input.props('dark')
  54. assert_props('standard', 'labeled', 'dark')
  55. input.props('dark')
  56. assert_props('standard', 'labeled', 'dark')
  57. input.props(remove='dark')
  58. assert_props('standard', 'labeled')