How can we login to system with multiple users during parallel run in cypress test run

Viewed 158

How can we run parallel cypress test with multiple users in a cypress-cucumber-preprocessor BDD framework. I can't use single user in parallel run, as the latest session will kicked out the existing cypress test run and existing test run will fail.

Please Note: In our web application if a user access the system and login to the web application and perform some actions. At the same time access the system in a different browser and login with the same user. The first session will gets logged out.

We are using

  1. bitbucket
  2. jenkins CI/CD pipeline
  3. docker container

Could someone please advise if you happened come across with similar

Automation folder structure:

tests/
       cypress/
        integration
           /folder1/
                test1.feature
           /folder2/
               test2.feature
               test3.feature
           /folder3/
               test4.feature
           /folder4/
               test5.feature
           /folder5/
               test6.feature
           /folder6/
               test7.feature
2 Answers

If you just need multiple users, but the tests are completely independent, you can have multiple user credentials in a fixture,

for example

// users.json

[
  { 
    userId: 1,
    username: 'John',
    password: 'abc'
  },
  { 
    userId: 2,
    username: 'Jack',
    password: 'def'
  },
  ... // and so on
]

Each test will pick a different user in the beginning

// test1

import { Given } from "@badeball/cypress-cucumber-preprocessor";

Given('User has logged in with unique credentials', () => {
  before(() => {
    cy.fixture('users.json').then(users => {
      const user = users[0]
      cy.login(user.username, user.password)
  })
})
// test2

import { Given } from "@badeball/cypress-cucumber-preprocessor";

Given('User has logged in with unique credentials', () => {
  before(() => {
    cy.fixture('users.json').then(users => {
      const user = users[1]
      cy.login(user.username, user.password)
  })
})

Then if the tests overlap when parallel running, the server sees different users logging on.


Or use the Cucumber Before hook

import { Before } from "@badeball/cypress-cucumber-preprocessor";

Before(function () {
  cy.fixture('users.json').then(users => {
    const user = users[0]
    cy.login(user.username, user.password)});
  })
})

You can have each test create a new user with appropriate permissions for each tests, run with APIs for quicker execution. You'll may need to have a separate clean up process to remove all the created users. The benefit of this to truly have a clean state for each test and not have to worry about user accounts.

Related