-
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
Inconsistant and invalid Events Typing using cypress #5650
Comments
The issue also seem to concern 'mousedown' and MouseEvent, except that I've added the tests for the MouseEvent to the MWE repo. |
Hey @avallete, I'm trying to narrow down exactly what the issue is from what you provided. Does the below summarize the totality of the issue? When using
Please clarify if I have missed something else. Thanks. |
I'll just add from my last comment that: When using the And also the strange behavior is that between But yeah @jennifer-shehane , you have summarized it way better than I did, thank's. |
Investigating a little bit on this issue, I've been able to found what seem to be the root of the behaviour I describe on this issue: cypress/packages/driver/src/cy/keyboard.ts Line 892 in 6749a0e
Replacing the But looking on the comment on top of this line, this fix may lead to some regression on Chrome < 63. |
I tried it with Cypress 4.12.1 +
Here are the screenshots. Something similar happens to It's a bit hard to figure out what made the difference. |
I had opened this issue so long ago that I had completely forgot about it. Related to your comment @sainthkh I've refactored my MEW to:
If you don't want to clone the MWE repository, here is the videos of the running tests to see what's wrong. KeyboardEvent tests: MouseEvent tests: FYI, the expected behaviour for both KeyboardEvent and MouseEvent can be summarize into three assertions who are the following ones:
I hope this addition may help to understand more clearly what's going on here. |
Finally, I understood what's going on. You provided us 9 tests for each event. I read them and learned that the last 3 are duplicates of the above 6. So, all we have to check was those 6. When we run the tests as-is with 4.12.0, only the test no. 3 passes. Other 5 fail. With the change to 'Event' to 'KeyboardEvent' you mentioned above, the test no.4 passes. Now, we have 4 tests to fix. When I saw the console log, the thing made me curious was this:
This problem happens with the test no. 1 and 6. And I finally learned why. It's because they use different To run the test application, Cypress uses iframe. When we write test code with So, the test 1 and 6 should be written like below: it('should trigger KeyboardEvent with .type inside Cypress event listener', (done) => {
cy.visit('fixtures/issue-5650-2.html')
console.log('test: should trigger KeyboardEvent with .type inside Cypress event listener')
cy.window().then((win) => {
cy.get('#test-input').then((jQueryElement) => {
let elemHtml = jQueryElement.get(0)
elemHtml.addEventListener('keydown', (event) => {
console.log(`trigerred event show from cypressListener file !
received key: '${event.keyCode}'
is instanceof KeyboardEvent ? : (${event instanceof KeyboardEvent})
event type: (${event.type})
event constructor: ${event.constructor}
event object:
`, event)
expect(event instanceof win['KeyboardEvent']).to.be.true
done()
})
})
})
cy.get('#test-input').type('A')
})
it('should trigger KeyboardEvent with .get.then .dispatchEvent on htmlElement inside html script event listener', () => {
cy.visit('fixtures/issue-5650-2.html')
console.log('test: should trigger KeyboardEvent with .get.then .dispatchEvent on htmlElement inside html script event listener')
cy.window().then((win) => {
cy.get('#test-input').then((jQueryElement) => {
let elemHtml = jQueryElement.get(0)
let kbEvent = new win['KeyboardEvent']('keydown', {
keyCode: 65,
which: 65,
shiftKey: false,
ctrlKey: false,
})
elemHtml.dispatchEvent(kbEvent)
})
})
cy.get('#result').contains('isKeyboardEvent: true')
}) Check that I used Finally, tests 2 and 4 are not fixed. The problem is here: cypress/packages/driver/src/cy/commands/actions/trigger.js Lines 8 to 16 in 1690d41
|
Before I go fix I'm asking this because to solve this problem, we need to add a big table of event name and event object types based on this. |
Hi there, As you mentioned, the last 3 tests are effectively a combination of the check in both (html script and tests runner) contexts. Which as you say, if all the above tests work, should also work. I do realize know that it may be confusing, sorry for that. Nice spot about the iframe and the use of win['KeyboardEvent']. It may be worthy to add this trick somewhere into the documentation since this problem should occurs for any On the question about 'why it is necessary ?':
|
There are 3 problems in this issue:
|
@sainthkh This seemed to be a use case that someone encountered in their app, where they were checking But yah, as you mentioned, this trigger thing may be a separate issue. Feel free to reopen that issue if it's the case. Also, maybe related??? #3686 |
@jennifer-shehane I guess so. After #8255 is done, I'll research it more deeply. |
I am not very experienced in the cypress codebase yet. But shouldn't the action "type" emit a KeyboardEvent instead of an HTMLEvent?
That way getModifierState() could work correctly? We currently have this problem in the project where we handle modifiers and it fails cause the method is not there. And including a workaround for cypress in the application code is kinda awkward. |
Ah I now also read all of #8255 So I assume my comment is obsolete then. Sorry for the noise |
I'm running into this issue because I have some Rust code which uses typed bindings to DOM APIs. The bindings provide a number of type definitions generated from WebIDL, and it's possible to cast between JS types. By default those casts use I have event handler helpers that take a typed callback and perform the conversion under the hood for the user. Elsewhere there are definitions mapping strings like I've had mixed results with removing the checks as it seems the bindings may rely on prototypes for doing their property lookups. The easiest fix that'd allow me to use cypress would be to emit an event with the correct prototype in its chain. EDIT: I should also note that #8255 will address the most common cases. |
The code for this is done in cypress-io/cypress#8305, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior:
Testing a code who used
event instanceof KeyboardEvent
, I've seen some strange behavior, digging up, it seems that there is some strange inconsistencies between the events triggered and also into the way they are sent to theeventListener
.Desired behavior:
Consistent behavior:
.type
.trigger({..., type: 'keydown'})
.get('#element').then(element.dispatchEvent(new KeyboardEvent('keydown', ...)))
Should all send a KeyboardEvent type event.
And the eventListener:
Should always return true.
Steps to reproduce: (app code and test code)
I've tested three different methods:
cy.type
,cy.trigger
,cy.get('#element').then( element.dispatchEvent)
.And two different
eventListener
for each test:cy.get -> addEventListener
You can find MWE here: https://github.com/avallete/cypress-test-tiny-event-bug
Simply run:
Versions
3.6.1, Linux, Chromium 78 / Electron 73
The text was updated successfully, but these errors were encountered: