How to type using cypress .type() inside the codemirror editor?

邮差的信 提交于 2020-06-14 06:20:08

问题


I am writing some cypress test for the Codemirror Editor. I have use cypress to type in the input field.

I am trying to achieve the cy.type() in the CodeMirror Editor. The Data I have in codemirror is inside the span.

<pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"> &lt; h1 &gt; Welcome to your web project canvas! &lt; /h1&gt;</span></pre> 

The cypress spec code cy.get('pre.CodeMirror-line') .type('Cypress HTML Data')

I am not able to type some data using cypress.

I would appreciate if someone can help?


回答1:


You're not targeting the correct element in your spec code. You're doing cy.get('pre.CodeMirror-line'), but a <pre> tag is not a cy.type()-able element.

You need to get the hidden CodeMirror <textarea> instead. This can be selected using .CodeMirror textarea. The following JS is a demo spec that works on codemirror.net:

describe('Codemirror', () => {
  it('can be tested using textarea', () => {
    cy.visit('https://codemirror.net/')
    // 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("")
    })
    cy.get('.CodeMirror textarea')
    // we use `force: true` below because the textarea is hidden
    // and by default Cypress won't interact with hidden elements
      .type('test test test test', { force: true })
  })
})


来源:https://stackoverflow.com/questions/55362875/how-to-type-using-cypress-type-inside-the-codemirror-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!