【问题标题】:Multiple values in a single assertion in webdriveriowebdriverio中单个断言中的多个值
【发布时间】:2021-01-19 12:03:06
【问题描述】:

是否可以为单个期望添加多个可能的值?就我而言,标题可能具有不同的文本值,具体取决于网站上的其他进程(下面的代码无法正常工作)。

it('card should have a one of two names', () => {
    expect(MyPage.cardName).toHaveText('Good Products' || 'Wonderfull Products');
});

chai 的deep.equal 也有同样的问题。一个断言中是否可以有多个变体?

it('should verify the card titles list', () => {
    // an array is created here
    const cardTitles = MyPage.cardTitlesText();
    chaiExpect(cardTitles).to.deep.equal(['Apples', 'Carrots', 'Tomatoes', 'Potatoes'] || ['Potatoes', 'Apples', 'Carrots', 'Tomatoes']);
});

或者也许我错过了测试的整体概念中的某些东西,而这应该首先以不同的方式处理?

【问题讨论】:

    标签: javascript testing e2e-testing webdriver-io


    【解决方案1】:
    'Good Products' || 'Wonderfull Products'
    

    这基本上意味着“如果左边的操作数为假,则返回右边的值”。但是你总是在那里有Good Products,它永远不会评估为假。

    与第二个示例中的数组类似。即使您有一个空数组,这也不会评估为 false:

    [] || [4]
    

    仍然返回[]

    这只是 JavaScript,所以你可以在这里阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR

    【讨论】:

    • pavelsaman,谢谢,我完全明白你在说什么,但 WebdriverIO 只检查第一个值,而不考虑右边的值。如果其中一个值对应于报废网站返回的值,我应该如何让 WebdriverIO 考虑这两个值并通过测试? (a || b 只是我正在谈论的视觉示例)
    • 我明白了。我不认为 WebdriverIO 对此有直接的支持。也许您可以使用 chai 库和 oneOf 方法。我认为这将是最优雅和可读的解决方案。
    【解决方案2】:

    现在可以在最新版本的内置expect-webdriverio中,只需传递一个字符串数组,见https://webdriver.io/docs/api/expect-webdriverio.html#tohavetext

    请随意向https://github.com/webdriverio/expect-webdriverio提出功能请求

    【讨论】:

    • Mike G.,不知何故这对我不起作用。我错过了什么?代码:it("card should have a one of two names", () => { expect(MyPage.cardName).toHaveText(['Good Products', 'Wonderfull Products']); }); 结果1) MayPage tests Product card should have a proper name [chrome 87.0.4280.141 mac os x #0-0] Expect $(".card-header span[class="card-title"]") to have text Expected: ["Good Products", "Wonderfull Products"] Received: "Wonderfull Products"
    • 确保安装最新版本的expect webdriverio
    猜你喜欢
    • 2014-10-27
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2017-04-09
    相关资源
    最近更新 更多