vax*_*xul 8 javascript testing google-chrome node.js puppeteer
我尝试从多个页面中截取屏幕截图,这些页面应完全加载(包括延迟加载的图像),以便以后进行比较。
我发现了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);
}
};
scroll();
});
await page.waitFor(5000);
await page.screenshot({
path: `screenshots/master/${fileName}.png`,
fullPage: true,
});
await page.close();
console.log(`Viewport "${viewport.name}", Route "${route}"`);
});
}
Run Code Online (Sandbox Code Playgroud)
问题:即使page.waitFor()
(timeout)的值较高,有时页面上所有与前端相关的JavaScript也不完全执行。
对于某些较旧的页面,其中某些JavaScript可能会更改前端。铁在一种遗留情况下jQuery.matchHeight
。
最好的情况:在理想情况下, Puppeteer会等到所有JavaScript都被评估并执行后再进行。这样的事情可能吗?
编辑
我可以在的帮助下稍微改善脚本cody-g
。
function jQueryMatchHeightIsProcessed() {
return Array.from($('.match-height')).every((element) => {
return element.style.height !== '';
});
}
// Within takeScreenshot() after page.waitFor()
await page.waitForFunction(jQueryMatchHeightIsProcessed, {timeout: 0});
Run Code Online (Sandbox Code Playgroud)
...但这远非完美。看来我必须为不同的前端脚本找到类似的解决方案,才能真正考虑目标页面上发生的所有事情。
jQuery.matchHeight
在我的情况下,主要的问题是它在不同的运行中会处理不同的高度。可能是由于图像延迟加载引起的。看来我必须等到可以用Flexbox替换它。(^ _ ^)°
其他要解决的问题:
禁用动画:
await page.addStyleTag({
content: `
* {
transition: none !important;
animation: none !important;
}
`,
});
Run Code Online (Sandbox Code Playgroud)
处理幻灯片:
function handleSwiperSlideshows() {
Array.from($('.swiper-container')).forEach((element) => {
if (typeof element.swiper !== 'undefined') {
if (element.swiper.autoplaying) {
element.swiper.stopAutoplay();
element.swiper.slideTo(0);
}
}
});
}
// Within takeScreenshot() after page.waitFor()
await page.evaluate(handleSwiperSlideshows);
Run Code Online (Sandbox Code Playgroud)
但是还不够。我认为对这些旧页面进行视觉测试是不可能的。
下面的waitForFunction可能对您有用,您可以使用它来等待任何任意函数求值为true。如果您有权访问页面的代码,则可以设置窗口状态,并使用该状态通知人偶,可以安全地继续操作,或者仅依靠某种其他就绪状态即可。 注意:此函数是一个轮询函数,它会按指定的间隔重新评估。
const watchDog = page.waitForFunction('<your function to evaluate to true>');
Run Code Online (Sandbox Code Playgroud)
例如,
const watchDog = page.waitForFunction('window.status === "ready"');
await watchDog;
Run Code Online (Sandbox Code Playgroud)
在页面代码中,您只需将设置window.status
为ready
要在多个异步文件中利用多个看门狗,您可以执行以下操作
index.js
...import/require file1.js;
...import/require file2.js;
...code...
Run Code Online (Sandbox Code Playgroud)
file1.js:
var file1Flag=false; // global
...code...
file1Flag=true;
Run Code Online (Sandbox Code Playgroud)
file2.js:
var file2Flag=false; // global
...code...
file2Flag=true;
Run Code Online (Sandbox Code Playgroud)
main.js:
const watchDog = page.waitForFunction('file1Flag && file2Flag');
await watchDog;
Run Code Online (Sandbox Code Playgroud)