test_markdown.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from selenium.webdriver.common.by import By
  2. from nicegui import ui
  3. from nicegui.testing import Screen
  4. def test_markdown(screen: Screen):
  5. m = ui.markdown('This is **Markdown**')
  6. screen.open('/')
  7. element = screen.find('This is')
  8. assert element.text == 'This is Markdown'
  9. assert element.get_attribute('innerHTML') == 'This is <strong>Markdown</strong>'
  10. m.set_content('New **content**')
  11. element = screen.find('New')
  12. assert element.text == 'New content'
  13. assert element.get_attribute('innerHTML') == 'New <strong>content</strong>'
  14. def test_markdown_with_mermaid(screen: Screen):
  15. m = ui.markdown('''
  16. Mermaid:
  17. ```mermaid
  18. graph TD;
  19. Node_A --> Node_B;
  20. ```
  21. ''', extras=['mermaid', 'fenced-code-blocks'])
  22. screen.open('/')
  23. screen.wait(0.5) # wait for Mermaid to render
  24. screen.should_contain('Mermaid')
  25. assert screen.find_by_tag('svg').get_attribute('id').startswith('mermaid-')
  26. node_a = screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_A")]]')
  27. assert node_a.get_attribute('class') == 'nodeLabel'
  28. m.set_content('''
  29. New:
  30. ```mermaid
  31. graph TD;
  32. Node_C --> Node_D;
  33. ```
  34. ''')
  35. screen.should_contain('New')
  36. node_c = screen.selenium.find_element(By.XPATH, '//span[p[contains(text(), "Node_C")]]')
  37. assert node_c.get_attribute('class') == 'nodeLabel'
  38. screen.should_not_contain('Node_A')
  39. def test_strip_indentation(screen: Screen):
  40. ui.markdown('''
  41. **This is Markdown.**
  42. ''')
  43. screen.open('/')
  44. screen.should_contain('This is Markdown.')
  45. screen.should_not_contain('**This is Markdown.**') # NOTE: '**' are translated to formatting and not visible
  46. def test_replace_markdown(screen: Screen):
  47. with ui.row() as container:
  48. ui.markdown('A')
  49. def replace():
  50. container.clear()
  51. with container:
  52. ui.markdown('B')
  53. ui.button('Replace', on_click=replace)
  54. screen.open('/')
  55. screen.should_contain('A')
  56. screen.click('Replace')
  57. screen.should_contain('B')
  58. screen.should_not_contain('A')