question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress mixes charCodes and keyCodes in `type()` function

See original GitHub issue

Current behavior:

Reference to ASCII table: https://www.asciitable.com

Normall ASCII charcode of dot character . is 46: image

But cypress with type() function uses charcode 190: image

Using code bellow it doesn’t work too. Cypress just converts String.fromCharCode(46) to 190 and normal . is also 190.

Code:

cy.get('#dateField').type('3' + String.fromCharCode(48) + String.fromCharCode(46) + '12.2020', {delay: 100});

What it does: image

And here is our problem: preventedDefault Our JS verifies input and it preventDefault by this RegExp: var regex = new RegExp("^[0-9.]+$");.

(snippet of) JS:

date: function (event) {
	if (event.charCode != 0) {
		var regex = new RegExp("^[0-9.]+$");
		var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
			if (!regex.test(key)) {
				event.preventDefault();
				return false;
			}
	}
}

Desired behavior:

Well… Cypress’ type() function should use right charcodes.

Steps to reproduce:

I already posted these steps above.

Versions

Cypress ['3.0.1', '3.0.2'] on ['macOS 10.13', 'macOS 10.14', 'Windows 10 1803', 'Ubuntu 18.04']

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pjcommented, Nov 9, 2018

A workaround that fixed it for me was just to delete the key mapping from the cypress internals in the beforeEach method:

    beforeEach(() => {
        delete Cypress.Keyboard.charCodeMap[47]; // Delete the '/' key from the mapping so it works as expected
    });

This might break other stuff though…

0reactions
pdougall1commented, Nov 19, 2018

Wow, well done cypress team! I literally just hit this issue and apparently it’s just been fixed! 👏

Read more comments on GitHub >

github_iconTop Results From Across the Web

type - Cypress Documentation
type() may include any of the special character sequences below. These characters will pass along the correct keyCode , key , and which...
Read more >
Cypress: type combination of keys and keyCodes
I need to navigate through the keyboard pressing ALT ...
Read more >
Use createSyntheticEvent in Playwright Internal With Examples ...
Learn how to use createSyntheticEvent function in Playwright Internal framework for your next JavaScript automation project with LambdaTest Automation ...
Read more >
Document Object Model (DOM) Level 3 Events Specification
This specification defines the Document Object Model Events Level 3, a generic platform- and language-neutral event system which allows ...
Read more >
KeyboardEvent.charCode - Web APIs | MDN
A number that represents the Unicode value of the character key that was pressed. Examples. HTML. <p>Type anything ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found