Fixture folder in cypress

In this fixture folder, the test data are stored. While executing, Cypress contacts the fixture folder automatically to retrieve the test data.

In the fixture folder, create a file named user.json and put below data

{
  "username": "invalid username",
  "password": "invalid password"
}

Let us understand, how the values from the user.json file are retrieved with the help of a program.

//Syntax 
<Filename>.<parameter_name>
  • Filename : Filename without extension
  • Parameter_name : The key value given in the JSON file

In the below code, we are retrieving the values from the file called user.json and parameter called username, password.

const username = user.username;
const password = user.password;

Example program :

describe("Overriding the fixture file", function () {
  it("try to login", () => {
    cy.visit("http://zero.webappsecurity.com/login.html");
    cy.fixture("user").then((user) => {
      const username = user.username;
      //it is mentioned like user.username since we are getting the details form the user.json file 
      const password = user.password;
      cy.get("#user_login").type(username);
      cy.get("#user_password").type(password);
      cy.contains("Sign in").click();
    });
  });
});

Output :

overriding-fixture-file

$ function in cypress

In cypress, by using the $ function also we can store the values in the variable. The use of the $ function is very bad in production testing. So don't use this function in the test program. Let's just know that this function also exists in cypress.

Example program :

describe("Xpaths with cypress example", () => {
  // it("should click on element using xpath", () => {
  it("should click expose jQuery element in the current window", () => {
    cy.visit("http://zero.webappsecurity.com/index.html");
  });
  it("should click expose jQuery element in the current window", () => {
    const signInButton = Cypress.$("#signin_button");
    signInButton.click();
  });
});

Output :

dollar-function-cypress

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions