3

I just have the below Test.json file in the fixture folder:

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

The above file contains three different dataset.

I just have the below spec file and It contains one It (Test case) and It will run multiple times based on the above input.

Test.spec.js file:

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");
        });

    });

});

The above code is working as expected. But I just want to run the above single test parallelly by using different dataset.

Example: Three browser instance open and it should pick three different data from the Test.json file and run the single test which is available in the Test.spec.js file.

I just need logic to implement for one of my project, But I'm not able to share the code which is more complex that's reason just create some dummy test data and test script to achieve my logic.

Can someone please share yours thoughts to achieve this.

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • How about this https://docs.cypress.io/guides/guides/parallelization does it help? First, I'd use `forEach` around the `it`, the way you have it now, it really is just one test case, so I doubt it can run in parallel. – pavelsaman Oct 01 '21 at 14:04

1 Answers1

3

One way to run multiple instances of Cypress in parallel is via the Module API, which is basically using a Node script to start up the multiple instances.

Node script

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

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

Test

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);
    ...
  });
});

Awaiting results

cypress.run() returns a promise, so you can collate the results as follows

Videos and screenshots are troublesome, since it tries to save all under the same name, but you can specify a folder for each fixture

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}`));
});
Fody
  • 23,754
  • 3
  • 20
  • 37
  • Thank you. Let me check this out. – ArrchanaMohan Oct 01 '21 at 22:12
  • Actually, I'm not sure this is correct. Node is single-threaded, so maybe the runs are sequential. I'll have to run an experiment - may have to add in spawn or [run-parallel](https://www.npmjs.com/package/run-parallel) – Fody Oct 02 '21 at 00:57
  • 1
    The module API calls are asynchronous, so you get some parallel effect when each call releases the thread. I added a more rounded script based on my trial. – Fody Oct 02 '21 at 02:08
  • Great..! It works like charm...! Thanks much..! – ArrchanaMohan Oct 03 '21 at 10:12