New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cypress mixes charCodes and keyCodes in type()
function
#2105
Comments
Here is example how to reproduce:Cypress' test src file: describe('Cypress\' issue #2105', function() {
it('Start', function() {
cy.visit('http://localhost/js.html');
cy.get('input').type('abc123./', { delay: 1000});
})
})
<!DOCTYPE html>
<html>
<body>
<p id="demo">test</p>
<input type="text" onkeypress="myFunction(event)">
<script>
function myFunction(event) {
document.getElementById("demo").innerHTML = event.charCode;
}
</script>
</body>
</html> Expected sequence:
Actual sequence:
|
New update:For some reason Cypress mixes KeyCodes and CharCodes for all characters except regular letters (a,b,c,d,A,B...) and numbers (0,1,2,3,...) . For dot KeyCodes detection service: http://keycode.info Table:
|
type()
functiontype()
function
Thanks for the thorough issue description. The majority of our keyboard code is located here, if it helps track down some of the issue in the meantime before we can look at it ourselves: https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cypress/keyboard.coffee#L50 |
I think I'm running into this same issue. I have a keyboard listener for |
@crookedneighbor keys may be ignored because of javascript's verification. For example in our case, we are using regex and 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;
}
}
} but this is problematic when cypress mixes charCodes and keyCodes. |
We are evaluating cypress and having the same issue. Our input validation javascript is fired on the keypress event. We are also validating number input and when we type a period "." into the number field, the event.which value when running in cypress is 190 instead of 46. The Javascript event.which value is different between the keypress and the keydown events. The event.which value for the keypress event should be the ascii code (46) and for the keydown it should be the keycode (190). http://blog.pothoven.net/2008/05/keydown-vs-keypress-in-javascript.html |
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... |
Thanks @pj that work around is possible for us. |
The code for this is done, but this has yet to be released. We'll update this issue and reference the changelog when it's released. |
Released in |
Wow, well done cypress team! |
Current behavior:
Reference to ASCII table: https://www.asciitable.com
Normall ASCII charcode of dot character
.
is 46:But cypress with
type()
function uses charcode 190:Using code bellow it doesn't work too. Cypress just converts
String.fromCharCode(46)
to190
and normal.
is also190
.Code:
What it does:
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:
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']
The text was updated successfully, but these errors were encountered: