Skip to content
Advertisement

Testcafe filter function

I have a filter function for Selector as following which accepts two parameters { subject: subject, from: from } as a dependencies object.

But after running this function, I am getting an error

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 } });
}

Can TestCafe team please help me with this?

Advertisement

Answer

In the case of the .filter method you need to rewrite your dependencies parameter ({ dependencies: { subject: subject, from: from } }) as follows:

{ subject: subject,  from: from }

I have prepared a sample test to illustrate it:

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);
});
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement