Skip to content

Latest commit

 

History

History
123 lines (81 loc) · 2.88 KB

arrayFunctions.md

File metadata and controls

123 lines (81 loc) · 2.88 KB

Array functions

usual JS array functions which are combined in Cypress

Installation

To install the plugin to your project please use:

npm install cypress-extender

in order to also add it to your package.json file so you can use:

npm install --save-prod cypress-extender

Manual

Once cypress-extender is installed you can use:

Map function

What is map function ?

Now when you get Cypress Chainable elements

you can use a map function,

exactly as you do in a normal JS array

        import { initCypressWithArrays } from 'cypress-extender';
        initCypressWithArrays();

        cy.get('li').map(e => e.text().trim()).then(texts => {
            cy.log('Texts are: ', texts);
        });

Reduce function

What is reduce function ?

Now when you get Cypress Chainable elements

you can use a reduce function,

exactly as you do in a normal JS array

        import { initCypressWithArrays } from 'cypress-extender';
        initCypressWithArrays();

    it('test array reduce with array', () => {
        cy.get('a').map(e => e.text()).reduce((acc, val) => {
            acc.push(val[0]);
            return acc;
        }, []).should('have.length.gt', 0);
    });

    it('test array reduce with string', () => {
        cy.get('a').map(e => e.text()).reduce((acc, val) => {
            acc += val[0] || '';
            return acc;
        }, '').should('have.length.gt', 0);
    });

    it('test array reduce with number', () => {
        cy.get('a').map(e => e.text()).reduce((acc, val) => acc += val.length, 0)
        .should('be.gt', 0);
    });

Every function

What is every function ?

Now when you get Cypress Chainable elements

you can use an every function,

exactly as you do in a normal JS every

which checks that a callback function's result

on each element is true

Use:

import { initCypressWithArrays } from 'cypress-extender';
initCypressWithArrays();

it('test that every from the prevSubjet is a string', () => {
    cy.get('a').map(e => e.text()).every(v => typeof v === 'string').should('be.true');
});

Join function

What is join function ?

When you get Cypress Chainable elements with string values

you can use an join function,

exactly as you do in a normal JS join

which returns a joined string from the array of strings

NOTICE: when you use chainable which is not a strings array, the joined value will be ''

Use:

import { initCypressWithArrays } from 'cypress-extender';
initCypressWithArrays();

it('test join texts are given', () => {
    cy.get('a').map(e => e.text()).join("HOWAREYOU").should('include', 'HOWAREYOU');
});