如何从赛普拉斯清除Codemirror编辑器字段 [英] How can I clear a Codemirror editor field from Cypress
问题描述
我已经从另一个SO答案如何在Codemirror编辑器中使用cypress .type()进行键入?
I already tried something like this from another SO answer How to type using cypress .type() inside the codemirror editor?
如果有帮助,这是我正在与 https://testing-playground.com/
If it helps this is the site I am working on Cypress with 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("")
})
我遇到的问题是,当我尝试实现此功能时,编辑器得到 undefined
。我尝试深入研究Window对象,却找不到类似于Codemirror编辑器的任何东西。在任何其他Codemirror元素上运行 .clear({force:true})
不会产生任何结果,只是 .type({force:true})
在文本字段上添加新内容和旧内容。
The issue I am running into is when I try to implement this I am getting undefined
for the editor. I tried digging into the Window object more and couldn't find anything resembling the Codemirror editor. Running .clear({force: true})
on any of the other Codemirror elements yields no results, just .type({force: true})
on the textfield adding the new content with the old.
推荐答案
我最终使用的解决方案看起来与问题中的代码块类似,但有一些更改。
The solution I ended up using looked something similar to code block in the question with a few changes.
-
我没有从Cypress获得window对象;
I am not getting the window object from Cypress; CodeMirror editor wasn't there.
我使用了Evan Plaice给我的想法,即与DOM上的Editor实例相关,然后手动搜索了CodeMirror DOM层次结构
I used the idea Evan Plaice gave me related to the Editor instance being on the DOM and then searched the CodeMirror DOM hierarchy manually to find it.
生成的代码将清除特定的编辑器,因此有两个,所以 [0]
是必需的。
The resulting code will clear a spefic editor, there are two so the [0]
is necessary for that.
cy.get('.CodeMirror')
.first()
.then((editor) => {
editor[0].CodeMirror.setValue('');
});
这篇关于如何从赛普拉斯清除Codemirror编辑器字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!