Assigning input value to variable in Cypress

  Kiến thức lập trình

For a Cypress e2e test I need to read out the value of an input field into a variable, but I haven’t found a proper way to do this. First, I tried this:

  cy.get('[data-cy=password-input]')
  .invoke('val').then((val) => {
        password = val.toString()
        cy.log(password)
    })

cy.wait(3000)

cy.log(password) // <--- undefined

It logs the correct value inside the then-block, but even after 3 seconds it doesnt seem to be assigned to the variable.
Then I tried returning the password inside the block to assign the return value, but the toString() call just returns an object.

password = cy.get('[data-cy=password-input]')
  .invoke('val').then((val) => {
        return val.toString();
    })

I wouldnt have assumed it takes hours to figure out a basic thing like this. How do I get the value of an input into a variable to use later in the test?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT