Skip to content

Commit 3cd15c0

Browse files
committed
Add doc about Test Runner iframe.
1 parent dd8c67f commit 3cd15c0

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

source/api/commands/window.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,50 @@ See {% url '"Set flag to start tests"' https://glebbahmutov.com/blog/set-flag-to
137137
cy.window({ timeout: 10000 }).should('have.property', 'foo')
138138
```
139139

140+
# Notes
141+
142+
## Cypress uses 2 different windows.
143+
144+
Let's say you want to check the type of the events. You might write code like below:
145+
146+
```js
147+
it('test', (done) => {
148+
cy.get('#test-input').then((jQueryElement) => {
149+
let elemHtml = jQueryElement.get(0)
150+
151+
elemHtml.addEventListener('keydown', (event) => {
152+
expect(event instanceof KeyboardEvent).to.be.true
153+
done()
154+
})
155+
})
156+
157+
cy.get('#test-input').type('A')
158+
})
159+
```
160+
161+
It fails. But the interesting thing is that the type of `event` is `KeyboardEvent` when you `console.log(event)`.
162+
163+
It's because Cypress Runner uses `iframe` to load the test application. In other words, `KeyboardEvent` used in the the code above and the `KeyboardEvent` class from which `event` variable is constructed are different `KeyboardEvent`s.
164+
165+
That's why the test should be written like this.
166+
167+
```js
168+
it('should trigger KeyboardEvent with .type inside Cypress event listener', (done) => {
169+
cy.window().then((win) => {
170+
cy.get('#test-input').then((jQueryElement) => {
171+
let elemHtml = jQueryElement.get(0)
172+
173+
elemHtml.addEventListener('keydown', (event) => {
174+
expect(event instanceof win['KeyboardEvent']).to.be.true
175+
done()
176+
})
177+
})
178+
})
179+
180+
cy.get('#test-input').type('A')
181+
})
182+
```
183+
140184
# Rules
141185

142186
## Requirements {% helper_icon requirements %}

source/faq/questions/using-cypress-faq.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,7 @@ Yes. You can leverage visual testing tools to test that charts and graphs are re
660660

661661
- see {% url "Testing a chart with Cypress and Applitools" https://glebbahmutov.com/blog/testing-a-chart/ %}
662662
- see {% url "Testing how an application renders a drawing with Cypress and Percy.io" https://glebbahmutov.com/blog/testing-visually/ %}
663+
664+
## {% fa fa-angle-right %} Why doesn't `instanceof Event` work?
665+
666+
It might be because of the 2 different windows in Cypress Test Runner. For more information, please check {% url "the note here" /api/commands/window.html#Cypress-uses-2-different-windows %}.

source/guides/core-concepts/test-runner.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ The image below shows that our application is displaying at `1000px` width, `660
9191

9292
{% imgTag /img/guides/errors.png "Errors" %}
9393

94+
*Note: Internally, AUT uses iframe. It can sometimes cause unexpected {% url "behaviors like this." /api/commands/window.html#Cypress-uses-2-different-windows %}*
95+
9496
# Selector Playground
9597

9698
The Selector Playground is an interactive feature that helps you:

0 commit comments

Comments
 (0)