通过与微软新的无头浏览器剧作家一起玩,我构建了既不返回错误也不返回其他东西的东西。
此时,我的想法已经结束,我请你给我一些提示,以指出我的失败。
这段代码应该只是启动多个无头浏览器组异步。但是浏览器的启动挂起,应用程序处于无限循环中。我在这里粘贴代码,它是一个简单的 nodejs 脚本,用于重现行为。
感谢您的帮助和阅读;)
const playwright = require('playwright');
log('start playwright async');
let maxRunners = 1;
let running = 0;
let list = [1,2,3,4,5,6,7,8,9,0,11,12,13,14,15];
log('start job');
while (list.length > 0) {
if (running < maxRunners) {
log('runner started');
running++;
let entry = list[0];
list.shift();
log('start browser loop');
for (const browserType of ['chromium', 'firefox', 'webkit']) {
log('fire async');
(async () => {
log('loop next');
log('launch: ', browserType);
const browser = await playwright[browserType].launch({
headless: false
});
log(browserType, ' launched');
const context = await browser.newContext();
log('open new page');
const page = await context.newPage('http://whatsmyuseragent.org/');
log('page opened');
log('make screenshot');
await page.screenshot({path: `example-${browserType}.png`});
log('screenshot made');
log('close browser');
await browser.close();
log('browser closed');
log('loop succeed');
running--;
})();
log('end async');
}
log('end loop');
if (running === 0 && list.length === 0) {
log('job finished');
}
}
}
log('end playwright script');
function log(...msgs) {
let date = new Date();
let timeString = date.toISOString().substr(11, 8);
let msg = '';
for (let i in msgs) {
msg += msgs[i];
}
console.log(timeString, ':', msg);
}
输出:
20:53:29 : start playwright async
20:53:29 : start job
20:53:29 : runner started
20:53:29 : start browser loop
20:53:29 : fire async
20:53:29 : loop next
20:53:29 : launch: chromium
20:53:29 : end async
20:53:29 : fire async
20:53:29 : loop next
20:53:29 : launch: firefox
20:53:29 : end async
20:53:29 : fire async
20:53:29 : loop next
20:53:29 : launch: webkit
20:53:29 : end async
20:53:29 : end loop