test_auto_context.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import asyncio
  2. from selenium.webdriver.common.by import By
  3. from nicegui import Client, ui
  4. from nicegui.task_logger import create_task
  5. from .screen import Screen
  6. def test_adding_element_to_shared_index_page(screen: Screen):
  7. ui.button('add label', on_click=lambda: ui.label('added'))
  8. screen.open('/')
  9. screen.click('add label')
  10. screen.should_contain('added')
  11. def test_adding_element_to_private_page(screen: Screen):
  12. @ui.page('/')
  13. def page():
  14. ui.button('add label', on_click=lambda: ui.label('added'))
  15. screen.open('/')
  16. screen.click('add label')
  17. screen.should_contain('added')
  18. def test_adding_elements_with_async_await(screen: Screen):
  19. async def add_a():
  20. await asyncio.sleep(0.1)
  21. ui.label('A')
  22. async def add_b():
  23. await asyncio.sleep(0.1)
  24. ui.label('B')
  25. with ui.card() as cardA:
  26. ui.timer(1.0, add_a, once=True)
  27. with ui.card() as cardB:
  28. ui.timer(1.1, add_b, once=True)
  29. screen.open('/')
  30. screen.wait_for('A')
  31. screen.wait_for('B')
  32. cA = screen.selenium.find_element(By.ID, cardA.id)
  33. cA.find_element(By.XPATH, f'.//*[contains(text(), "A")]')
  34. cB = screen.selenium.find_element(By.ID, cardB.id)
  35. cB.find_element(By.XPATH, f'.//*[contains(text(), "B")]')
  36. def test_autoupdate_after_handshake(screen: Screen):
  37. @ui.page('/')
  38. async def page(client: Client):
  39. ui.label('before handshake')
  40. await client.handshake()
  41. ui.label('after handshake')
  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 handshake')
  50. screen.should_contain('after handshake')
  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():
  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)
  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.wait_for('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.timer(1.0, update, once=True)
  77. screen.open('/')
  78. screen.should_contain('0')
  79. screen.should_not_contain('1')
  80. screen.wait_for('1', timeout=2.0)
  81. screen.should_not_contain('2')
  82. screen.wait_for('2')
  83. def test_adding_elements_from_different_tasks(screen: Screen):
  84. card1 = ui.card()
  85. card2 = ui.card()
  86. async def add_label1() -> None:
  87. with card1:
  88. await asyncio.sleep(1.0)
  89. ui.label('1')
  90. async def add_label2() -> None:
  91. with card2:
  92. ui.label('2')
  93. await asyncio.sleep(1.0)
  94. screen.open('/')
  95. create_task(add_label1())
  96. create_task(add_label2())
  97. screen.wait_for('1')
  98. screen.wait_for('2')
  99. c1 = screen.selenium.find_element(By.ID, card1.id)
  100. c1.find_element(By.XPATH, f'.//*[contains(text(), "1")]')
  101. c2 = screen.selenium.find_element(By.ID, card2.id)
  102. c2.find_element(By.XPATH, f'.//*[contains(text(), "2")]')