Using cy.writeFile and cy.readFile to capture element text

In one of my current tests, I have the need to capture the text of a button that changes based on prior tests that were executed. Basically, as tests are run the label of a button changes based on how many times that button has been clicked.

In order to capture the current label of that button, I used cy.writeFile to put that label in a text file for later use.

// SELECT FIRST AVAILABLE 
cy.get('[id="stall-button-0"]').click()
 .then(($el1) => {
  const text = $el1.text(); 
  cy.writeFile('cypress/integration/05-ops/filterValsStall.txt', $el1.text())
 })
cy.readFile('cypress/integration/05-ops/filterValsStall.txt')
.should('exist').and('contains', 'Test')

At this point, the contents of that file is simply

Test_103

Then, I later call that same file using cy.readFile and type that text into another field:

    cy.get('[class*="__flex-wrapper--title"]').click({force:true})
    cy.readFile('cypress/integration/05-ops/filterValsStall.txt')
    .then(($el1) => {
    cy.get('[id="STALL NUMBER"]').focus().type($el1)
    })
Previous
Previous

More fun with Payments

Next
Next

What is Cypress?