我尝试从多个页面中截取屏幕截图,这些页面应完全加载(包括延迟加载的图像),以便以后进行比较。
我发现了lazyimages_without_scroll_events.js示例,该示例很有帮助。
使用以下代码,屏幕快照看起来不错,但是存在一些主要问题。
async function takeScreenshot(browser, viewport, route) {
return browser.newPage().then(async (page) => {
const fileName = `${viewport.directory}/${getFilename(route)}`;
await page.setViewport({
width: viewport.width,
height: 500,
});
await page.goto(
`${config.server.master}${route}.html`,
{
waitUntil: 'networkidle0',
}
);
await page.evaluate(() => {
/* global document,requestAnimationFrame */
let lastScrollTop = document.scrollingElement.scrollTop;
// Scroll to bottom of page until we can't scroll anymore.
const scroll = () => {
document.scrollingElement.scrollTop += 100;
if (document.scrollingElement.scrollTop !== lastScrollTop) {
lastScrollTop = document.scrollingElement.scrollTop;
requestAnimationFrame(scroll);
}
}; …Run Code Online (Sandbox Code Playgroud)