How to get editor content from the browser

Hi. I’m writing some end-to-end tests for my CodeMirror v6 project. I’d like to get the text content inside my CodeMirror v6 editor from the browser, but I’m not sure how to do it.


I’ve tried document.querySelector('.cm-content').innerText, but the result contains some extra newlines.

What I want here is h1\nh2\n\nh3\n\n\nh4. Could I just call text = text.replaceAll('\n\n', '\n') to remove these extra newlines?


I’ve also tried to get a CodeMirror instance from its wrapper element, but .CodeMirror doesn’t seem to work anymore in CodeMirror v6.

This isn’t really something that the editor provides anymore. But you could do something icky like Array.from(document.querySelectorAll(".cm-line")).map(e => e.textContent).join("\n") arranging some reasonable entry point for your tests to connect to isn’t viable.

This works well. Thanks for your help.