赛普拉斯-从const中删除文本

时间:2019-10-25 09:04:50

标签: automation automated-tests cypress

我需要帮助。我有控制某些值是否大于其他值的代码。

它看起来像这样:

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);
        });
    });

问题在于,保存的文本不仅是简单的数字,而且总是以“Kč”结尾。有什么办法可以删除此文本(“Kč”)?

我试图将文本解析为浮动文本,但效果不佳。

感谢您的所有帮助。

1 个答案:

答案 0 :(得分:1)

这里有一个快速技巧,可以解析字符串中的数字:

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);
        });
    });
});