Skip to content

Commit

Permalink
feat: Support for adding attributes to an element before visual testi…
Browse files Browse the repository at this point in the history
…ng (#68)
  • Loading branch information
brankol authored Nov 18, 2022
1 parent de8e6b0 commit 08b5a3d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions resources/Visual/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ module.exports = async (page, scenario) => {
scenario.keyPressSelectors || scenario.keyPressSelector;
const scrollToSelector = scenario.scrollToSelector;
const postInteractionWait = scenario.postInteractionWait; // selector [str] | ms [int]
const addAttribute = scenario.addAttributes || scenario.addAttribute;

if (addAttribute) {
for (const item of [].concat(addAttribute)) {
if (typeof item === 'object' && item.hasOwnProperty('selector') && item.hasOwnProperty('attribute')) {
await page.waitForSelector(item.selector);
await page.evaluate((item) => {
document.querySelectorAll(item.selector).forEach(el => {
const name = item.attribute.name;
const value = item.attribute.value;
const delimiter = item.delimiter || ' ';
if (el.getAttribute(name)) {
const prevValue = el.getAttribute(name);
el.setAttribute(name, prevValue + delimiter + value);
} else {
el.setAttribute(name, value);
}
});
}, item);
} else {
throw new Error('addAttribute must be an object: {selector: "", attribute: {name: "", value: ""}}');
}
}
}

if (keyPressSelector) {
for (const keyPressSelectorItem of [].concat(keyPressSelector)) {
Expand Down

0 comments on commit 08b5a3d

Please sign in to comment.