【问题标题】:How to run the single test with different data set in parallel by using cypress on single machine如何在单机上使用 cypress 并行运行具有不同数据集的单个测试
【发布时间】:2021-11-23 04:08:10
【问题描述】:

我在夹具文件夹中只有以下 Test.json 文件:

[
    {
        "searchKeyword":"cypress"
    },
    {
        "searchKeyword":"QA automation"
    },
    {
        "searchKeyword":"stackoverflow"
    }
]

上述文件包含三个不同的数据集。

我只有下面的规范文件,它包含一个它(测试用例),它将根据上面的输入运行多次。

Test.spec.js 文件:

describe("Run the test parallel based on the input data",() =>{

    const baseUrl = "https://www.google.com/";

    before("Login to consumer account", () => {
        
        cy.fixture('Test').then(function (data) {
            this.data = data;
          })
    });

    it("Search the keyword", function () {
        this.data.forEach((testData) =>{
            cy.visit(baseUrl);
            cy.xpath("//input[@name='q']").type(testData.searchKeyword);
            cy.xpath("//input[@value='Google Search']").click();
            cy.get("//ul/li[2]").should("be.visible");
        });

    });

});

上面的代码按预期工作。但我只想通过使用不同的数据集并行运行上述单个测试。

示例:三个浏览器实例打开,它应该从 Test.json 文件中选择三个不同的数据并运行 Test.spec.js 文件中可用的单个测试。

我只需要为我的一个项目实现逻辑,但我无法共享更复杂的代码,因此只需创建一些虚拟测试数据和测试脚本来实现我的逻辑。

有人可以分享您实现这一目标的想法吗?

【问题讨论】:

  • 这个docs.cypress.io/guides/guides/parallelization 有帮助吗?首先,我会在it 周围使用forEach,就像你现在拥有的那样,它实际上只是一个测试用例,所以我怀疑它是否可以并行运行。

标签: parallel-processing cypress parallel.foreach cypress-component-test-runner


【解决方案1】:

并行运行多个 Cypress 实例的一种方法是通过 Module API,它基本上是使用 Node 脚本来启动多个实例。

节点脚本

// run-parallel.js
const cypress = require('cypress')
const fixtures = require('./cypress/fixtures/Test.json')

fixture.forEach(fixture => {
  cypress.run({
    env: {
      fixture
    },
  })
})

测试

describe("Run the test for given env data",() =>{

  const testData = Cypress.env('fixture')
  ...

  it("Search the keyword", function () {
    cy.visit(baseUrl);
    cy.xpath("//input[@name='q']").type(testData.searchKeyword);
    ...
  });
});

等待结果

cypress.run()返回一个promise,所以可以整理结果如下

视频和截图很麻烦,因为它试图以相同的名称保存所有,但您可以为每个灯具指定一个文件夹

const promises = fixtures.map(fixture => {
  return cypress.run({
    config: {
      video: true,
      videosFolder: `cypress/videos/${fixture.searchKeyword}`,
      screenshotsFolder: `cypress/screenshots/${fixture.searchKeyword}`,
    },
    env: {
      fixture
    },
    spec: './cypress/integration/dummy.spec.js',
  })
})

Promise.all(promises).then((results) => {
  console.log(results.map(result => `${result.config.env.fixture.searchKeyword}: ${result.status}`));
});

【讨论】:

  • 谢谢。让我看看这个。
  • 实际上,我不确定这是否正确。节点是单线程的,所以运行可能是顺序的。我必须进行实验 - 可能需要添加 spawn 或 run-parallel
  • 模块API调用是异步的,所以每次调用释放线程时都会得到一些并行效果。我根据我的试验添加了一个更圆润的脚本。
  • 太棒了..!它的作用就像魅力......!非常感谢..!
猜你喜欢
  • 2021-04-21
  • 2023-01-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
相关资源
最近更新 更多