1
0

test_interactive_image.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from pathlib import Path
  2. import pytest
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from nicegui import app, ui
  5. from nicegui.testing import Screen
  6. URL_PATH1 = '/test1.jpg'
  7. URL_PATH2 = '/test2.jpg'
  8. @pytest.fixture(autouse=True)
  9. def provide_image_files():
  10. app.add_static_file(local_file=Path(__file__).parent / 'media' / 'test1.jpg', url_path=URL_PATH1)
  11. app.add_static_file(local_file=Path(__file__).parent / 'media' / 'test2.jpg', url_path=URL_PATH2)
  12. def test_set_source_in_tab(screen: Screen):
  13. """https://github.com/zauberzeug/nicegui/issues/488"""
  14. @ui.page('/')
  15. async def page():
  16. with ui.tabs() as tabs:
  17. ui.tab('A')
  18. ui.tab('B')
  19. with ui.tab_panels(tabs, value='A'):
  20. with ui.tab_panel('A'):
  21. ui.label('Tab A')
  22. img = ui.interactive_image()
  23. with ui.tab_panel('B'):
  24. ui.label('Tab B')
  25. await ui.context.client.connected()
  26. img.set_source(URL_PATH1)
  27. screen.open('/')
  28. screen.wait(0.5)
  29. assert screen.find_by_tag('img').get_attribute('src').endswith(URL_PATH1)
  30. screen.click('B')
  31. screen.wait(0.5)
  32. screen.click('A')
  33. assert screen.find_by_tag('img').get_attribute('src').endswith(URL_PATH1)
  34. @pytest.mark.parametrize('cross', [True, False])
  35. def test_with_cross(screen: Screen, cross: bool):
  36. ui.interactive_image(URL_PATH1, content='<circle cx="100" cy="100" r="15" />', cross=cross)
  37. screen.open('/')
  38. screen.find_by_tag('svg')
  39. with screen.implicitly_wait(0.5):
  40. assert len(screen.find_all_by_tag('line')) == (2 if cross else 0)
  41. assert len(screen.find_all_by_tag('circle')) == 1
  42. def test_replace_interactive_image(screen: Screen):
  43. with ui.row() as container:
  44. ui.interactive_image(URL_PATH1)
  45. def replace():
  46. container.clear()
  47. with container:
  48. ui.interactive_image(URL_PATH2)
  49. ui.button('Replace', on_click=replace)
  50. screen.open('/')
  51. assert (screen.find_by_tag('img').get_attribute('src') or '').endswith(URL_PATH1)
  52. screen.click('Replace')
  53. screen.wait(0.5)
  54. assert (screen.find_by_tag('img').get_attribute('src') or '').endswith(URL_PATH2)
  55. @pytest.mark.parametrize('cross', [True, False])
  56. def test_mousemove_event(screen: Screen, cross: bool):
  57. counter = {'value': 0}
  58. ii = ui.interactive_image(URL_PATH1, cross=cross, events=['mousemove'],
  59. on_mouse=lambda: counter.update(value=counter['value'] + 1))
  60. screen.open('/')
  61. element = screen.find_element(ii)
  62. ActionChains(screen.selenium) \
  63. .move_to_element_with_offset(element, 0, 0) \
  64. .pause(0.5) \
  65. .move_by_offset(10, 10) \
  66. .pause(0.5) \
  67. .perform()
  68. assert counter['value'] > 0
  69. def test_loaded_event(screen: Screen):
  70. ii = ui.interactive_image(URL_PATH1)
  71. ii.on('loaded', lambda: ui.label('loaded'))
  72. ui.button('Change Source', on_click=lambda: ii.set_source(URL_PATH2))
  73. screen.open('/')
  74. screen.click('Change Source')
  75. screen.should_contain('loaded')
  76. assert (screen.find_by_tag('img').get_attribute('src') or '').endswith(URL_PATH2)
  77. def test_add_layer(screen: Screen):
  78. ii = ui.interactive_image(URL_PATH1, content='<rect x="0" y="0" width="100" height="100" fill="red" />')
  79. ii.add_layer(content='<circle cx="100" cy="100" r="15" />')
  80. screen.open('/')
  81. screen.find_by_tag('svg')
  82. with screen.implicitly_wait(0.5):
  83. assert len(screen.find_all_by_tag('rect')) == 1
  84. assert len(screen.find_all_by_tag('circle')) == 1