我目前正在设置一个CI环境,以自动执行我们团队在测试工具中运行的e2e测试。我正在Gitlab上设置这个,目前使用的是Puppeteer。我有一个从我们的测试工具中触发的事件,它指定测试何时完成。现在我正在尝试“池化”执行,这样我就不会耗尽所有资源或耗尽侦听器。我决定尝试使用"puppeteer-cluster“来完成这个任务。我已经接近让事情正常工作了,但是我似乎不能让它在关闭浏览器之前等待页面上的事件。在使用puppeteer-cluster之前,我将一个回调传递给我的函数,当自定义事件被触发(通过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;
}
});发布于 2021-03-24 22:50:08
所以我做了一些工作,但对我来说真的很麻烦,我不确定这是不是正确的方法。因此,如果任何人有合适的方法或更推荐的方法,请毫不犹豫地回应。我在这里发帖,希望其他任何人都能处理类似的事情。我能够使用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);
}
});
});https://stackoverflow.com/questions/66770434
复制相似问题