Testcafe: Add a .map function

Created on 7 Mar 2018  路  4Comments  路  Source: DevExpress/testcafe

Are you requesting a feature or reporting a bug?

Feature

What is the current behavior?

N/A

What is the expected behavior?

Generic map function that allows you to map over a Selector. This could be useful with creating assertions that deal with predefined/ expected lists vs actual lists created by the DOM. Since there is already a .filter function, and people always talk about map/filter/reduce being together, this seems like a natural addition to the Selector API.

How would you reproduce the current behavior (if this is a bug)?

N/A

Provide the test code and the tested page URL (if applicable)

Tested page URL:

Test code

Selector('li').map((element) => element.innerText);

Specify your

  • operating system: Windows 10
  • testcafe version: 0.18.1
  • node.js version: 8.8.1
Stale enhancement

Most helpful comment

The suggested code won't work because NodeList does not support "map"

const getInnerTextArr = ClientFunction( function ( selector ) {
    const values = []
    const elements = document.querySelectorAll( selector )

    for ( let i = 0; i < elements.length; i++ ) {
        values.push( elements[ i ].textContent )
    }

    return values
} )

All 4 comments

Hi @dv297,

Thanks for you proposal, it sounds reasonable.

We need to know what will be returned by Selector methods (a DOM node or something else). Obviously, we can't determine it in case of the map function.
To implement these methods, we would have to rethink the internal Selector methods mechanism.
We'll discuss the possibility to do this in the future.

As a workaround, you can create a ClientFunction, e.g.:

const getInnerTextArr = ClientFunciton((selector) => {
    return document.querySelectorAll(selector).map(el => el.innerText);
});

test('Test', async t => {
    await t.expect(getInnerTextArr('li')).eql(...);
});

/cc @AlexanderMoskovkin

The suggested code won't work because NodeList does not support "map"

const getInnerTextArr = ClientFunction( function ( selector ) {
    const values = []
    const elements = document.querySelectorAll( selector )

    for ( let i = 0; i < elements.length; i++ ) {
        values.push( elements[ i ].textContent )
    }

    return values
} )

This issue has been automatically marked as stale because it has not had any activity for a long period. It will be closed and archived if no further activity occurs. However, we may return to this issue in the future. If it still affects you or you have any additional information regarding it, please leave a comment and we will keep it open.

We're closing this issue after a prolonged period of inactivity. If it still affects you, please create a new issue with up-to-date information. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ParachuteCat picture ParachuteCat  路  3Comments

jvanoostveen picture jvanoostveen  路  4Comments

fnlctrl picture fnlctrl  路  3Comments

madroneropaulo picture madroneropaulo  路  3Comments

Lukas-Kullmann picture Lukas-Kullmann  路  3Comments