Sign in with multiple users simultaneously during one test by utilizing JSON

Instead of going through the hassle of logging in with one user, then logging out and repeating the process with different users for each test, I want to streamline the process by testing login using multiple users within a single test.

Here is the JSON data that will be used to fetch information:

[
    {
      "id": "standard User",
      "username": "standard_user",
      "password": "secret_sauce"
    },
    {
      "id": "locked out user",
      "username": "locked_out_user",
      "isComplete": "secret_sauce"
    },
    {
      "id": "Problem user",
      "username": "problem_user",
      "password": "secret_sauce"
    },
    {
      "id": "perfromance glitch user",
      "username": "performance_glitch_user",
      "password": "secret_sauce"
    },
    {
        "id": "Invalid User",
        "username": "perform",
        "password": "secret12"
      }
]

Below is the script that demonstrates how to log in with multiple users:

it('Login with multple users', () => {
        
        // Logging in with the first User.
        cy.visit('/')
        cy.get('[data-test="username"]').type(user[0].username)
        cy.get('[data-test="password"]').type(user[0].password)
        cy.get('[data-test="login-button"]').click()
        cy.url('https://www.saucedemo.com/inventory.html')
        // Logging in with the second User.
        cy.visit('/')
        cy.get('[data-test="username"]').type(user[1].username)
        cy.get('[data-test="password"]').type(user[1].password)
        cy.get('[data-test="login-button"]').click()
        cy.get('[data-test="error"]').should('have.text', 'Epic sadface: Sorry, this user has been locked out.')
    });
    
});

Answer №1

If you want to achieve a similar outcome, consider the following code snippet:

it('Login with multiple users', () => {
  cy.readFile('cypress/fixtures/users.json').then((users) => {
    users.forEach((user) => {
      cy.visit('/')
      cy.get('[data-test="username"]').type(user.username)
      cy.get('[data-test="password"]').type(user.password)
      cy.get('[data-test="login-button"]').click()
      cy.url('https://www.saucedemo.com/inventory.html')
      cy.get('[data-test="error"]').should(
        'have.text',
        'Epic sadface: Sorry, this user has been locked out.'
      )
    })
  })
})

Additionally, please note that in your JSON file, the second entry does not have a password field but instead has an isComplete field.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Aggregate JSON data to showcase in React Dropdown menu

After receiving my JSON data, I have the following structure: { "cars": [ { "make":"BMW", "model":"X3", "Lot":"Used" }, { ...

What is the process for integrating Socket io into an external JavaScript file that is connected to an HTML file?

Currently, I am utilizing node js, socket io, and express to develop a multiplayer web game. To begin, the server initiates and listens on port 2000. Upon visiting localhost:2000, the file lobby.html is transmitted using: //app.js const express = require ...

Navigating through an Angular 2 service

I'm struggling to retrieve an array from a JSON API and then loop through it. I can't seem to grasp how it all fits together. Any guidance would be greatly appreciated. This is what my service looks like: import {Injectable} from '@angular ...

A guide on breaking down the ID passed from the backend into three segments using React JS

I pulled the data from the backend in this manner. https://i.stack.imgur.com/vMzRL.png However, I now require splitting this ID into three separate parts as shown here. https://i.stack.imgur.com/iy7ED.png Is there a way to achieve this using react? Bel ...

Add content to the current position of the text input field in Ionic 2 by placing

I'm currently developing a simple calculator app and I'm facing an issue. My goal is to add text at the caret position of an input text field when a button is clicked. However, the code snippet provided below is not functioning as expected: At ...

Changing the URI in accordance with the previous URI

I am encountering an issue in my React application where multiple updates of the URI are being made within the same event by adding query parameters using the router.push function from various locations in the code. However, some updates are getting lost b ...

Creating a vertical column in an HTML table: The step-by-step guide

I need help creating a vertical and centered table layout. The current output is not what I expected. Expected Output https://i.sstatic.net/WL4px.png Current Output: https://i.sstatic.net/aBX6c.png I want the table to be vertical and center-aligned ...

The script fails to start using npm start, however, it runs smoothly when using node server.js

For a while now, I've been facing an issue that I just can't seem to resolve. When I navigate to my project's src folder and execute `node server.js`, everything functions as expected. I can access the application at http://localhost:3000/cl ...

Utilizing jQuery to Toggle Visibility of Table Rows on Button Click

I have a unique layout on my page where there are two tables positioned side by side. The table on the left consists of buttons with company names, and the table on the right should display employees associated with each specific company. Upon initially l ...

Using JavaScript to retrieve items from an array

Using an ajax call, I fetch an array of objects from a php script. However, I'm struggling to access the nested objects within it. Let's take a look at the structure of the openings object: Object 2014-01-11: Array[5] 0: Object appoint ...

The 'canvas' module could not be located in the system.Here are the required stacks:- /var/task/index.js- /var/runtime/index.mjs

I am currently developing a lambda function using the serverless framework. The function utilizes chartjs-node-canvas to create graphics, and everything runs smoothly on my MacBook when tested locally. However, when I deploy the function to AWS either dire ...

Trying to iterate through and make changes to a Firebase array results in an undefined

Update: Upon further investigation, it appears that value[i].extrainfoimage is only undefined when accessing imageRef.child("-JlSvEAw...... Despite reviewing the documentation multiple times, I still haven't been able to resolve this issue. Essentia ...

Verify if the contract address corresponds to a token and retrieve the token details, such as its symbol

Can I use web3 to retrieve token information such as symbol and total supply similar to the etherscan API pro endpoint tokeninformation by providing the contract address? I'm interested in determining whether the addresses I collect are tokens or reg ...

What is the best way to retrieve route data based on route name in VueJs?

When working with VueJs, I have found that I can easily access the data of the current route. However, I have encountered a situation where I need to access the data of a different route by its name. Let's say I have defined the following routes: [ ...

Switch up the sequence of selected/appended SVGs in D3

In this dot matrix visual example, different colored circles represent funding percentages from three countries: USA, Canada, and Mexico. The gray circles indicate the remaining funding to be raised. The code snippet showcases how the circles are mapped ba ...

Retrieving data from an Array containing a mix of string and integer values represented as strings, organized by the length of the

Imagine you have an array structured like this: employeeArr = ['Steve', '80000', 'Jen', '90000', 'Bob', '40000'] Where employeeArr[0] and employeeArr[2] represent employee names, and employeeA ...

finding the index of a particular checkbox in a list

I am working with a list of checkboxes that are dynamically created using JavaScript, each contained within its own separate div element. I need to determine the position number of a specific checkbox within all checkboxes sharing the same class within a p ...

What is the most efficient way to organize these arrays?

I've been racking my brain trying to figure out a sorting logic for some arrays, but I'm stumped. Maybe you have an idea? Imagine we have an array of objects structured like this: let obj = {day:'2', month:'6', year:'19 ...

Unable to recreate the Jquery Mobile Autocomplete demonstration

Attempting to recreate a demo using my own remote data source: The HTML page mirrors the demo, with one change: url: "http://localhost/sample.php", Here is the dummy remote data source sample.php <?php $a = array('apple', 'mango&apo ...

Amcharts displays individual balloons for each graph instead of a single balloon for all graphs

My goal is to have a single balloon for all stacked graphs. I modified my code according to this guide. Unfortunately, the data for messages is not being displayed. https://i.sstatic.net/2MRhc.jpg Did I make an error in my implementation? function creat ...