1

在我的 testcafe 测试中,我有一个匹配多个节点的选择器。我想在此选择器匹配的所有节点上执行断言。

这将仅对返回的第一个元素执行断言mySelector

await t.expect(mySelector.innerText).eql("foo");

这将在所有元素上执行它,但它真的很冗长:

const count= await mySelector.count;
for (let i = 0; i < count; ++i) {
    await t.expect(mySelector.nth(i).innerText).eql("foo");
}

有没有一种我缺少的内置方法可以做到这一点?

4

2 回答 2

1

TestCafe 没有这样的方法,expectEach所以我认为您提出的方法是最好的方法。它添加了几行代码,但它清楚地表明了您要在测试中检查的内容。

于 2017-09-11T15:09:01.873 回答
1

正如@Alexander Moskovkin 所回答的那样,“TestCafe 没有像expectEach......这样的方法”。但是,我决定expectEach 在我的 testcafe-utils 模块中制作。如下面的示例用法所示,我建议eachAsync在断言的 @message 参数中插入 的 @n 参数,以便在测试失败时知道哪个第 n 个元素导致失败。

const { Selector } = require('testcafe');
const tu = require('testcafe-utils');

const baseURL = 'http://www.imdb.com/title/tt0092400/ratings?ref_=tt_ov_rt';

fixture `IMDB`
  .page `${baseURL}`;

const mySelector = Selector('#main .title-ratings-sub-page table:nth-of-type(2) tr');

test('it', async t => {
  await tu.expectEach(mySelector, n => t.expect(mySelector.nth(n).innerText).match(/all/gi, `Failed on n '${n}'.`));
})

于 2018-02-05T05:18:35.627 回答