var objarray = I.executeScript(function(_locator) { return document.querySelectorAll(_locator)});
var i = 0;
for (i ; i < objarray.length; i++) {
I.seeAttributesOnElements(objarray.item[i],{ 'aria-expanded': "false"});
objarray.item[i].click();
I.seeAttributesOnElements(objarray.item[i],{ 'aria-expanded': "true"});
}
I tried with NO success with these two plugins also:
You use one of the last versions of codecept, where page objects and helpers inject method was changed form const I = actor(); to const { I } = inject();
see https://codecept.io/pageobjects#pageobject
Also, you do not send argument _locator to function in execute script.
You should add it as argument to executeScript after function: I.executeScript(function(_locator) { return document.querySelectorAll(_locator)}, _locator);
see 2nd example from section https://codecept.io/helpers/Puppeteer/#executescript
I have had a similar issue in the past. The way I solved it was to implement a helper using executeScript function rather than using it in a pageObject. This is for WebDriver however, so you will need to probably adapt the implementation to puppeteer:
This is inside my wdio-helper file
/**
* This functions is focused on helping to click elements by index
* @param {String} selector - string selector - CSS mandatory
* @param {Number} index - position of the locator if in array of elements
*/
async clickByIndex(selector, index = 0) {
const helper = this.helpers.WebDriver;
return helper.executeScript(async (selector, index) => {
document.querySelectorAll(selector)[index].click();
}, selector, index);
}
/**
* This functions is focused on helping to click elements by index
* @param {String} selector - string selector - CSS mandatory
* @param {Number} index - position of the locator if in array of elements
* @param {String} attribute - the element attribute of interest
*/
getElementAttrByIndex(selector, index = 0, attribute) {
const helper = this.helpers.WebDriver;
// const helper = this.helpers.Puppeteer; // this is the helper you want to use instead of WebDriver
return helper.executeScript(async (selector, index, attribute) => {
await document.querySelectorAll(selector)[index].getAttribute(attribute);
}, selector, index, attribute);
}
after this in your tests you should be able to just do this
await I.clickByIndex('#someSelector', 1); // 1 stands for the index as per implementation
await I.getElementAttrByIndex('#someSelector', 0, 'href'); // this returns the href attribute of the element
Made sure this works still and checked the code now, everything running correctly and as expected.
I can’t find the wdio-helper file anywhere. I installed the wdio CLI and also the helpers using npm, but still cant find the helper file. Can you point to the directory?