I am having an issue trying to share an alias between two steps in cypress. Basically what I am trying to do is in the first then step, I get the value and pass it in as a variable storedOddsValue. In the second then step, when I try to get that alias value and complete the step, it fails because I beleive it's coming back with undefined.
I know this because instead of using:
let storedOddsValue = oddslib.from("fractional", cy.get("@storedOddsValue"));
If I replace the alias with the hardcoded value like so:
let storedOddsValue = oddslib.from("fractional", "13/5");
The whole code below executes fine and the assertion passes. So my question is what am I doing wrong, why is it not getting the alias?
Then ("The prices are formatted in fractions by default", () => {
priceFormatElements.priceFormatDropdown().should('have.value', 'Fraction');
const oddsValue = oddsSelectionElements.oddsButton().first().invoke("text");
oddsValue.then((oddsValue) => {
expect(oddsValue.trim()).contains("/");
oddsValue.trim();
}).as("storedOddsValue")
})
Then ("The prices are formatted in {string}", (priceFormat) => {
let expectedOddsValue;
let currentOddsValue = oddsSelectionElements.oddsButton().first().invoke("text");
//let storedOddsValue = oddslib.from("fractional", "13/5");
let storedOddsValue = oddslib.from("fractional", cy.get("@storedOddsValue"));
switch (priceFormat) {
case "Fractional":
expectedOddsValue = storedOddsValue.to("fractional");
break;
case "Decimal":
expectedOddsValue = storedOddsValue.to("decimal", {precision: 2});
break;
case "American":
expectedOddsValue = "+" + storedOddsValue.to("moneyline");
break;
default:
throw new Error('unknown price format: ' + priceFormat);
}
currentOddsValue.then((oddsValue) => {
expect(oddsValue.trim()).equals(expectedOddsValue);
});
})
Not sure what oddslib.from does, but to use it with an alias you need to nest in this way
Then (... () => {
let expectedOddsValue
let currentOddsValue = oddsSelectionElements.oddsButton().first().invoke("text")
cy.get("@storedOddsValue").then(aliasValue => {
let storedOddsValue = oddslib.from("fractional", aliasValue)
switch (priceFormat) {
...
}
currentOddsValue.then((oddsValue) => {
expect(oddsValue.trim()).equals(expectedOddsValue);
});
}) // end of cy.get("@storedOddsValue")
})
Because cy.get("@storedOddsValue") is asynchronous, and the switch() runs before it gives it's value.