https://cypress.io logo
Define Value Synchronously
# i-need-help
w
Hi, I want to define a value after the Cypress do something first. Here are my codes:
Copy code
cy.get("strong + .btn-close").should("be.visible").click().then(function() {
let text = moment().format("YYYY-MM-DD hh:mm");
return text
});
cy.contains(text).should("be.visible");
The result is: text is not defined What did I do wrong here? Thank you.
e
The problem you are having in this example doesn't have to do with synchronousity. You are defining the
text
variable within a different scope than your
cy.contains(text)
validation This is just a JS problem where you are trying to reference a variable that isn't defined in the correct scope. If you move
cy.contains(text).should("be.visible");
within the
.then
this should would just fine without having to return any variable.