test_interactive_image.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from typing import List
  2. import pytest
  3. from selenium.webdriver.common.action_chains import ActionChains
  4. from nicegui import Client, ui
  5. from nicegui.testing import Screen
  6. def test_set_source_in_tab(screen: Screen):
  7. """https://github.com/zauberzeug/nicegui/issues/488"""
  8. @ui.page('/')
  9. async def page(client: Client):
  10. with ui.tabs() as tabs:
  11. ui.tab('A')
  12. ui.tab('B')
  13. with ui.tab_panels(tabs, value='A'):
  14. with ui.tab_panel('A'):
  15. ui.label('Tab A')
  16. img = ui.interactive_image()
  17. with ui.tab_panel('B'):
  18. ui.label('Tab B')
  19. await client.connected()
  20. img.set_source('https://picsum.photos/id/29/640/360')
  21. screen.open('/')
  22. screen.wait(0.5)
  23. assert screen.find_by_tag('img').get_attribute('src') == 'https://picsum.photos/id/29/640/360'
  24. screen.click('B')
  25. screen.wait(0.5)
  26. screen.click('A')
  27. assert screen.find_by_tag('img').get_attribute('src') == 'https://picsum.photos/id/29/640/360'
  28. @pytest.mark.parametrize('cross', [True, False])
  29. def test_with_cross(screen: Screen, cross: bool):
  30. ui.interactive_image('https://picsum.photos/id/29/640/360',
  31. content='<circle cx="100" cy="100" r="15" />', cross=cross)
  32. screen.open('/')
  33. screen.find_by_tag('svg')
  34. with screen.implicitly_wait(0.5):
  35. assert len(screen.find_all_by_tag('line')) == (2 if cross else 0)
  36. assert len(screen.find_all_by_tag('circle')) == 1
  37. def test_replace_interactive_image(screen: Screen):
  38. with ui.row() as container:
  39. ui.interactive_image('https://picsum.photos/id/29/640/360')
  40. def replace():
  41. container.clear()
  42. with container:
  43. ui.interactive_image('https://picsum.photos/id/30/640/360')
  44. ui.button('Replace', on_click=replace)
  45. screen.open('/')
  46. assert (screen.find_by_tag('img').get_attribute('src') or '').endswith('id/29/640/360')
  47. screen.click('Replace')
  48. screen.wait(0.5)
  49. assert (screen.find_by_tag('img').get_attribute('src') or '').endswith('id/30/640/360')
  50. @pytest.mark.parametrize('cross', [True, False])
  51. def test_mousemove_event(screen: Screen, cross: bool):
  52. counter = {'value': 0}
  53. ii = ui.interactive_image('https://picsum.photos/id/29/640/360', cross=cross, events=['mousemove'],
  54. on_mouse=lambda: counter.update(value=counter['value'] + 1))
  55. screen.open('/')
  56. element = screen.find_element(ii)
  57. ActionChains(screen.selenium) \
  58. .move_to_element_with_offset(element, 0, 0) \
  59. .pause(0.5) \
  60. .move_by_offset(10, 10) \
  61. .pause(0.5) \
  62. .perform()
  63. assert counter['value'] > 0
  64. def test_loaded_event(screen: Screen):
  65. sources: List[str] = []
  66. ii = ui.interactive_image('https://picsum.photos/id/29/640/360')
  67. ii.on('loaded', lambda e: sources.append(e.args['source']))
  68. ui.button('Change Source', on_click=lambda: ii.set_source('https://picsum.photos/id/30/640/360'))
  69. screen.open('/')
  70. screen.wait(0.5)
  71. assert len(sources) == 1
  72. screen.click('Change Source')
  73. screen.wait(1.5)
  74. assert len(sources) == 2
  75. assert sources[1].endswith('id/30/640/360')
  76. assert screen.find_by_tag('img').get_attribute('src') == sources[1]