vitaliy4us
vitaliy4us

Reputation: 605

How to resolve missing characters bug in Cypress type() command?

I have a problem regarding typing into the search field of the select2 element. In some tests random character is missing while executing type() command. I tried to wait() before start typing and type with timeout after each character but nothing helped. So the bypass implemented by me is putting the string into the field as a whole instead of typing it char by char:

this.getSelect2Input().invoke('val', str).trigger('change')

but in this case the test does not understand that the string has been input and continue to wait for input. So the solution was to add a space at the end of the string:

this.getSelect2Input().invoke('val', str).trigger('change').type(' ');

It works but not for the all tests as in some cases the space is treated as additional character and the string can not be found. For this case I can't found nothing else then typing a space and deleting it (I know that this is not good solution, but what can I do?):

this.getSelect2Input().invoke('val', str).trigger('change').type(' {backspace}');

But now this is not working as well as in some cases the space is printed but is not deleted. Can anybody advice how to initiate select2 search after setting the value of the field? type({'enter'}) does not work as well.

Upvotes: 7

Views: 5009

Answers (1)

Alapan Das
Alapan Das

Reputation: 18634

If this.getSelect2Input() translates into something cy.get('locator'), you can add a delay with type() and also add .should to make sure the intended value that you're typing is typed correctly.

// Delay each keypress by 0.1 sec
this.getSelect2Input().type('[email protected]', {
    delay: 100
}).should('have.value', '[email protected]')

Or, You can also add a timeout:

// Add a timeout of 10 seconds
this.getSelect2Input().type('[email protected]', {
    timeout: 10000
}).should('have.value', '[email protected]')

Upvotes: 5

Related Questions