test_auto_context.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import asyncio
  2. from selenium.webdriver.common.by import By
  3. from nicegui import Client, background_tasks, ui
  4. from nicegui.testing import Screen
  5. def test_adding_element_to_shared_index_page(screen: Screen):
  6. ui.button('add label', on_click=lambda: ui.label('added'))
  7. screen.open('/')
  8. screen.click('add label')
  9. screen.should_contain('added')
  10. def test_adding_element_to_private_page(screen: Screen):
  11. @ui.page('/')
  12. def page():
  13. ui.button('add label', on_click=lambda: ui.label('added'))
  14. screen.open('/')
  15. screen.click('add label')
  16. screen.should_contain('added')
  17. def test_adding_elements_with_async_await(screen: Screen):
  18. async def add_a():
  19. await asyncio.sleep(1.0)
  20. ui.label('A')
  21. async def add_b():
  22. await asyncio.sleep(1.0)
  23. ui.label('B')
  24. with ui.card() as cardA:
  25. ui.timer(1.0, add_a, once=True)
  26. with ui.card() as cardB:
  27. ui.timer(1.5, add_b, once=True)
  28. screen.open('/')
  29. with screen.implicitly_wait(10.0):
  30. screen.should_contain('A')
  31. screen.should_contain('B')
  32. cA = screen.find_element(cardA)
  33. cA.find_element(By.XPATH, './/*[contains(text(), "A")]')
  34. cB = screen.find_element(cardB)
  35. cB.find_element(By.XPATH, './/*[contains(text(), "B")]')
  36. def test_autoupdate_after_connected(screen: Screen):
  37. @ui.page('/')
  38. async def page(client: Client):
  39. ui.label('before connected')
  40. await client.connected()
  41. ui.label('after connected')
  42. await asyncio.sleep(1)
  43. ui.label('one')
  44. await asyncio.sleep(1)
  45. ui.label('two')
  46. await asyncio.sleep(1)
  47. ui.label('three')
  48. screen.open('/')
  49. screen.should_contain('before connected')
  50. screen.should_contain('after connected')
  51. screen.should_not_contain('one')
  52. screen.wait_for('one')
  53. screen.should_not_contain('two')
  54. screen.wait_for('two')
  55. screen.should_not_contain('three')
  56. screen.wait_for('three')
  57. def test_autoupdate_on_async_event_handler(screen: Screen):
  58. async def open_dialog():
  59. with ui.dialog() as dialog, ui.card():
  60. l = ui.label('This should be visible')
  61. dialog.open()
  62. await asyncio.sleep(1)
  63. l.text = 'New text after 1 second'
  64. ui.button('Dialog', on_click=open_dialog)
  65. screen.open('/')
  66. screen.click('Dialog')
  67. screen.should_contain('This should be visible')
  68. screen.should_not_contain('New text after 1 second')
  69. screen.should_contain('New text after 1 second')
  70. def test_autoupdate_on_async_timer_callback(screen: Screen):
  71. async def update():
  72. ui.label('1')
  73. await asyncio.sleep(1.0)
  74. ui.label('2')
  75. ui.label('0')
  76. ui.button('start', on_click=lambda: ui.timer(2.0, update, once=True))
  77. screen.open('/')
  78. screen.click('start')
  79. screen.should_contain('0')
  80. screen.should_not_contain('1')
  81. screen.wait_for('1')
  82. screen.should_not_contain('2')
  83. screen.wait_for('2')
  84. def test_adding_elements_from_different_tasks(screen: Screen):
  85. card1 = ui.card()
  86. card2 = ui.card()
  87. async def add_label1() -> None:
  88. with card1:
  89. await asyncio.sleep(0.5)
  90. ui.label('1')
  91. async def add_label2() -> None:
  92. with card2:
  93. ui.label('2')
  94. await asyncio.sleep(0.5)
  95. ui.timer(0, lambda: ui.label('connection established'), once=True) # HACK: allow waiting for client connection
  96. screen.open('/')
  97. with screen.implicitly_wait(10.0):
  98. screen.wait_for('connection established')
  99. background_tasks.create(add_label1())
  100. background_tasks.create(add_label2())
  101. screen.should_contain('1')
  102. screen.should_contain('2')
  103. c1 = screen.find_element(card1)
  104. c1.find_element(By.XPATH, './/*[contains(text(), "1")]')
  105. c2 = screen.find_element(card2)
  106. c2.find_element(By.XPATH, './/*[contains(text(), "2")]')