https://cypress.io logo
How can I use a variable for the next test?
# i-need-help
l
Hello, good afternoon. I need to store one variable from one test, to use in another test, which is the best way to do that? Also another question... The value of my var is inside of this Copy Button at the right, but once I click the CopyButton to get the value, a PopUp appears with the value, but Idk how I can access to the value. I was trying something like this:
Copy code
js
Cypress.Commands.add('getBySel', (selector, ...args) => {
  return cy.get(`[data-test-id="${selector}"]`, ...args);
});
Copy code
js
 cy.getBySel('SUPPLY KEY').then((key) => {
        console.log('Key - cy.getBySel',key);
        let SUPPLY_KEY = key;
        cy.wrap(SUPPLY_KEY).as('wags');
      });
But the val I'm getting is (Attach to second Image). Any help pls?
g
quick question: if you are using the copied value, why is this a new test?
l
Because I need to use that value for another test
Other test is gonna be burn NFTs
And I need that value
But even in the same test I can't access to the value
Since is inside of the copy button and when I press the copy button appears a Popup (Only in Cypress and is an Input of the own browser not mine).
m
Instead of using let, use var
Let has a block scope and var has a global scope, this means that var can be used outside of the test suite while let can only be accessed in 1 test suit where it was declared.
l
Yeah, but that's not the problem, the problem is I can't even get the value in any scope
Since Cypress instead of copy the value in my button and It happens in my real App, open a new PopUp with an Input and there is the value but Idk how I can acces to that
p
https://docs.cypress.io/api/cypress-api/env store it in the env in the before or beforeEach, use it in as many tests as you want. Its a bad practice to make tests dependable on each other
l
Ty
Also I have another Issue with Cypress
1 each 5 runs, my test one of my comprobations fails
In each onChange I do a request to an API to see If the Account Id is good or not
And as I said 1 of each 5 times, cypress doesn't get the good response
The other 4 times the test finish correctly
I have this same issue in other test when I'm checking some of the keys
p
either something to do with backend services returning different information
or you arent implementing retryability correctly
i
Hello all 👋 I use cy.readFile and cy.writeFile commands for that. Basically I store the texts that I need to use on other tests in a .json file, here is an example: * To store
Copy code
cy.get(anElement).invoke('text').then((aText) => {

            cy.readFile('yourFixtureFile.json').then((anObject) => {
                anObject.aKey = aText
                cy.writeFile('yourFixtureFile.json', anObject)
            })
        })
* To use the stored text in another test
Copy code
cy.readFile('yourFixtureFile.json').its('aKey').then((aText) => {

            cy.get(anElement).invoke('text').then((anElementText) => { expect(aText).have.contain(anElementText) })
        })
I hope this will help you
l
Yup I'm already using it
Copy code
js
 cy.fixture('kabiUser.json').then((kabiUser) => { })
Nope, that's not possible and outside of Cypress never fails either we never have any problem
3 Views