Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] - Screenshot on lazyload sites #1872

Closed
Tymotey opened this issue Apr 20, 2020 · 2 comments
Closed

[Question] - Screenshot on lazyload sites #1872

Tymotey opened this issue Apr 20, 2020 · 2 comments

Comments

@Tymotey
Copy link

Tymotey commented Apr 20, 2020

I have been trying to make a screenshot for a link that has lazyload images.
This is what I have so far:

const page = await context.newPage()
await page.goto(site, {waitUntil: 'domcontentloaded'})
var resources_before = 0
const watchComplete = page.waitForFunction((resources_before) => {
  let body = document.body
  let html = document.scrollingElement || document.documentElement
  let viewportHeight = window.innerHeight
  let documentHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight)
  const getScrollState = async () => {
    return await new Promise(resolve => {
      setTimeout(function () {
        html.scrollTop = html.scrollTop + viewportHeight
        if (html.scrollTop >= (documentHeight - viewportHeight)) {
          resolve('scrolled_bottom')
        } else {
          resolve('not_scrolled_bottom')
        }
      }, 500)
    }).then((val) => {
      window.scroll_state = val;
    })
  };
  getScrollState()
  if ('scrolled_bottom' === window.scroll_state) {
    return_val = true
  } else {
    return_val = false
  }
  return return_val
}, resources_before, {
  polling: 1000
})
await watchComplete
await page.evaluate(() => {
  window.scrollTo(0, 0);
})
await page.screenshot({
    type: 'jpeg',
    fullPage: true,
    waitUntil: 'networkidle0'
})

I want to get rid of the hardcoded times from code and make the screenshot fire at the moment all resources are loaded. Any ideeas?

resources_before - I wanted to compare the nr of resources after scroll with the number before and wait for load.

Thank you!

@celeryclub
Copy link
Contributor

I think your best bet here is to have some code in your app that sets a state variable, and then use waitForFunction to check that variable in your test code.

App code

// check if images are loaded, then run this
window.allImagesLoaded = true;

Test code

await page.waitForFunction(() => {
  return window.allImagesLoaded === true;
});
await page.screenshot({...});

@pavelfeldman
Copy link
Member

This advice looks great. Closing for now, please feel free to reopn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants