# Window

Examples of referencing window and other properties on window in Cypress, for a full reference of commands, go to docs.cypress.io (opens new window)

# cy.window() (opens new window)

To get the global window object, use the cy.window() command.

cy.window().should('have.property', 'top')

You can use the yielded window object to call the methods on that window directly. For example, if the application code adds a method getAppName to the window object, you can execute the method from the spec. You can even pass arguments and confirm the result.

  // the application adds a method "getAppName" at some point
  setTimeout(function () {
    window.getAppName = function (version) {
      return `App_v${version}`
  }, 1000)
// if the method exists on the "window" object, we can immediately call it
cy.window().then((w) => {
  expect(w.eval('2 + 2')).to.equal(4)
// if the method is added at some point in the future
// we can use built-in retry-ability using cy.invoke command
  // This command waits until the method exists
  // the calls it with the given arguments
  .invoke('getAppName', '1.0')
  // confirm the result
  .should('equal', 'App_v1.0')

Watch the explanation in the video Call Window Method From Cypress Spec (opens new window).

# cy.document() (opens new window)

To get the document object, use the cy.document() command.

  .should('have.property', 'charset')
  .and('eq', 'UTF-8')

# cy.title() (opens new window)

To get the title, use the cy.title() command.

  document.title = 'Window APIs'
cy.title().should('include', 'Window APIs')