LucianoGanga / simple-headless-chrome

Simple abstraction to use Chrome as a Headless Browser with Node JS
MIT License
217 stars 50 forks source link

waitForFrameToLoad not working as expected #50

Open mmarsella opened 7 years ago

mmarsella commented 7 years ago

I am trying to inject an iframe into a new tab and need to wait for it to finish loading before proceeding. In the example below I am trying to use waitForFrameToLoad:

const baseURL = "about:blank";
// Open up a new tab
  let mainTab = await browser.newTab();

  //inject iframe into this tab - (iframetester creates an Iframe and injects it into the DOM
  const iframe = await mainTab.evaluate(iframeTester, "http://www.exampleURL.com"); 

  await mainTab.waitForFrameToLoad(baseURL, 5000); // Should be waiting for iframe to load for this

The goal is to inject an iframe and wait until it has fully loaded to take next actions. The mainTab.waitForFrameToLoad doesn't throw an error, but doesn't seem to be waiting - ideally I would want to be listening for any changes in the DOM (as well as if the src inside the iframe busts out/etc...)- Is there support for page listeners such as Page.loadEventFired as well as the other Page domain listeners? How would I be able to implement these listeners using your library?

Thanks so much.

M

LucianoGanga commented 7 years ago

Hello @mmarsella !

I think that this case won't work with waitForFrameToLoad, because that method uses the function above, which tries to seek the frames in the resource tree of the page:

/**
 * Get the list of frames in the loaded page
 * @return {object} - List of frames, with childFrames
 */
exports.getFrames = async function () {
  debug(`:: getFrames => Getting frames list`)
  browserIsInitialized.call(this)
  const frames = []
  const resourceTree = await this.client.Page.getResourceTree()
  frames.push(resourceTree.frameTree.frame)
  _.each(resourceTree.frameTree.childFrames, (frameObj) => {
    frames.push(frameObj.frame)
  })
  return frames
}

What I mean with this is that I'm not sure if you can dynamically add a frame and hope for the getFrames method to find it in the resourceTree.

I'll be glad to test your case if you can help me, just give me the iframeTester fn that you're using so we can create a test case for this and debug it.

Thanks! Lucho

mmarsella commented 7 years ago

Ok, will mock up an example and send it over when I get some free time later.

Thanks Lucho.

M