如何从赛普拉斯清除 Codemirror 编辑器字段

How can I clear a Codemirror editor field from Cypress

我已经从另一个 SO 答案中尝试过类似的东西

如果有帮助,这是我正在与 https://testing-playground.com/

合作开发 Cypress 的网站
  // CodeMirror's editor doesn't let us clear it from the
    // textarea, but we can read the Window object and then
    // invoke `setValue` on the editor global
    cy.window().then(win => {
      win.editor.setValue("")
    })

我 运行 遇到的问题是,当我尝试实现这个时,我得到 undefined 给编辑器。我尝试深入研究 Window 对象,但找不到任何类似于 Codemirror 编辑器的东西。 运行 .clear({force: true}) 在任何其他 Codemirror 元素上都不会产生任何结果,只是在文本字段上 .type({force: true}) 添加新内容和旧内容。

非常不直观,但您可以通过换入新的 CodeMirror 文档实例来更新内容

editor.swapDoc(CodeMirror.Doc([contents],[language]));

这就是我在 wc-codemirror

上处理内容更新的方式

我最终使用的解决方案看起来类似于问题中的代码块,但有一些更改。

  • 我没有从 Cypress 获取 window 对象; CodeMirror 编辑器不存在。

  • 我使用了 Evan Plaice 给我的关于 DOM 上的编辑器实例的想法,然后手动搜索 CodeMirror DOM 层次结构来找到它。

生成的代码将清除特定的编辑器,有两个,因此 [0] 是必需的。

  cy.get('.CodeMirror')
      .first()
      .then((editor) => {
        editor[0].CodeMirror.setValue('');
      });