I was given a test code, which I am not allowed to change, but only implementing it using Puppeteer (and other libraries if necessary).
This is the code I received:
// this code is given And can't be changed!
assertOnLeft(function (asserter) {
asserter(window.location.href.indexOf("XXX:3000/regression_test_pages/links_page.html") == 0,
"Address should be links_page.html");
});
I tried to implement it this way:
const assert = require("assert");
const puppeteer = require("puppeteer");
const assertOnLeft = async (predicate) => {
const asserterOk = assert.ok;
await page.exposeFunction("predicate", predicate);
await page.evaluate((asserterOk) => { // creates Error: Evaluation failed: ReferenceError: window is not defined
return predicate(asserterOk)
}, asserterOk).catch((e) => console.log("error", e));
}
but I get an error:
error Error: Evaluation failed: ReferenceError: window is not defined
I tried to pass the function directly as recommended here
return await page.evaluate(predicate(assert.ok))
or
var runnable = predicate(assert.ok); return await page.evaluate(runnable);
But still get the same error, probably because I am sending a function that cal's a function, as a parameter to page.evaluate
Thanks