1

I need to help. I have code that is controlling if some value is greater than other value.

It looks like this:

cy.get(':nth-child(1) > .offer-block > :nth-child(1) > .flex-col > .offer-price').then(($span) => {
        // capture what num is right now
        const num1 = $span.text();

        cy.get(':nth-child(2) > .flex-column').click();
        cy.wait(5000);
        cy.get(':nth-child(1) > .offer-block > :nth-child(1) > .flex-col > .offer-price').then(($span) => {
          // now capture it again
          const num2 = $span.text();

          // make sure it's what we expected
          expect(num1).to.be.greaterThan(num2);
        });
    });

Problem is that saved text isn't only simple number but it allways ends with " Kč". Is there some way to delete this text (" Kč")?

I was trying to parse text to float but it didn't end well.

Thanks for all your help.

Dominik Skála
  • 793
  • 4
  • 12
  • 30
  • 1
    Maybe simplest is `$span.text().slice(0, -3)`, see [JavaScript chop/slice/trim off last character in string](https://stackoverflow.com/a/952945/4716245). –  Oct 25 '19 at 19:53

1 Answers1

1

Here's a quick hack that will parse the string for a number:

describe('test', () => {
    it('test', () => {
        cy.document().then(doc => {
            doc.body.innerHTML = `
                <!-- regular space as separator -->
                <div class="test">7 201 Kč</div>
                <!-- U+202F NARROW NO-BREAK SPACE separator -->
                <div class="test">7 201 Kč</div>
            `;
        });

        cy.get('.test').each( $el => {
            const number = parseInt(
                $el.text()
                    // match number, including spaces as number
                    //  separators
                    .match(/([\d\s]+|$)/)[1]
                    // remove non-numeric characters
                    .replace(/[^\d]/g, ''),
                // interpret as base-10
                10
            );

            expect(number).to.be.gt(5000);
        });
    });
});
dwelle
  • 6,894
  • 2
  • 46
  • 70