在关闭之前,让puppeteer集群等待页面事件时出现问题



我目前正在设置一个CI环境,以自动化我们团队在测试线束中运行的e2e测试。我正在Gitlab上设置这个,目前正在使用Puppeter。我有一个事件从我们的测试线束中触发,指定测试何时完成。现在我正试图";池";执行,这样我就不会耗尽所有资源或耗尽侦听器。我决定试试";木偶集群";用于此任务。我已经接近正常工作了,但我似乎无法在关闭浏览器之前等待页面上的事件。在使用puppeteer集群之前,我向函数传递了一个回调,当自定义事件被触发(通过exposeFunction注入(时,我会继续调用它。该回调函数现在正在数据中传递,因此没有等待。我似乎找不到让处决等待的方法,希望有人能在这里有个主意。如果有人有什么建议,我很乐意听取。

test('Should launch the browser and run e2e tests', async (done) => {
try {
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 10,
monitor: false,
timeout: 1200000,
puppeteerOptions: browserConfig
});

// Print errors to console
cluster.on("taskerror", (err, data) => {
console.log(`Error crawling ${data}: ${err.message}`);
});

//Setup our task to be run
await cluster.task( async ({page, data: {testUrl, isLastIndex, cb}, worker}) => {
console.log(`Test starting at url: ${testUrl} - isLastIndex: ${isLastIndex}`);

await page.goto(testUrl);
await page.waitForSelector('#testHarness');
await page.exposeFunction('onCustomEvent', async (e) => {         
if (isLastIndex === true){ ; 
//Make a call to our callback, finalizing tests are complete
cb();
}
console.log(`Completed test at url: ${testUrl}`);
});

await page.evaluate(() => {
document.addEventListener('TEST_COMPLETE', (e) => {
window.onCustomEvent('TEST_COMPLETE');   
console.log("TEST COMPLETE");         
});
});
});
//Perform the assignment of all of our xml tests to an array
let arrOfTests = await buildTestArray();
const arrOfTestsLen = arrOfTests.length;
for( let i=0; i < arrOfTestsLen; ++i){
//push our tests on task queue
await cluster.queue( {testUrl: arrOfTests[i], isLastIndex: (i === arrOfTestsLen - 1), cb: done });
};
await cluster.idle();
await cluster.close();

} catch (error) {
console.log('ERROR:',error);
done();
throw error;
}
});

所以我做了一些工作,但这对我来说真的很棘手,我真的不确定这是正确的方法。因此,如果有人有正确的方法或更推荐的方法,请毫不犹豫地做出回应。我在这里发帖,希望其他人也能处理类似的事情。我能够用bool和setInterval来实现这一点。我已将工作结果粘贴在下面。

await cluster.task( async ({page, data: {testUrl, isLastIndex, cb}, worker}) => {

let complete = false;
console.log(`Test starting at url: ${testUrl} - isLastIndex: ${isLastIndex}`);

await page.goto(testUrl)       
await page.waitForSelector('#testHarness');
await page.focus('#testHarness');

await page.exposeFunction('onCustomEvent', async (e) => {   
console.log("Custom event fired");      
if (isLastIndex === true){ ; 
//Make a call to our callback, finalizing tests are complete
cb();
complete = true;
//console.log(`VAL IS ${complete}`);
}
console.log(`Completed test at url: ${testUrl}`);

});
//This will run on the actual page itself. So setup an event listener for
//the TEST_COMPLETE event sent from the test harness itself
await page.evaluate(() => {
document.addEventListener('TEST_COMPLETE', (e) => {
window.onCustomEvent('TEST_COMPLETE');                                       
});
});

await new Promise(resolve => {
try {
let timerId = setInterval(()=>{
if (complete === true){
resolve();
clearInterval(timerId);
}
}, 1000);
} catch (e) {
console.log('ERROR ', e);
}

});
});

最新更新