How to fix error code fixtures in cypress

235 Views Asked by At

I get the error code, when i coding fixtures in cypress, and if we want create fixture, we must create 2 files, first file for inizialitation fixtures and second file for main file.

and here is the code for the fixture with the file name data_sauce_demo.json

{
  "username": "standard_user",
  "password": "secret_sauce"
}

And here is the spec:

describe('Login with fixture data', function () {
  it('Should try to login', () => {
    cy.visit('https://www.saucedemo.com/');
    cy.fixture('data_sauce_demo').then((user) => {
      const username = user.username;
      const password = user.password;

      cy.get('#user-name').type(username);
      cy.get('#password').type(password);
      cy.contains('#login-button').click();
    });
  });
});

How do fix solved the problem?

1

There are 1 best solutions below

0
user16695029 On

You should add the fixture extension to the test code. Cypress uses the extension to know that the file contents should be interpreted as json.

cy.fixture('data_sauce_demo.json')   // specify file with extension
  .then((user) => {                  // user is an object, Cypress has parsed as JSON
    const username = user.username;
    const password = user.password;
    ...

Refer to docs fixture - Yields

cy.fixture() yields the contents of the file.
Formatting is determined by its file extension.