Testcafe过滤功能
Testcafe filter function
我有一个 Selector 过滤器函数,如下所示,它接受两个参数
{ subject: subject, from: from }
作为依赖对象。
但是在 运行 这个函数之后,我得到一个错误
ReferenceError: subject is not defined
async function getMessage(subject, from) {
return await Selector('[data-test=messageListItem]').filter(( message ) => {
return message.querySelector('[data-test=subject]').textContent.includes(subject) &&
message.querySelector('[data-test=email]').textContent.includes(from);
}, { dependencies: { subject: subject, from: from } });
}
TestCafe 团队可以帮我解决这个问题吗?
In the case of the .filter
method 你需要重写你的依赖参数({ dependencies: { subject: subject, from: from } }
)如下:
{ subject: subject, from: from }
我准备了一个示例测试来说明它:
import { Selector } from 'testcafe';
fixture `New Fixture`
.page `google.com`;
test('New Test', async t => {
await t
.click(Selector('#tsf').find('[name="q"]'))
.typeText(Selector('#tsf').find('[name="q"]'), 'testcafe')
.pressKey('enter');
await t.expect(Selector('.LC20lb').count).eql(10);
function fn (title) {
return Selector('.LC20lb').filter((node, idx) => {
return node.textContent.includes(title);
}, { title }); // dependencies parameter
}
await t.expect(fn('TestCafe').count).gt(1);
});
谢谢弗拉基米尔 A。
我有同样的问题,看起来我不应该跳过 TestCafe 中过滤功能的“可选”参数。
可以参考Selector.filter Method
-> 转到 filter(filterFn, dependencies)
并阅读可选参数 dependencies (optional):
的说明
Functions, variables, or objects passed to the filterFn function.
我有一个 Selector 过滤器函数,如下所示,它接受两个参数
{ subject: subject, from: from }
作为依赖对象。
但是在 运行 这个函数之后,我得到一个错误
ReferenceError: subject is not defined
async function getMessage(subject, from) {
return await Selector('[data-test=messageListItem]').filter(( message ) => {
return message.querySelector('[data-test=subject]').textContent.includes(subject) &&
message.querySelector('[data-test=email]').textContent.includes(from);
}, { dependencies: { subject: subject, from: from } });
}
TestCafe 团队可以帮我解决这个问题吗?
In the case of the .filter
method 你需要重写你的依赖参数({ dependencies: { subject: subject, from: from } }
)如下:
{ subject: subject, from: from }
我准备了一个示例测试来说明它:
import { Selector } from 'testcafe';
fixture `New Fixture`
.page `google.com`;
test('New Test', async t => {
await t
.click(Selector('#tsf').find('[name="q"]'))
.typeText(Selector('#tsf').find('[name="q"]'), 'testcafe')
.pressKey('enter');
await t.expect(Selector('.LC20lb').count).eql(10);
function fn (title) {
return Selector('.LC20lb').filter((node, idx) => {
return node.textContent.includes(title);
}, { title }); // dependencies parameter
}
await t.expect(fn('TestCafe').count).gt(1);
});
谢谢弗拉基米尔 A。 我有同样的问题,看起来我不应该跳过 TestCafe 中过滤功能的“可选”参数。
可以参考Selector.filter Method -> 转到 filter(filterFn, dependencies) 并阅读可选参数 dependencies (optional):
的说明Functions, variables, or objects passed to the filterFn function.