I would like to add custom methods to the puppeteer.Page
object, so I could invoke them like so:
let page = await browser.newPage();
page.myNewCustomMethod();
Here is one out of many custom methods I have created. It finds first available element by the XPath expression, using the array of expressions:
const findAnyByXPath = async function (page: puppeteer.Page, expressions: string[]) {
for (const exp of expressions) {
const elements = await page.$x(exp);
if (elements.length) {
return elements[0];
}
}
return null;
}
I have to invoke it like so...
let element = await findAnyByXPath(page, arrayOfExpressions);
To me, that looks weird in the editor, especially in a region where many custom methods are being invoked. It looks to me, a bit of "out of context". So I would rather invoke it like that:
page.findAnyByXPath(arrayOfExpressions);
I'm aware that there is a page.exposeFunction
method, but it is not what I'm looking for.
What is a way to achieve this?