alfredopacino
alfredopacino

Reputation: 3241

Accessing a global variable in Cypress

Is there a way I can access (and possibly edit) a global variable myVar from the Cypress side?

To be clear, by global I mean it is defined in the document object. I tried through cy.window()

cy.window().then((win) => {
  console.log(Cypress.$(win.document).myVar); //undefined
  console.log(Cypress.$(win.document).get(0).myVar); //undefined
});

Upvotes: 1

Views: 922

Answers (1)

Rosen Mihaylov
Rosen Mihaylov

Reputation: 1427

According to cypress docs here is the solution

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
    })
})

Upvotes: 2

Related Questions