Save login state

Is it possible to save the login state in k6 like playwright. In playwright it can be stored in the user.json and used for other tests. Is this sort of operation possible with k6 as my application will go to login page each time a test is run. This will not be useful if I have to login everytime I run a test. This would not mimic the user behavior at all.

@sagarthetennis10k
I assume that an acceptable behavior for you would be when every VU represents a user, and every VU should login once while running the test. Per VU user credentials can be achieved by using SharedArray like this:

import { browser } from 'k6/experimental/browser';
import { SharedArray } from 'k6/data';
import { vu } from 'k6/execution';

const users = new SharedArray('some name', function () {
  const userArray = [   
    { "username": "myuser1@example.com", "password": "MyFirstSecurePassword" },
    { "username": "myuser2@example.com", "password": "MySecondSecurePassword" }
  ];
  return userArray;
});

export default async function () {
    const user = users[vu.idInTest-1];
    const page = browser.newPage();
    // do the login with the user data
}

The problem is, when you call browser.newPage() a new BrowserContext is created and it is deleted at the end of the iteration. So in the next iteration in the same VU you get a clean BrowserContext, and you are logged off. I can’t managed to save the BrowserContext for the next iteration because it is closed at the end of the previous iteration, but you can achieve something like that.
User sessions are usually identified by cookies, so if you save the cookies after a successful login, and you set theese cookies in the BrowserContext at the begining of every iteration, you will allready be logged in in every iteration.

I experimented with the login page of uptimerobot.com with two users (the credentials in the example are not valid, so you should replace them if you want to try it out). sleep() commands are there to be able see the process if you run the script with K6_BROWSER_HEADLESS=false k6 run test.js . The whole script looks like this:

import { browser } from 'k6/experimental/browser';
import { sleep } from 'k6';
import { SharedArray } from 'k6/data';
import { vu } from 'k6/execution';

const users = new SharedArray('some name', function () {
  const userArray = [   
    { "username": "myuser1@example.com", "password": "MyFirstSecurePassword" },
    { "username": "myuser2@example.com", "password": "MySecondSecurePassword" }
  ];
  return userArray;
});

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      vus: 2,
      iterations: 4,
      options: {
        browser: {
          type: 'chromium',
        },
      },
    },
  }
}

let cookies = null;

async function login () {
  const ctx = browser.newContext();
  if (cookies != null) {  // we are already logged in
    ctx.addCookies(cookies);     // setting saved cookies
  } else {                // not logged in
    const page = ctx.newPage();
    try {
      const user = users[vu.idInTest-1];
      await page.goto('https://uptimerobot.com/login');
      page.locator('input[name="userEmail"]').type(user.username);
      page.locator('input[name="userPassword"]').type(user.password);
      page.locator('button[class="uk-button uk-button-primary uk-button-large uk-width-1-1"]').click();
      sleep(5);
    } finally {
      page.close();
    }
    cookies = ctx.cookies();  // saving cookies after the login
  }
  return ctx;
}

export default async function () {
  const ctx = await login();
  const page1 = ctx.newPage();
  try {
    await page1.goto('https://uptimerobot.com/dashboard');
    sleep(5);
  } finally {
    page1.close();
  }
}

I hope this is a help for your issue

Thanks a lot. How would the above script look if I’m having to scripts as different test cases. The first one is login and the second one will take the state of login and continue the actions.

@sagarthetennis10k
The above script keeps the state of the login (cookies). In the 2nd iteration and above it will not play the login procedure.

1 Like