Skip to content

Runtime.evaluate can happen before Page.addScriptToEvaluateOnNewDocument #182

Closed

Description

@TrevorSundberg

The issue is that you may have successfully completed Page.addScriptToEvaluateOnNewDocument, however you can still occasionally call Runtime.evaluate or Runtime.callFunctionOn when a page is loading and it will evaluate before the new document script.

This behavior seems like a race, as I was only able to repro it with a contrived situation where I purposefully do not await page.goto (however it happens in our production code). This script picks between two urls that may have iframes and loads them randomly. I've reproduced this with puppeteer, however I believe it is unlikely a puppeteer bug:


const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.evaluateOnNewDocument(() => {
    window.shouldAlwaysBeHere = 'GOOD'
  })
  await page.goto('about:blank');
  const urls = [
    'https://www.w3schools.com/html/tryit.asp?filename=tryhtml_default',
    'https://www.youtube.com/watch?v=HagVnWAeGcM',
  ]
  setInterval(() => {
    if (Math.random() < 0.05) {
      const url = urls[Math.floor(Math.random() * urls.length)];
      page.goto(url).catch(() => {})
    }
    for (const frame of page.frames()) {
      frame.evaluate(() => window.shouldAlwaysBeHere || 'BAD').then(value => {
        console.log(value)
        if (value === 'BAD') {
          process.exit(1)
        }
      }, () => {})
    }
  }, 20)
})()

Expected:

...
GOOD
GOOD
GOOD
...

Actual:

...
GOOD
GOOD
BAD

Activity

  1. patrickhulce commented on Jul 30, 2019

    @patrickhulce

    Isn't this expected @TrevorSundberg? The solution here is to await page.goto before evaluating whatever it is you need to evaluate at which point you're certain that Page. addScriptToEvaluateOnNewDocument has been evaluated.

  2. TimvdLippe commented on Apr 9, 2020

    @TimvdLippe
    Contributor

    This repository is related to Chrome DevTools Protocol, but does not track issues regarding its definition or implementation. If you want to file an issue for the Chrome DevTools Protocol, please open an issue on https://crbug.com under component: Platform>DevTools>Platform. Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions