Puppeteer: examples audit

Created on 2 Aug 2017  路  3Comments  路  Source: puppeteer/puppeteer

  • [x] add example for saving a PDF
  • [x] add example: navigate to google, enter search query, click the submit button

    • maybe do something with results?

    • add pavel's

  • [x] screenshot

    • save screenshot to disk. done.

    • get and save fullpage screenshot

    • get and save emulated iphone screenshot

  • [x] loadurlwithoutcss

    • update so that it works.

  • [x] unrandomize

    • what is this example actually doing? delete?

  • [ ] pagecallback

    • needs comments to explain what the example is actually doing.

  • [x] detectsniff

    • at least the setTimeout should be done via an await.

    • would be nice to use a proxy instead of defineGetter, but that's lower priority

    • i'd recommend a function declaration for passing into evaluateOnNewDocument. we use that convention in lighthouse to make it more clear that its' a pageFunction. (colorWheel does this already :)

  • [x] waitFor example

Most helpful comment

For examples, we should start running them through the linter and clean up the code a bit so they're consistent and using es6. e.g. use const/let and a top-level async func to remove the mix of .then() and async/await. I think that's less common to see.

For example, this:

var browser = new Browser({headless: false});
browser.newPage().then(async page => {
    page.setRequestInterceptor(request => {
        if (request.url.endsWith('.css'))
            request.abort();
        else
            request.continue();
    });
    var success = await page.navigate(address);
    if (!success)
        console.log('Unable to load the address!');
    browser.close();

to maybe to something like this:

(async () => {

const browser = new Browser({headless: false});
const page = await browser.newPage();
page.setRequestInterceptor(request => {
  request.url.endsWith('.css') ?  request.abort() : request.continue();
});

const success = await page.navigate(address);
if (!success) {
  console.log('Unable to load the address!');
}
browser.close();

})();

All 3 comments

For examples, we should start running them through the linter and clean up the code a bit so they're consistent and using es6. e.g. use const/let and a top-level async func to remove the mix of .then() and async/await. I think that's less common to see.

For example, this:

var browser = new Browser({headless: false});
browser.newPage().then(async page => {
    page.setRequestInterceptor(request => {
        if (request.url.endsWith('.css'))
            request.abort();
        else
            request.continue();
    });
    var success = await page.navigate(address);
    if (!success)
        console.log('Unable to load the address!');
    browser.close();

to maybe to something like this:

(async () => {

const browser = new Browser({headless: false});
const page = await browser.newPage();
page.setRequestInterceptor(request => {
  request.url.endsWith('.css') ?  request.abort() : request.continue();
});

const success = await page.navigate(address);
if (!success) {
  console.log('Unable to load the address!');
}
browser.close();

})();

Do we want to keep the phantom examples? They feel out of place.

I can't come up with a simple yet illustrative example for page.addBinding (former inPage callback).

However, documentation has a few examples which might be good enough.
Closing this for now.

P.S. Feel free to PR an example with page.addBinding / share an idea for it if any!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kesava picture kesava  路  3Comments

aslushnikov picture aslushnikov  路  3Comments

ryanvincent29 picture ryanvincent29  路  3Comments

pyper picture pyper  路  3Comments

historylife picture historylife  路  3Comments