-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add doc for window events #1512
Closed
chrisbreiding
wants to merge
3
commits into
issue-890-background-updates
from
issue-1465-window-events
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
title: window:<event> | ||
--- | ||
|
||
You can use the `window:<event>` event to listen for arbitrary events on the page's `window`, where `<event>` is any event name that can be listened for on a page's `window`. This is the same as listening to the events with `window.addEventListener` with the following advantages: | ||
|
||
* You can listen before the page has loaded (before {% url "`cy.visit()`" visit %}) and the listener will be bound once the page loads. | ||
* If the page changes or transitions, the listener will be re-bound when the new page loads. | ||
* If you listen with `cy.on` (as opposed to `Cypress.on`), the listener will be unbound when the test ends. | ||
|
||
There are a large number of events you can listen to. {% url "View this reference" https://developer.mozilla.org/en-US/docs/Web/Events %} for list of many of them. | ||
|
||
# Environment | ||
|
||
{% wrap_start 'event-environment' %} | ||
|
||
Some events run in the {% url "browser" all-events#Browser-Events %}, some in the {% url "background process" background-process %}, and some in both. | ||
|
||
Event | Browser | Background Process | ||
--- | --- | --- | ||
`window:<event>` | {% fa fa-check-circle green %} | {% fa fa-times-circle grey %} | ||
|
||
{% wrap_end %} | ||
|
||
# Arguments | ||
|
||
**{% fa fa-angle-right %} event** ***(Object)*** | ||
|
||
The event object. The type of object and properties on the object depend on the event. | ||
|
||
# Usage | ||
|
||
To listen to an event on the window, prefix it with `window:`. For example, to listen to the `resize` event, listen to `window:resize`. | ||
|
||
## In the browser | ||
|
||
In a spec file or support file you can tap into the `window:<event>` event. In this example we listen to the `hashchange` event via `window:hashchange` in order to assert that the URL changed as expected. | ||
|
||
```javascript | ||
it('visits about when "about" is clicked', (done) => { | ||
cy.on('window:hashchange', (event) => { | ||
// event looks like: | ||
// { | ||
// bubbles: false | ||
// defaultPrevented: false | ||
// newURL: "http://localhost:8080/index.html#about" | ||
// oldURL: "http://localhost:8080/index.html" | ||
// target: Window {...} | ||
// type: "hashchange" | ||
// ... more properties ... | ||
// } | ||
|
||
expect(event.newUrl).to.match(/about/) | ||
done() | ||
}) | ||
|
||
cy.visit('http://localhost:8080/index.html') | ||
// this will navigate to #about and trigger a hashchange event | ||
cy.get('.nav .about').click() | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What if their page has several
window
s, like if they have iframes on the page, can they listen to the iframe's window events? Can we show an example of this if so?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.
No, it's the status quo as far as iframe support is concerned. It only binds to the AUT's top window. We'll have to do extra work when we add iframe support, but it shouldn't change the API. The window is available as
event.target
, so the user can know which iframe window it is based on that.