[英]Scrape headlines with Playwright, how to write output into Markdown file (Console.log is working)
[英]How to console.log in playwright
我想记录playwright 测试用例中的一个变量,但无法在开发人员工具控制台中加载日志,因为我正在使用 page.on() function
test('largest contentful paint', async ({ page }) => {
await page.goto('http://localhost:3000/', { waitUntil: 'networkidle' });
const largestContentfulPaint = await page.evaluate(() => {
return new Promise((resolve) => {
new PerformanceObserver((l) => {
const entries = l.getEntries();
// the last entry is the largest contentful paint
const largestPaintEntry = entries.at(-1);
page.on('console', () => {
console.log('largestPaintEntry', largestPaintEntry);
});
// resolve(largestPaintEntry.startTime);
}).observe({
type: 'largest-contentful-paint',
buffered: true,
});
});
});
await expect(largestContentfulPaint).toBeLessThan(2500);
});
如评论中所述,问题是您必须在page.evaluate()
回调之外附加page.on
事件处理程序。
// @ts-check
const { test, expect } = require('@playwright/test');
test('largest contentful paint', async ({ page }) => {
await page.goto('https://www.stefanjudis.com/', { waitUntil: 'networkidle' });
page.on('console', (msg) => {
console.log(msg);
});
const largestContentfulPaint = await page.evaluate(() => {
return new Promise((resolve) => {
new PerformanceObserver((l) => {
const entries = l.getEntries();
// the last entry is the largest contentful paint
const largestPaintEntry = entries.at(-1);
console.log(largestPaintEntry.startTime)
}).observe({
type: 'largest-contentful-paint',
buffered: true,
});
});
});
await expect(largestContentfulPaint).toBeLessThan(2500);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.