首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取puppeteer时出现问题-群集在关闭前等待页面事件

获取puppeteer时出现问题-群集在关闭前等待页面事件
EN

Stack Overflow用户
提问于 2021-03-24 03:58:22
回答 1查看 151关注 0票数 0

我目前正在设置一个CI环境,以自动执行我们团队在测试工具中运行的e2e测试。我正在Gitlab上设置这个,目前使用的是Puppeteer。我有一个从我们的测试工具中触发的事件,它指定测试何时完成。现在我正在尝试“池化”执行,这样我就不会耗尽所有资源或耗尽侦听器。我决定尝试使用"puppeteer-cluster“来完成这个任务。我已经接近让事情正常工作了,但是我似乎不能让它在关闭浏览器之前等待页面上的事件。在使用puppeteer-cluster之前,我将一个回调传递给我的函数,当自定义事件被触发(通过exposeFunction注入)时,我将开始调用它。该回调函数现在正在传递数据,因此不需要等待。我似乎找不到一种让行刑等待的方法,希望有人能在这里有个主意。如果有人有什么建议,我很乐意听听。

代码语言:javascript
复制
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;
    }
});
EN

回答 1

Stack Overflow用户

发布于 2021-03-24 22:50:08

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

代码语言:javascript
复制
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);
         }
                    
     });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66770434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档