test_link.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from nicegui import ui
  2. from nicegui.testing import Screen
  3. def test_local_target_linking_on_sub_pages(screen: Screen):
  4. '''The issue arose when using <base> tag for reverse-proxy path handling. See https://github.com/zauberzeug/nicegui/pull/188#issuecomment-1336313925'''
  5. @ui.page('/sub')
  6. def main():
  7. ui.link('goto target', '#target').style('margin-bottom: 600px')
  8. ui.link_target('target')
  9. ui.label('the target')
  10. ui.label('main page')
  11. screen.open('/sub')
  12. screen.click('goto target')
  13. screen.should_contain('the target')
  14. screen.should_not_contain('main page')
  15. def test_opening_link_in_new_tab(screen: Screen):
  16. @ui.page('/sub')
  17. def subpage():
  18. ui.label('the sub-page')
  19. ui.link('open sub-page in new tab', '/sub', new_tab=True)
  20. screen.open('/')
  21. screen.click('open sub-page')
  22. screen.switch_to(1)
  23. screen.should_contain('the sub-page')
  24. screen.should_not_contain('open sub-page')
  25. screen.switch_to(0)
  26. screen.should_not_contain('the sub-page')
  27. screen.should_contain('open sub-page')
  28. def test_replace_link(screen: Screen):
  29. with ui.row() as container:
  30. ui.link('nicegui.io', 'https://nicegui.io/')
  31. def replace():
  32. container.clear()
  33. with container:
  34. ui.link('zauberzeug', 'https://zauberzeug.com/')
  35. ui.button('Replace', on_click=replace)
  36. screen.open('/')
  37. assert screen.find('nicegui.io').get_attribute('href') == 'https://nicegui.io/'
  38. screen.click('Replace')
  39. assert screen.find('zauberzeug').get_attribute('href') == 'https://zauberzeug.com/'
  40. def test_updating_href_prop(screen: Screen):
  41. label = ui.link('nicegui.io', 'https://nicegui.io')
  42. ui.button('change href', on_click=lambda: label.props('href="https://github.com/zauberzeug/nicegui"'))
  43. screen.open('/')
  44. assert screen.find('nicegui.io').get_attribute('href') == 'https://nicegui.io/'
  45. screen.click('change href')
  46. assert screen.find('nicegui.io').get_attribute('href') == 'https://github.com/zauberzeug/nicegui'
  47. def test_link_to_elements(screen: Screen):
  48. navigation = ui.row()
  49. for i in range(100):
  50. ui.label(f'label {i}')
  51. link = ui.link('goto top', navigation)
  52. with navigation:
  53. ui.link('goto bottom', link)
  54. screen.open('/')
  55. assert screen.selenium.execute_script('return window.scrollY') == 0
  56. screen.click('goto bottom')
  57. screen.wait(0.5)
  58. assert screen.selenium.execute_script('return window.scrollY') > 100
  59. screen.click('goto top')
  60. screen.wait(0.5)
  61. assert screen.selenium.execute_script('return window.scrollY') < 100