CodeceptJS - unable to run any window. or document. line

Hi,
I have tried thousands of different things and I’m always receiving below error message.

message: “document is not defined”
stack: "ReferenceError: document is not defined↵

  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:

What does everyone do to use document.XXXXXX?

Thank you,

Please, paste more info in your question:
where do you run this code? is it helper or test ? which helper, driver and browser do you use?

where does it fails? runtime? or Typescript compile-time?

do you use session or run code in iframe?

I seen something similar to your code.
It was TS-linter or TS error, cause TS was not know that context is the browser. It solved by //@ts-ignore

Hi, thank you for guiding me with your questions

Visual Studio Code
image

SCENARIO is under F-ActivitiesActivityDetails_test.js, and it unique line is calling ExpandCollapse method from ButtonLink.

///
///

Feature(‘Feature:’);

//Scenarios

//Activity Options|Collapse buttons

BeforeSuite((I) => {
});

Before((I) => {

I.amOnPage(‘any web’);

I.waitForNavigation;

});

Scenario(‘Scenario: Activity Options|Collapse buttons’, (I,ButtonLink) => {

ButtonLink.ExpandCollapse(’.ams-activities-details-panel__icon’);

});

After((I) => {

pause();

});

AfterSuite((I) => {

});

ButtonLink.js complete file data:

///

const I = actor();

module.exports = {
// insert your locators and methods here
//Methods
ButtonLinkValidation(_locator,_currenturlvalidation){
I.click(_locator);
I.waitForNavigation;
I.seeInCurrentUrl(_currenturlvalidation);
},
ButtonLinkValidationNewTab(_locator,_currenturlvalidation){
I.click(_locator);
I.wait(5);
I.switchTo();
I.waitForNavigation;
I.seeInCurrentUrl(_currenturlvalidation);
I.closeCurrentTab();
},
ExpandCollapse(_locator){
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”});
}
}
}

codecept.json

{
“tests”: “./*_test.js”,
“timeout”: 10000,
“output”: “./output”,
“helpers”: {
“Puppeteer”: {
“url”: “http://localhost”,
“waitForNavigation”: [ “domcontentloaded”, “networkidle0” ],
“show”: true,
“windowSize”: “1366x768”,
“WebDriver” : {
“url”: “http://localhost”,
“browser”: “chrome”
}
}
},
“include”: {
“I”: “./steps_file.js”,
“SignInOut”: “./PageObject/SignInOut.js”,
“ButtonLink”: “./PageObject/ButtonLink.js”
},

“plugins”: {
“allure”: {},
“stepByStepReport”: {
“enabled”: true,
“screenshotsForAllureReport”: true
}
},

“gherkin”: {
“features”: “./features/*.feature”,
“steps”: [
“./step_definitions/steps.js”
]
},
“multiple”: {
“basic”: {
“browsers”: [“chrome”, “firefox”]
}
},
“bootstrap”: false,
“mocha”: {},
“name”: “wyn_auto”
}

I’m using puppeteer and currently using this command line:

npx --node-arg=–inspect-brk codeceptjs run F-ActivitiesActivityDetails_test.js --debug

How can I provide this?

where does it fails? runtime? or Typescript compile-time?
do you use session or run code in iframe?

Thank you

Not worked with Puppeteer.
But I see 2 possible problems

  1. There are no access to “http://localhost/any%20web
  2. 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

Thank you, I have tried all suggestions and still document is not defined