11

I want to test the validation for the character limit of a RichTextEditor, which is 2000 characters, but typing 2000+ characters with .type() takes a lot of time.

Is there a way to speed this up? I tried with Ctrl+c and Ctrl+v modifiers but it is not copy-pasting the text in the input box.

Madhur Bansal
  • 726
  • 2
  • 8
  • 14
  • 3
    [`.type()`](https://docs.cypress.io/api/commands/type) has a configuration argument that takes an object. One of the configurable options is `delay` which controls the interval between key presses. The default is `10` so setting it to `0` or `1` is likely to speed things up. – Ouroborus Jun 30 '21 at 08:01

4 Answers4

13

The problem with reducing the delay { delay: 0 } is that it's there to throttle the char stream in case some event handler or validation can't handle the highest rate.

Also, if I test with the simplest input, no javascript attached

<input maxlength="2000">

the test takes 37 seconds with default delay of 10ms, but is still 24 seconds with delay of 0.

I would recommend setting 2000 chars via the val() method and type the last

cy.get('input')
  .invoke('val', stringGen(2000))  // set 2000 chars
  .type('!')                       // add another
  .invoke('val')                   // read the value
  .should('have.length', 2000)     // confirm the last did not go in

This runs in 0.6 seconds

If you have some javascript event handlers, you would need to trigger them after setting the initial 2000 chars

.trigger('change')

or

.trigger('input')

RichTextEditor

A rich text editor using <div> to hold the text can be pre-loaded with the text() method instead of the val() method.

You will also need to identify the div that receives the text.

For example react-quill uses the class ql-editor on it's primary div.

cy.get('div.ql-editor')
  .invoke('text', stringGen(2000))  // set 2000 chars
  .type('!')                        // add another
  .invoke('text')                   // read the value
  .should('have.length', 2000)      // confirm the last did not go in

Timings for react-quill

{ delay: 10 } (default) 32 seconds
{ delay: 0 } 18 seconds
preload the text 1.5 seconds

9

You can pass options to the type function type(text, options)

For available options, you can take a look at the docs

In your case, you could try to overwrite the delay option to remove the input duration.

inputField.type('a'.repeat(2000), { delay: 0})
sschwei1
  • 362
  • 2
  • 11
2

if You pu this piece of code to cypress/support/index.js file it should remove the delay between the key strokes.

Cypress.Keyboard.defaults({
  keystrokeDelay: 0,
})

update: Here is a documentation of Cypress API Keyboard Cypress API

2

I used the following code (from this post) to quickly copy-paste hundred email adresses in a textarea :

it('pastes text to textarea', () => {
  const textToPaste = 'this is not a valid email'
  cy.visit('index.html')
  cy.get("[type='email']").invoke('val', textToPaste).trigger('blur')
})

Based on the invoke() cypress method

Ousmane
  • 2,673
  • 3
  • 30
  • 37