1
0

test_scene.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  48. def test_deleting_group(screen: Screen):
  49. with ui.scene() as scene:
  50. with scene.group() as group:
  51. scene.sphere()
  52. ui.button('Delete group', on_click=group.delete)
  53. screen.open('/')
  54. screen.wait(0.5)
  55. assert len(scene.objects) == 2
  56. screen.click('Delete group')
  57. screen.wait(0.5)
  58. assert len(scene.objects) == 0
  59. def test_replace_scene(screen: Screen):
  60. with ui.row() as container:
  61. with ui.scene() as scene:
  62. scene.sphere().with_name('sphere')
  63. def replace():
  64. container.clear()
  65. with container:
  66. nonlocal scene
  67. with ui.scene() as scene:
  68. scene.box().with_name('box')
  69. ui.button('Replace scene', on_click=replace)
  70. screen.open('/')
  71. screen.wait(0.5)
  72. assert screen.selenium.execute_script(f'return scene_{scene.id}.children[4].name') == 'sphere'
  73. screen.click('Replace scene')
  74. screen.wait(0.5)
  75. assert screen.selenium.execute_script(f'return scene_{scene.id}.children[4].name') == 'box'