Skip to content

Commit

Permalink
refactoring clickOnElements function
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-hanneforth committed Jun 18, 2021
1 parent 49cc19d commit 64e785e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@ const evaluate = async (page, js, args) => {
*
* @param {puppeteer.Page} page
* @param {String} querySelector
* @param {Object} property
* @param {Object} properties
* @returns {Promise<Boolean>} whether an element has been clicked or not
*/
const clickOnElements = async (page, querySelector, property) => {
await evaluate(page, (args) => {
const elements = [...document.querySelectorAll(args.querySelector)];
const keysProp = Object.keys((args.property ? args.property : {}));
elements.forEach((element) => {
const matchesProperties = keysProp.reduce((acc, cur) => {
if (args.property[cur] == element[cur]) return true;
const clickOnElements = async (page, querySelector, properties) => {
const clickedOnElement = await page.evaluate(({ querySelector, properties }) => {
const elements = [...document.querySelectorAll(querySelector)];
const keys = Object.keys(properties ? properties : {});
const matchedElements = elements.filter(element => {
const matchesAllProperties = keys.reduce((acc, cur) => {
if(!acc) return false;
if(properties[cur] == element[cur]) return true;
return false;
}, true);
if (matchesProperties) element.click();
return matchesAllProperties;
});
}, { querySelector, property });
matchedElements.forEach(element => element.click());
if(matchedElements.length > 0) return true;
return false;
}, { querySelector, properties });
return clickedOnElement;
};

/**
Expand Down

0 comments on commit 64e785e

Please sign in to comment.