0

I want to access the input value given/already present in the input field. I am trying the below solution but I can't access it outside the then function.

    let name
    cy.visit('/')
    cy.get('[id=username]').type("Kanye")
    cy.get('[id=username]').then((input) => {
      name = input.val()
      cy.log(name)  //Works here
    })
    cy.log(name)    //Does not work here

Can you help me modify this function? Or suggest me any alternative I can use.

Tirath Sojitra
  • 359
  • 4
  • 15
  • `then` implies it's asynchronous. So likely the `then` hasn't executed when you try to log name. That's why there is a `then` - so you can put your code that needs to wait for the async part in there. – James Feb 18 '22 at 21:07
  • This is my explanation of the multiple issues that arise from this code [Value stored in then block is empty when printed or used outside the block](https://stackoverflow.com/a/71088483/16997707) – Fody Feb 18 '22 at 21:09

0 Answers0