test_mermaid.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import pytest
  2. from selenium.webdriver.common.by import By
  3. from nicegui import ui
  4. from nicegui.testing import Screen
  5. def test_mermaid(screen: Screen):
  6. m = ui.mermaid('''
  7. graph TD;
  8. Node_A --> Node_B;
  9. ''')
  10. screen.open('/')
  11. node_a = screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_A")]]')
  12. assert node_a.get_attribute('class') == 'nodeLabel'
  13. m.set_content('''
  14. graph TD;
  15. Node_C --> Node_D;
  16. ''')
  17. node_c = screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_C")]]')
  18. assert node_c.get_attribute('class') == 'nodeLabel'
  19. screen.should_not_contain('Node_A')
  20. def test_mermaid_with_line_breaks(screen: Screen):
  21. ui.mermaid('''
  22. requirementDiagram
  23. requirement test_req {
  24. id: 1
  25. text: some test text
  26. risk: high
  27. verifymethod: test
  28. }
  29. ''')
  30. screen.open('/')
  31. screen.should_contain('<<Requirement>>')
  32. screen.should_contain('Id: 1')
  33. screen.should_contain('Text: some test text')
  34. screen.should_contain('Risk: High')
  35. screen.should_contain('Verification: Test')
  36. def test_replace_mermaid(screen: Screen):
  37. with ui.row() as container:
  38. ui.mermaid('graph LR; Node_A')
  39. def replace():
  40. container.clear()
  41. with container:
  42. ui.mermaid('graph LR; Node_B')
  43. ui.button('Replace', on_click=replace)
  44. screen.open('/')
  45. screen.should_contain('Node_A')
  46. screen.click('Replace')
  47. screen.wait(0.5)
  48. screen.should_contain('Node_B')
  49. screen.should_not_contain('Node_A')
  50. def test_create_dynamically(screen: Screen):
  51. ui.button('Create', on_click=lambda: ui.mermaid('graph LR; Node'))
  52. screen.open('/')
  53. screen.click('Create')
  54. screen.should_contain('Node')
  55. def test_error(screen: Screen):
  56. ui.mermaid('''
  57. graph LR;
  58. A --> B;
  59. A -> C;
  60. ''').on('error', lambda e: ui.label(e.args['message']))
  61. screen.open('/')
  62. screen.should_contain('Syntax error in text')
  63. screen.should_contain('Parse error on line 3')
  64. @pytest.mark.parametrize('security_level', ['loose', 'strict'])
  65. def test_click_mermaid_node(security_level: str, screen: Screen):
  66. ui.mermaid('''
  67. flowchart TD;
  68. A;
  69. click A call document.write("Success")
  70. ''', config={'securityLevel': security_level})
  71. screen.open('/')
  72. screen.click('A')
  73. screen.wait(0.5)
  74. if security_level == 'loose':
  75. screen.should_contain('Success')
  76. else:
  77. screen.should_not_contain('Success')