首页 > 解决方案 > Testcafe过滤功能

问题描述

我有一个 Selector 的过滤器函数,如下所示,它接受两个参数 { subject: subject, from: from }作为依赖项对象。

但是运行这个函数后,我得到一个错误

ReferenceError:未定义主题

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 团队可以帮我解决这个问题吗?

标签: javascripttestingautomated-testse2e-testingtestcafe

解决方案


.filter方法的情况下,您需要重写您的依赖项参数 ( { 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);
});

推荐阅读