test_interactive_image.py 2.7 KB

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