test_interactive_image.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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://nicegui.io/logo.png')
  21. screen.open('/')
  22. screen.wait(0.5)
  23. assert screen.find_by_tag('img').get_attribute('src') == 'https://nicegui.io/logo.png'
  24. screen.click('B')
  25. screen.wait(0.5)
  26. screen.click('A')
  27. assert screen.find_by_tag('img').get_attribute('src') == 'https://nicegui.io/logo.png'
  28. @pytest.mark.parametrize('cross, number_of_lines', [(True, 2), (False, 0)])
  29. def test_with_cross(screen: Screen, cross: bool, number_of_lines: int):
  30. ii = ui.interactive_image('https://nicegui.io/logo.png', cross=cross)
  31. ii.content = '<circle cx="100" cy="100" r="15" fill="none" stroke="red" stroke-width="4" />'
  32. screen.open('/')
  33. screen.wait(0.5)
  34. screen.find_by_tag('svg')
  35. with screen.implicitly_wait(0.5):
  36. assert len(screen.find_all_by_tag('line')) == number_of_lines
  37. assert len(screen.find_all_by_tag('circle')) == 1
  38. def test_replace_interactive_image(screen: Screen):
  39. with ui.row() as container:
  40. ui.interactive_image('https://picsum.photos/id/29/640/360')
  41. def replace():
  42. container.clear()
  43. with container:
  44. ui.interactive_image('https://picsum.photos/id/30/640/360')
  45. ui.button('Replace', on_click=replace)
  46. screen.open('/')
  47. screen.wait(0.5)
  48. assert screen.find_by_tag('img').get_attribute('src').endswith('id/29/640/360')
  49. screen.click('Replace')
  50. screen.wait(0.5)
  51. assert screen.find_by_tag('img').get_attribute('src').endswith('id/30/640/360')
  52. @pytest.mark.parametrize('cross', [True, False])
  53. def test_mousemove_event(screen: Screen, cross: bool):
  54. counter = {'value': 0}
  55. ii = ui.interactive_image('https://picsum.photos/id/29/640/360', cross=cross, events=['mousemove'],
  56. on_mouse=lambda: counter.update(value=counter['value'] + 1))
  57. screen.open('/')
  58. element = screen.find_element(ii)
  59. ActionChains(screen.selenium) \
  60. .move_to_element_with_offset(element, 0, 0) \
  61. .pause(0.5) \
  62. .move_by_offset(10, 10) \
  63. .pause(0.5) \
  64. .perform()
  65. assert counter['value'] > 0
  66. def test_loaded_event(screen: Screen):
  67. sources: List[str] = []
  68. ii = ui.interactive_image('https://picsum.photos/id/29/640/360')
  69. ii.on('loaded', lambda e: sources.append(e.args['source']))
  70. ui.button('Change Source', on_click=lambda: ii.set_source('https://picsum.photos/id/30/640/360'))
  71. screen.open('/')
  72. screen.wait(0.5)
  73. assert len(sources) == 1
  74. screen.click('Change Source')
  75. screen.wait(1.5)
  76. assert len(sources) == 2
  77. assert sources[1].endswith('id/30/640/360')
  78. assert screen.find_by_tag('img').get_attribute('src') == sources[1]