I am testing the eshop.
I'm trying to verify that if you buy one product, the price will be the same as the final purchase price.
If I buy only 1 specific product (doesnt matter about quantity), then Final Price = Price Holder.
How can I verify that the price is equal? I can't use .should("have.text", a specific text) because the number is dynamic.
I find both elements and log their text. I was trying to compare them, but the results shows me error, that FinalPrice is not defined. It is because the const is defined in function before?
Probably I have to do it another way. Do you see better solution?
cy.log("Log Final Price")
cy.get(".final-price")
.find("strong")
.then(($strong) => {
const FinalPrice = $strong.text()
cy.log(FinalPrice);
})
cy.log("Log Price Holder")
cy.get(".price-holder")
.then(($span) => {
const PriceHolder = $span.text()
cy.log(PriceHolder)
})
cy.log("Price Equality")
cy.get(FinalPrice).should("be.equal", PriceHolder)
You would save intermediate data into aliases
// select an item
cy.get(".price-holder")
.invoke('text')
.as('priceHolder')
cy.get(".final-price")
.find("strong")
.invoke('text')
.as('final')
cy.get('@final').then((final) => {
cy.get('@priceHolder').then((priceHolder) => {
expect(parseFloat(final)).to.eq(parseFloat(priceHolder))
})
})
Of course, if there are no intervening steps, you can just nest the queries
cy.get(".final-price")
.find("strong")
.invoke('text')
.then(text => parseFloat(text))
.then((final) => {
cy.get(".price-holder")
.invoke('text')
.then(text => parseFloat(text))
.should('eq', final)
})
but usually you will have some sort of page manipulation such as selecting items for the shopping cart, so using aliases is the way to go in that kind of test.