【问题标题】:How to make assertions with variable text content如何使用可变文本内容进行断言
【发布时间】:2021-07-08 20:56:33
【问题描述】:
await t.expect(Selector('#elementId').innerText)
       .eql('Discounted 20% with your subscription');

这里是文本 - Discounted 20% with your membership
20% 不是恒定的,它可能会变为 15% 或 19%

我怎样才能“断言”这个?

我试过了

await t.expect(Selector('#elementId').innerText)
       .eql('Discounted' + 'with your subscription');

但是没用

【问题讨论】:

    标签: typescript testing automated-tests e2e-testing testcafe


    【解决方案1】:

    您可以使用正则表达式。

    const regexp = new RegExp (your regular expression);
    
    await t.expect(Selector('#elementId').innerText).match(regexp);
    

    有关详细信息,请参阅此文档文章:https://devexpress.github.io/testcafe/documentation/reference/test-api/testcontroller/expect/match.html

    【讨论】:

      【解决方案2】:

      这可能不是解决这个问题的最优雅的方法,但我能想到的一个解决方案如下:

      const myDiscountList = ["20", "19", "15"];
      const innerTextOfMyElement = await Selector('#elementId').textContent;
      
      await t.expect(myDiscountList.some(discount => innerTextOfMyElement === ("Discounted " + discount + " with your membership"))).ok("At least one of the expected discounts should match!");
      

      解决这个问题的更优雅的方法可能是使用正则表达式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多