2

Previously using Nightwatch.js I was able to create custom Nightwatch commands: https://github.com/nightwatchjs/nightwatch-docs/blob/master/guide/extending-nightwatch/custom-commands.md

I'm wondering if there is anything that exists like this for Puppeteer-- the closest thing I've seen is: Is there a way to add script to add new functions in evaluate() context of chrome+puppeeter?

But it's still far away from what I want. I would like to be able to call page.commonAction(...) instead of

page.x();
page.y();
page.z();
svey
  • 115
  • 1
  • 7

1 Answers1

1

You can always create your own script with functions you can call. For example, I have a myFunctions.js in the same folder, from which I name

const mf = require('./myFunctions.js');

Inside the myFunctions.js I have, for example, this function:

async function verifyElementPresent(page, selector) {
  let verifySelector = await page.$(selector);
  if (verifySelector !== null) {
    console.log(found('> OK - Element present'));
  } else {
    console.log(notfound('>>> ERROR - Element not present: ' + selector));
  }
}

So now, in my Puppeteer script all I have to do is write:

mf.verifyElementPresent(page, '#headerTitle');

And it'll print in console the result.

Hope this helps.

Nicolás A.
  • 523
  • 4
  • 10