elgentos / magento2-cypress-testing-suite

A community-driven Cypress testing suite for Magento 2
MIT License
172 stars 37 forks source link

Alternative totals calculation in cart for USA locale display in format n,nnn.nn #42

Open ProxiBlue opened 2 years ago

ProxiBlue commented 2 years ago

Is your feature request related to a problem? Please describe. Defined in cart.spec.js test 'displays the correct product prices and totals' the totals calculations seem off, or likely don;t cater for USA locale price display format of x,xxx.xx

The tests as is always fail not being able to compare price values/totals are correct, and it seems it only uses the thousands as a single int. In example below '2' from a total of 2,254.97 is definitely not equal to 48 (2 + 46) of the line items totals

image

Describe the solution you'd like I have created an alternative test block, that properly works out the totals, as is, not just the first digits of the toatls, as what seems to be happening. I did not do a MR, as I am unsure if these will work for other locales, and shouls likely just be in some form of docs for others to be able to use, and tweak to their needs. No need for others to work this out as well. Also, i am not a JS expert, so my code could likely be refined.

    it.only('displays the correct product prices and totals', () => {
        cy.visit(cart.url.product1Url)

        //check if product price matches with price in cart
        cy.get(cart.product.productPrice).then(($productPrice) => {
            const productPrice = Number($productPrice[0].textContent.replace(/[^0-9\.-]+/g, ""))
            Cart.addProductToCart(cart.url.product2Url)
            cy.visit(cart.url.cartUrl)
            cy.wait(3000)
            cy.get(cart.product1Price).then((itemPrice) => {
                let cartItemPrice = Number(itemPrice.text().replace(/[^0-9\.-]+/g, ""));
                cy.log(cartItemPrice)
                expect(productPrice).to.eq(cartItemPrice)
            })

            //change the qty value
            cy.get(cart.qtyInputField).eq(0).type('{backspace}2{enter}').then(($qty) => {
                const qty = parseInt($qty.val())
                cy.wait(3000)
                //check if qty * product subtotal displays the correct amount
                cy.get(cart.product1Subtotal).then(($subTotal) => {
                    const subTotal = Number($subTotal[0].textContent.replace(/[^0-9\.-]+/g, ""))
                    expect(productPrice * qty).to.equal(subTotal)

                })
            })
        })

        //check if the grand total is correct
        cy.get(cart.product1Subtotal).then(($total1) => {
            const subTotal1 = Number($total1[0].textContent.replace(/[^0-9\.-]+/g, "")).toFixed(2)
            cy.log(subTotal1)
            cy.get(cart.product2Subtotal).then(($total2) => {
                const subTotal2 = Number($total2[0].textContent.replace(/[^0-9\.-]+/g, "")).toFixed(2)
                cy.log(subTotal2)
                const calcTotal = Number(subTotal1) + Number(subTotal2)
                cy.log(calcTotal)
                cy.get(cart.grandTotal).then(($grandTotal) => {
                    const grandTotal = Number($grandTotal[0].textContent.replace(/[^0-9\.-]+/g, "")).toFixed(2)
                    cy.log(grandTotal)
                    expect(grandTotal).to.equal(calcTotal.toFixed(2))
                })
            })
        })
    })

image

Describe alternatives you've considered See above

Additional context None, see above