title |
---|
window |
Get the window
object of the page that is currently active.
cy.window()
cy.window(options)
{% fa fa-check-circle green %} Correct Usage
cy.window()
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.window()
.
Option | Default | Description |
---|---|---|
log |
true |
{% usage_options log %} |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout cy.window %} |
{% yields sets_subject cy.window 'yields the window
object' %}
cy.visit('http://localhost:8080/app')
cy.window().then((win) => {
// win is the remote window
// of the page at: http://localhost:8080/app
})
If the application sets a custom property, like:
window.tags = {
foo: 'bar',
}
Our test can confirm the property was properly set.
cy.window()
.its('tags.foo')
.should('equal', 'bar')
Note: Cypress commands are asynchronous, so you cannot check a property value before the Cypress commands ran.
it('equals bar', () => {
let foo
cy.window()
.then((win) => {
foo = win.foo
})
// variable "foo" is still undefined
// because the above "then" callback
// has not been executed yet
expect(foo).to.equal('bar') // test fails
})
Instead, use {% url cy.then()
then %} callback to check the value.
it('equals bar', () => {
let foo
cy.window()
.then((win) => {
foo = win.foo
})
.then(() =>
// variable "foo" has been set
expect(foo).to.equal('bar') // test passes
)
})
If an application takes a while to start, it might "signal" its readiness by setting a property that Cypress can wait for.
// app.js
// only set property "appReady" if Cypress is running tests
if (window.Cypress) {
window.appReady = true
}
Cypress Test Runner can wait for the property window.appReady
to be true
before every test
// spec.js
beforeEach(() => {
cy.visit('/')
cy.window().should('have.property', 'appReady', true)
})
{% note info "When Can The Test Start?" %}
{% url "This blog post" https://www.cypress.io/blog/2018/02/05/when-can-the-test-start/ %} explains how to use cy.window()
to spy on the DOM prototype
to detect when the application starts adding event listeners to the DOM elements. When this happens for the first time, the Test Runner knows that the application has started and the tests can begin.
See {% url '"Set flag to start tests"' https://glebbahmutov.com/blog/set-flag-to-start-tests/ %} for more examples. {% endnote %}
cy.window({ timeout: 10000 }).should('have.property', 'foo')
{% requirements parent cy.window %}
{% assertions retry cy.window %}
{% timeouts assertions cy.window %}
Get the window
cy.window()
The commands above will display in the Command Log as:
{% imgTag /img/api/window/window-command-log-for-cypress-tests.png "Command Log window" %}
When clicking on window
within the command log, the console outputs the following:
{% imgTag /img/api/window/console-shows-the-applications-window-object-being-tested.png "Console Log window" %}
{% history %}
{% url "0.20.0" changelog#0-20-0 %} | Can call {% url ".focus()
" focus %} and {% url ".blur()
" blur %} on cy.window()
{% url "0.11.6" changelog#0-11-6 %} | cy.window()
logs to Command Log
{% endhistory %}
- {% url
cy.visit()
visit %} - {% url
cy.document()
document %} - {% url
cy.its()
its %} - {% url 'Adding custom properties to the global
window
with the right TypeScript type' https://github.com/bahmutov/test-todomvc-using-app-actions#intellisense %} - {% url 'Set flag to start tests' https://glebbahmutov.com/blog/set-flag-to-start-tests/ %}