0

If it possible in Cypress.io to save data in variables and reuse it in other block For example, I have a special name of device. I want to recieve this data and after clicking on it, check if title in new window contains that special name of device

I know how it make with static name, but I try to create dunamic autotest and I wanted to know if it possible

Briana
  • 204
  • 7
Yaroslav
  • 21
  • 2

2 Answers2

3

I have a feeling there is already an answer to this, but could not find one that is generally applicable.

Maybe a good approach is to use cypress-data-session. There is a lot of documentation but here is a sample usage:

beforeEach(() => {
  // let's say you want to set up the value "A"
  cy.dataSession(
    'A', // data name
    () => 'a', // data creation commands
    (x) => x === 'a', // data validation function
  )
})

it('has object A', () => {
  expect(Cypress.getDataSession('A')).to.equal('a')
})

Another option I have seen is using the Cypress environment as a data store.

// setting
Cypress.env('my-value', variable)

// retrieving
const myValue = Cypress.env('my-value')

The downside IMO is - it's not elegant to use Cypress.env() everywhere, and it can lose the values if Cypress browser resets.

Spike
  • 31
  • 3
1

Just put a const/var/let infront of all your test on the top and give it no value. in your test u can just give it an value and use it elsewhere in another test For example like this

var underlinedText

it("test", () => {
    cy.get('div>u').then(($btn) => {
        underlinedText = $btn.text();
    })
})

it("also test", () => {
    cy.get('div>a').should('contain', underlinedText);
})
skkrrea
  • 170
  • 9