Window

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

cy.window()open in 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.

<script>
  // the application adds a method "getAppName" at some point
  setTimeout(function () {
    window.getAppName = function (version) {
      return `App_v${version}`
    }
  }, 1000)
</script>
// 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
cy.window()
  // 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 Specopen in new window.

cy.document()open in new window

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

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

cy.title()open in new window

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

<script>
  document.title = 'Window APIs'
</script>
cy.title().should('include', 'Window APIs')