-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
fix: Fire appropriate event when cy.type(). #8255
Conversation
Thanks for taking the time to open a PR!
|
@sainthkh we should always be able to workaround cases where the AUT overwrites some native class that we need, we backup the getter/setter or prototype as soon as the page is loaded before the AUT has time to overwrite it, see https://github.com/cypress-io/cypress/blob/composite-error/packages/driver/src/dom/elements.ts#L264 Is there any other reason we need to use Edit: looking at the tests and the related issues, it makes sense to move to KeyboardEvent, thanks for the PR! |
it('should trigger KeyboardEvent, not Event, for event listeners', () => { | ||
cy.visit('fixtures/issue-5650.html') | ||
|
||
cy.get('#test-input').type('A') | ||
cy.get('#result').contains('isKeyboardEvent: true') | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest something like (to keep test smaller, faster):
it('should trigger KeyboardEvent, not Event, for event listeners', () => { | |
cy.visit('fixtures/issue-5650.html') | |
cy.get('#test-input').type('A') | |
cy.get('#result').contains('isKeyboardEvent: true') | |
}) | |
it('should trigger KeyboardEvent, not Event, for event listeners', () => { | |
cy.$$('input:first').on('keydown', (e) => { | |
if (e instanceOf e.currentTarget.ownerDocument.defaultView.KeyboardEvent) return | |
throw new Error('event was not instanceOf KeyboardEvent') | |
}) | |
cy.get('input:first').type('A') | |
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to reuse the test created in #5650. But I prefer your way.
But the code above has 2 problems:
e
is not a native event. To access it, we need to usee.originalEvent
.
It's also important to note that the event object contains a property called
originalEvent
, which is the event object that the browser itself created. jQuery wraps this native event object with some useful methods and properties, but in some instances, you'll need to access the original event viaevent.originalEvent
for instance. This is especially useful for touch events on mobile devices and tablets.
done
function should be used. Otherwise, the test would pass without checkingkeydown
event. Or fail later when Cypress is running other tests.
I fixed these 2 problems and updated the test.
// promise in the submit command it will break again | ||
// consider changing type to a Promise and juggle logging | ||
return cy.now('submit', form, { log: false, $el: form }) | ||
// In Firefox, submit event is automatically fired |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice comments 👍
As I wrote in the comment, there are 3 problems in #5650:
|
893e521
to
b24a4a4
Compare
kitchensink tests failed because of the change in #7782. |
@sainthkh sigh, yah, these should pass but I think they fail for outside contributors because the run doesn't have access to env var |
Flaky failure. |
User facing changelog
cy.type()
now firesKeyboardEvent
, notEvent
.It didn't because it had to support Chrome < 63. But Chrome 63 isn't in the lastest list of chrominum. I guess we don't have to support it any more.
Additional details
Why was this change necessary?
cy.type()
firesEvent
class and it doesn't match the behavior of the Browsers.Event
class is overridden and you cannot fix the code of the site, cy.type() fails.What is affected by this change?
N/A
Any implementation details to explain?
I tried to simply revive the commented-out code, but there were some problems in Firefox.
TextInput
event. So, we had to check if it's a valid Event constructor and fallback toKeyboardEvent
.KeyboardEvent
withEnter
key is sent to an input field in a form in Firefox,submit
event is automatically triggered. I had to fix problems related with it.How has the user experience changed?
N/A
PR Tasks
cypress-documentation
?type definitions
?cypress.schema.json
?