如何与puppeteer启用并行测试?

时间:2017-10-06 02:14:57

标签: puppeteer

我正在使用chrome puppeteer库直接运行浏览器集成测试。我现在在单个文件中编写了一些测试。有没有办法并行运行它们?实现这一目标的最佳方法是什么?

4 个答案:

答案 0 :(得分:0)

// My tests contain about 30 pages I want to test in parallel
const aBunchOfUrls = [
  {
    desc: 'Name of test #1',
    url: SOME_URL,
  },
  {
    desc: 'Name of test #2',
    url: ANOTHER_URL,
  },
  // ... snip ...
];

const browserPromise = puppeteer.launch();

// These test pass! And rather quickly. Slowest link is the backend server.
// They're running concurrently, generating a new page within the same browser instance
describe('Generate about 20 parallel page tests', () => {
  aBunchOfUrls.forEach((testObj, idx) => {
    it.concurrent(testObj.desc, async () => {
      const browser = await browserPromise;
      const page = await browser.newPage();

      await page.goto(testObj.url, { waitUntil: 'networkidle' });
      await page.waitForSelector('#content');

      // assert things..
    });
  });
});
来自https://github.com/GoogleChrome/puppeteer/issues/474

https://github.com/quicksnap

撰写

答案 1 :(得分:0)

要并行运行人偶实例,您可以查看我编写的该库:puppeteer-cluster

它有助于在多个浏览器,上下文或页面中并行运行不同的伪造者任务,并解决错误和浏览器崩溃的问题。这是一个最小的示例:

const { Cluster } = require('puppeteer-cluster');

(async () => {
  const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_CONTEXT, // use one browser per worker
    maxConcurrency: 4, // cluster with four workers
  });

  // Define a task to be executed for your data
  cluster.task(async ({ page, data: url }) => {
    await page.goto(url);
    const screen = await page.screenshot();
    // ...
  });

  // Queue URLs
  cluster.queue('http://www.google.com/');
  cluster.queue('http://www.wikipedia.org/');
  // ...

  // Wait for cluster to idle and close it
  await cluster.idle();
  await cluster.close();
})();

您还可以像这样直接将函数排队:

  const cluster = await Cluster.launch(...);

  cluster.queue(async ({ page }) => {
    await page.goto('http://www.wikipedia.org');
    await page.screenshot({path: 'wikipedia.png'});
  });

  cluster.queue(async ({ page }) => {
    await page.goto('https://www.google.com/');
    const pageTitle = await page.evaluate(() => document.title);
    // ...
  });

  cluster.queue(async ({ page }) => {
    await page.goto('https://www.example.com/');
    // ...
  });

答案 2 :(得分:0)

我实现这一目标的方法是,像已经完成的那样,在单个文件中创建测试套件。然后创建一个testSuiteRunner.js文件(或任何您想调用的文件)并按如下所示进行设置:

require('path/to/test/suite/1);
require('path/to/test/suite/2);
require('path/to/test/suite/3);
...

像上面一样,使用require语句导入所有套件(无需给它们提供const变量名或类似名称),然后可以使用node ./path/to/testSuiteRunner.js并行执行所有套件。我可以为此提出一个最简单的解决方案!

答案 3 :(得分:0)

我认为最好的主意是使用像Jest这样的测试运行程序可以为您解决这个问题。至少我是这样的。请记住,如果您同时运行过多的Chrome实例,则计算机可能会崩溃,因此可以将并行测试的数量限制为2个左右。

不幸的是,官方文档中没有清楚地说明Jest如何并行测试。您可能会发现此https://github.com/facebook/jest/issues/6957有用。

x <- seq(0, 1, 0.01) ns <- c(0.1,0.11,0.13,0.15,0.17,0.2,0.25,0.33,0.5,1,2,3,4,5,6,7,8,9,10) plot(x, x, type = "l") for (n in ns){ lines(x, x^n) } 这样的库很棒,但是请记住,首先要并行进行测试,而不是伪造者的任务。

相关问题