如何从 Cypress 清除 Codemirror 编辑器字段

Jac*_*cob 2 javascript codemirror e2e-testing cypress

我已经在另一个答案中尝试过类似的方法How to type using cypress .type() inside the codemirror editor?

如果有帮助的话,这是我正在 Cypress 上工作的网站https://testing-playground.com/

  // 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("")
    })
Run Code Online (Sandbox Code Playgroud)

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

Jac*_*cob 6

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

  • 我没有从 Cypress 获取窗口对象;CodeMirror 编辑器不在那里。

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

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

  cy.get('.CodeMirror')
      .first()
      .then((editor) => {
        editor[0].CodeMirror.setValue('');
      });
Run Code Online (Sandbox Code Playgroud)