Home > database >  How to access specific record from fixture/*.json file into sepcific test case in cypress?
How to access specific record from fixture/*.json file into sepcific test case in cypress?

Time:12-22

testdata.json

[
    {
        "case_id": 1,
        "case": "Login with valid data as wholesaler",
        "username": "admin",
        "password": "password",
        "result": "pass"
    },
    {
        "case_id": 2,
        "case": "Login with valid data as reseller",
        "username": "myreseller.admin",
        "password": "password",
        "result": "pass"
    },
    {
        "case_id": 3,
        "case": "Login with valid data as subscriber",
        "username": "mytenant.admin",
        "password": "password",
        "result": "pass"
    }
]

login.spec.js

  before(() => {
    cy.fixture('testdata').then((datajson) => {
      testdata = datajson
      return testdata
    })
  })
------it blocks-----
  it.only('TC03 - Login with valid data as subscriber', () => {
    cy.login(testdata.username, testdata.password);                            
    cy.title().should('equal', 'Home');
    cy.logout();
-------more it blocks-------

How to use it properly so that line 2 returns {testdata.username = mytenant.admin || testdata.password = password} fetching specifically from 3rd record

CodePudding user response:

Your json is structured as an array, so you need to get elements from it using index or restructure your testdata.json fixture.

Try this:

it.only('TC03 - Login with valid data as subscriber', () => {
    cy.fixture('testdata').then((dataFixture) => {
        cy.login(dataFixture[2].username, dataFixture[2].password);
        cy.title().should('equal', 'Home');
        cy.logout();
    })
})

P.S. Assuming your testdata is imported at the top of the file.

CodePudding user response:

You can do something like this:

describe('Some Test Suite', function () {
  beforeEach(function () {
    cy.fixture('testdata').then(function (testdata) {
      this.testdata = testdata
    })
  })

  it.only('TC03 - Login with valid data as subscriber', function () {
    cy.log(this.testdata[2].username) //prints mytenant.admin
    cy.log(this.testdata[2].password) //prints password
    cy.login(this.testdata[2].username, this.testdata[2].password)
    cy.title().should('equal', 'Home')
    cy.logout()
  })
})

Make sure that you are getting the fixtures file in beforeEach() so that the fixture is available for all test cases.

CodePudding user response:

In the Cypress Docs there are two ways described how you can handle those JSON files from the fixtures folder: https://docs.cypress.io/api/commands/fixture#Examples

1. Option

Load your testdata.json using cy.fixture():

cy.fixture('testdata.json').as('testdata');

2. Option (my recommendation)

In your login.spec.js you can import all relevant json files from your fixtures folder:

import testdata from '../fixtures/testdata.json';

You can then directly use them as follows to for example use the username and password for the first user:

  it.only('TC03 - Login with valid data as subscriber', () => {
    cy.login(testdata[0].username, testdata[0].password);                            
    cy.title().should('equal', 'Home');
    cy.logout();
  })
  • Related