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
Wait for lazy loaded elements when using the fullPage
option
#40
Changes from 9 commits
d81b43a
d4000ad
d61dc4c
5dec125
8662efa
a940db1
c165d3d
4b80210
16eec30
990ad56
e0ebd01
5f1aa86
ebb5c1c
7ae2147
46ee934
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "capture-website", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't bump this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dunno why that'd get bumped. Gotcha. |
||
"description": "Capture screenshots of websites", | ||
"license": "MIT", | ||
"repository": "sindresorhus/capture-website", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,48 @@ test('`fullPage` option', async t => { | |
t.true(size.height > 100); | ||
}); | ||
|
||
test('`fullPage` option - lazyloading', async t => { | ||
const server = await createTestServer(); | ||
|
||
server.get('/', async (request, response) => { | ||
response.end(` | ||
<body> | ||
<div style="display: grid; grid-template-columns: repeat( auto-fill, minmax(150px, 1fr) );" id="grid"></div> | ||
<script> | ||
const numItemsToGenerate = 250; //how many gallery items you want on the screen | ||
const numImagesAvailable = 1100; //how many total images are in the collection you are pulling from | ||
const imageWidth = 150; //your desired image width in pixels | ||
const imageHeight = 150; //desired image height in pixels | ||
const collectionID = 9717149; //the collection ID from the original url | ||
function renderGalleryItem(randomNumber){ | ||
fetch('https://source.unsplash.com/collection/'+collectionID+'/'+imageWidth+'x'+imageHeight+'/?sig='+randomNumber) | ||
.then((response)=> { | ||
let galleryItem = document.createElement('div'); | ||
galleryItem.classList.add('gallery-item'); | ||
galleryItem.innerHTML = '<img class="gallery-image" src="'+response.url+'" alt="gallery image" loading="lazy" />'; | ||
document.getElementById('grid').appendChild(galleryItem); | ||
}) | ||
} | ||
for(let i=0;i<numItemsToGenerate;i++){ | ||
let randomImageIndex = Math.floor(Math.random() * numImagesAvailable); | ||
renderGalleryItem(randomImageIndex); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you clean up the code style here? It's a bit messy. Use async/await, template literals, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also move the string to the top-level. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Will learn a few things here and there and proceed as such. Thanks for the feedback |
||
</script> | ||
</body> | ||
`); | ||
}); | ||
|
||
const size = imageSize(await instance(server.url, { | ||
width: 200, | ||
height: 300, | ||
scaleFactor: 1, | ||
fullPage: true | ||
})); | ||
|
||
t.is(size.width, 200); | ||
t.true(size.height > 150); | ||
}); | ||
|
||
test('`timeout` option', async t => { | ||
const server = await createTestServer(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the user would expect it to take up to one minute. If we keep this, it needs to be documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might also be added as an argument with default value set to timeout 60. Might want to comment on this or implement some other better functionality? Maybe it's also worth looking into alternate functions, such as the
waitForNavigation
Vasile mentioned earlier.In case it might be useful for documentation purposes; I retrieved part of the solution from https://stackoverflow.com/questions/51651830/how-to-wait-for-all-images-to-load-from-page-evaluate-function-in-puppeteer-when
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would look into something better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using
page.waitForNavigation
as per pointed by #40 (review)