test_element.py 2.1 KB

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