test_query.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from nicegui import ui
  2. from nicegui.testing import Screen
  3. def test_query_body(screen: Screen):
  4. ui.label('Hello')
  5. ui.query('body').classes('bg-orange-100')
  6. ui.button('Red background', on_click=lambda: ui.query('body').classes(replace='bg-red-100'))
  7. ui.button('Blue background', on_click=lambda: ui.query('body').classes(replace='bg-blue-100'))
  8. ui.button('Small padding', on_click=lambda: ui.query('body').style('padding: 1px'))
  9. ui.button('Large padding', on_click=lambda: ui.query('body').style('padding: 10px'))
  10. ui.button('Data X = 1', on_click=lambda: ui.query('body').props('data-x=1'))
  11. ui.button('Data X = 2', on_click=lambda: ui.query('body').props('data-x=2'))
  12. screen.open('/')
  13. screen.should_contain('Hello')
  14. assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light bg-orange-100'
  15. screen.click('Red background')
  16. screen.wait(0.5)
  17. assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light bg-red-100'
  18. screen.click('Blue background')
  19. screen.wait(0.5)
  20. assert screen.find_by_tag('body').get_attribute('class') == 'desktop no-touch body--light bg-blue-100'
  21. screen.click('Small padding')
  22. screen.wait(0.5)
  23. assert screen.find_by_tag('body').value_of_css_property('padding') == '1px'
  24. screen.click('Large padding')
  25. screen.wait(0.5)
  26. assert screen.find_by_tag('body').value_of_css_property('padding') == '10px'
  27. screen.click('Data X = 1')
  28. screen.wait(0.5)
  29. assert screen.find_by_tag('body').get_attribute('data-x') == '1'
  30. screen.click('Data X = 2')
  31. screen.wait(0.5)
  32. assert screen.find_by_tag('body').get_attribute('data-x') == '2'
  33. def test_query_multiple_divs(screen: Screen):
  34. ui.label('A')
  35. ui.label('B')
  36. ui.button('Add border', on_click=lambda: ui.query('div').style('border: 1px solid black'))
  37. screen.open('/')
  38. screen.click('Add border')
  39. screen.wait(0.5)
  40. assert screen.find('A').value_of_css_property('border') == '1px solid rgb(0, 0, 0)'
  41. assert screen.find('B').value_of_css_property('border') == '1px solid rgb(0, 0, 0)'
  42. def test_query_with_css_variables(screen: Screen):
  43. ui.add_body_html('<div id="element">Test</div>')
  44. ui.query('#element').style('--color: red; color: var(--color)')
  45. screen.open('/')
  46. assert screen.find('Test').value_of_css_property('color') == 'rgba(255, 0, 0, 1)'