如何对选择器的所有节点执行断言?

How to perform assertion for all nodes of a selector?

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

这将仅对 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");
}

是否有我缺少的内置方法?

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

正如@Alexander Moskovkin 的回答,"TestCafe doesn't have methods like expectEach..."。但是,我决定制作expectEachin my testcafe-utils module。如下面的示例用法所示,我建议在断言的 @message 参数中插入 eachAsync 的 @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}'.`));
})