I want to use a value of a var that is set inside of .then()
in the outer scope
I tried
let numOfDropdownFields = 0;
cy.get("dropdown")
.find("button")
.its("length")
.as("len")
.then(($len) => {
cy.log("No. of buttons:" + $len);
numOfDropdownFields = $len;
cy.get("@len").should("eq", numOfDropdownFields);
cy.log(numOfDropdownFields);
});
cy.log(numOfDropdownFields);
The first log prints: No. of elements: 14
The second log: 14
And the third log: 0
How can I see the value 14 outside the .then()
callback?
What you are trying to achieve is considered an antipattern in cypress. Generally you should do everything you want to do with that Variable inside the .then()
.
Make sure to use Syntax like: .then() => {all your code in here}
.
Look here for Reference: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Aliases
Also you can Use Aliases like you already did with .as('len')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With