vendredi 1 janvier 2021

Passing a variable's value from a test to the next one in Cypress

Is it possible to pass a variable from one it test to the next it test ? The following using cy.wrap does not work:

it('test1', () => {
 const var1 = 'test'
 cy.wrap(var1).as('var1Alias')
})
it('test2', () => {
 cy.log('@var1Alias')
})

I've checked old stackoverflow questions similar to mine such as this: Use variables across multiple 'it' statements to track change in Cypress and the difference is that the variable is declared globally outside the tests (it). Hence, you can always replace the variable.

My specific issue is that the dependency on the variable's value from the previous test. I know Cypress best practice suggests that tests shouldn't be sequential but this is a common scenario in my opinion to properly categorise tests in a readable manner.

For context, my first test is calling a POST endpoint then the response body will be passed as a query parameter for my GET endpoint in the succeeding test.

Current workarounds (while I am still looking for a better option):

  • Use fixtures and write then retrieve (slower runtime)
  • Use localstorage-commands plugin (I have multiple variables to retrieve)
  • Include the succeeding test inside the arrow function of the previous test (thus combination both tests in one test)

Aucun commentaire:

Enregistrer un commentaire