【问题标题】:Cypress - delete text from const赛普拉斯 - 从 const 中删除文本
【发布时间】:2019-10-25 09:04:50
【问题描述】:

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

看起来像这样:

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č”)吗?

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

感谢您的所有帮助。

【问题讨论】:

标签: automation automated-tests cypress


【解决方案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);
        });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    相关资源
    最近更新 更多