I am trying to re-use a variable or alias across multiple step definitions in my Cypress / Cucumber test.
Variable declaration & 1st step def:
let assetName = '';
And('I click on the button', (view) => {
if (view === 'Grid') {
homePage.getAssetName().first().then($el => {
assetName = $el.text();
}).click();
}
});
2nd step def:
And('I verify something', (view) => {
cy.get('.ItgAssetDetails-name').should('have.text', + assetName);
});
But I get the below error message:
expected <p.ItgAssetDetails-name> to have text NaN, but the text was lenses System
I've also tried assigning $el.text() an alias, but that hasn't worked either.
Can someone please show me what changes are required to pass this value between step definitions?
Your problem is likely a typo since you are calling .should('have.text', + assetName). The + is a unary operator trying to convert your string in assetName into a number and giving you NaN
console.log(+"Hello there")
Just remove the +
.should('have.text', assetName)