ArrchanaMohan
ArrchanaMohan

Reputation: 2566

How to map different JSON objects from the fixture into specific spec test file in the cypress

I have the below Input.json as fixture and It contains two different test cases.

Input.json (Fixture folder)

[
    {
        "searchKeyword":"cypress"
    },
    {
        "username":"QATesting",
        "password":"testprofile"
    }
]

The above data will validate two different functionality of Google. One is going to validate search engine and another one is going to validate the user login activity (This is just for sample use case which may imitate my actual requirement).

I just created the cypress runner and I just want to run the spec file by using the below runner.js file

const cypress = require('cypress')
const fixtures = require('./cypress/fixtures/Test.json')

const promises = fixtures.map(fixture => {
  return cypress.run({
    env: {
      fixture
    },
    spec: './cypress/integration/test.spec.js',
  });
});

I just added two different It(test cases) respectively in the below "test.spec.js" file. And one test is gonna do the search function and another one is gonna check the existing user login activity:

describe("How to map two different data set with respective test function",() =>{

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

    const testData = Cypress.env('fixture')

    beforeEach("",()=>{

        cy.visit(baseUrl);
    });

    it("Test Case1: Search the keyword", function () {
            cy.xpath("//input[@name='q']").type(testData.searchKeyword);
            cy.xpath("//input[@value='Google Search']").click();
            cy.get("//ul/li[2]").should("be.visible");

    });

    it("Test Case2: login to the gmail account", function(){
        cy.xpath("//a[contains(text(),'Sign in')]").click();
        cy.xpath("//div[contains(text(),'Use another account')]").click();
        cy.xpath("#identifierId").type(testData.username);
        cy.xpath("//*[contains(text(),'Next')]").click();
        cy.xpath("#password").type(testData.password);
        cy.xpath("#submitbtn").click();
    })

});

But the second test is getting failed and the testData.username return undefined.

Is there anyway to map the specific JSON array object with specific function in the test.spec.js file?

Not sure how to map the first dataset index with first It (Test case 1) and second dataset index with second test case respectively.

Upvotes: 2

Views: 1828

Answers (2)

Fody
Fody

Reputation: 31904

One quick way is to skip if the testData does not have the required properties,

describe("How to map two different data set with respective test function",() =>{

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

    const testData = Cypress.env('fixture')

    beforeEach("",()=>{
        cy.visit(baseUrl);
    });

    it("Test Case1: Search the keyword", function () {

      if (!testData.searchKeyword) this.skip          

      cy.xpath("//input[@name='q']").type(testData.searchKeyword);
      cy.xpath("//input[@value='Google Search']").click();
      cy.get("//ul/li[2]").should("be.visible");
    });

    it("Test Case2: login to the gmail account", function() {

      if (!testData.username) this.skip

      cy.xpath("//a[contains(text(),'Sign in')]").click();
      cy.xpath("//div[contains(text(),'Use another account')]").click();
      cy.xpath("#identifierId").type(testData.username);
      cy.xpath("//*[contains(text(),'Next')]").click();
      cy.xpath("#password").type(testData.password);
      cy.xpath("#submitbtn").click();
    })
});

Tagging

You can also get into tags, adding a tag property to the testData

[
    {
        "tag": "search",
        "searchKeyword":"cypress"
    },
    {
        "tag": "user",
        "username":"QATesting",
        "password":"testprofile"
    }
]

Perhaps use a library like cypress-tags, then in the runner script

const cypress = require('cypress')
const fixtures = require('./cypress/fixtures/Test.json')

const promises = fixtures.map(fixture => {

  if (fixture.tag) {
    process.env.CYPRESS_INCLUDE_TAGS = fixture.tag
  }

  return cypress.run({
    env: {
      fixture
    },
    spec: './cypress/integration/test.spec.js',
  });
});

Upvotes: 1

Alapan Das
Alapan Das

Reputation: 18650

Since your fixtures data is in a array and the username and password fields are at index 1, so in order to access those you have to use:

testData[1].username
testData[1].password

In case if you don't want to use the index value, change the fixture structure to:

{
  "searchKeyword": "cypress",
  "username": "QATesting",
  "password": "testprofile"
}

And in your test directly use:

testData.username
testData.password

Upvotes: 0

Related Questions