usual JS array functions which are combined in Cypress
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
Once cypress-extender is installed you can use:
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);
});
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);
});
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');
});
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');
});