window

Improve this doc

Get the window object of the page that is currently active.

Syntax

cy.window()
cy.window(options)

Usage

Correct Usage

cy.window()

Arguments

options (Object)

Pass in an options object to change the default behavior of cy.window().

Option Default Description
log true Displays the command in the Command log
timeout defaultCommandTimeout Time to wait for cy.window() to resolve before timing out

Yields

  • cy.window() yields the window object.

Examples

No Args

Yields the remote 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
})

Start tests when app is ready

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)
})
When Can The Test Start?

This blog post 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 “Set flag to start tests” for more examples.

Options

Passes timeout through to .should() assertion

cy.window({ timeout: 10000 }).should('have.property', 'foo')

Rules

Requirements

  • cy.window() requires being chained off of cy.

Assertions

  • cy.window() will automatically retry itself until assertions you've chained all pass.

Timeouts

  • cy.window() can time out waiting for assertions you've added to pass.

Command Log

Get the window

cy.window()

The commands above will display in the Command Log as:

Command Log

When clicking on window within the command log, the console outputs the following:

Console Log

See also