test_scene.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from selenium.common.exceptions import JavascriptException
  2. from nicegui import ui
  3. from .screen import Screen
  4. def test_moving_sphere_with_timer(screen: Screen):
  5. with ui.scene() as scene:
  6. sphere = scene.sphere().with_name('sphere')
  7. ui.timer(0.1, lambda: sphere.move(0, 0, sphere.z + 0.01))
  8. screen.open('/')
  9. def position() -> None:
  10. for _ in range(3):
  11. try:
  12. pos = screen.selenium.execute_script(f'return scene_{scene.id}.getObjectByName("sphere").position.z')
  13. if pos is not None:
  14. return pos
  15. except JavascriptException as e:
  16. print(e.msg, flush=True)
  17. screen.wait(1.0)
  18. raise Exception('Could not get position')
  19. screen.wait(0.2)
  20. assert position() > 0
  21. def test_no_object_duplication_on_index_client(screen: Screen):
  22. with ui.scene() as scene:
  23. sphere = scene.sphere().move(0, -4, 0)
  24. ui.timer(0.1, lambda: sphere.move(0, sphere.y + 0.5, 0))
  25. screen.open('/')
  26. screen.wait(0.4)
  27. screen.switch_to(1)
  28. screen.open('/')
  29. screen.switch_to(0)
  30. screen.wait(0.2)
  31. assert screen.selenium.execute_script(f'return scene_{scene.id}.children.length') == 5
  32. def test_no_object_duplication_with_page_builder(screen: Screen):
  33. @ui.page('/')
  34. def page():
  35. global scene
  36. with ui.scene() as scene:
  37. sphere = scene.sphere().move(0, -4, 0)
  38. ui.timer(0.1, lambda: sphere.move(0, sphere.y + 0.5, 0))
  39. screen.open('/')
  40. screen.wait(0.4)
  41. screen.switch_to(1)
  42. screen.open('/')
  43. screen.switch_to(0)
  44. screen.wait(0.2)
  45. assert screen.selenium.execute_script(f'return scene_{scene.id}.children.length') == 5
  46. screen.switch_to(1)
  47. assert screen.selenium.execute_script(f'return scene_{scene.id}.children.length') == 5