Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 258
Rahul Thawal Avatar asked Mar 26 '19 17:03

Rahul Thawal


1 Answers

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 })
  })
})
like image 104
Zach Bloomquist Avatar answered Nov 28 '22 02:11

Zach Bloomquist