How to pass login page, save token and get the subsequent requests

Hi all, I currently have a user scenario where I have to open up the login page, login with certain credentials, save that token and then use it to open up two other webpages that come after the login is accepted. I am new to k6 so bear with me. I know there are a lot of resources out there but if you would give me actual code help instead of just links that would be awesome. Thank you in advance.

This is what I have thus far and it runs but I don’t know if it’s doing what I want.

import http from 'k6/http';
import {check, group, sleep, fail} from 'k6';

export let options = {
    maxRedirects: 0,
    stages: [
    { duration: "30s", target: 5}, //simulate ramp-up of traffic from 1 to 5 users over 30 seconds
    { duration: "15s", target:5}, // stay at 5 users for 15 seconds
    { duration: "30s", target: 0},  //ramp down to 0 users over 5 seconds
  ]
  };

  function startScript(){
      let res='';
      return loginres;
  }
  const USERNAME = 'demo........';
  const PASSWORD = 'z<U...........';
  const BASE_URL = 'https://socialscape........./';

  export function setup() {
  
    let res = http.post(`${BASE_URL}/login/`, {
      username: USERNAME,
      password: PASSWORD
    });



    let authToken = res.json('access');
    check(authToken, { 'logged in successfully': () => authToken !== '', });
  
    return authToken;
  }
  
  
  export default (authToken) => {
    const requestConfigWithTag = tag => ({
      headers: {
        Authorization: `Bearer ${authToken}`
      },
      tags: Object.assign({}, {
        name: 'PrivateCrocs'
      }, tag)
    });

    group('spectrum and dashboard', () => {
        let responses = http.batch([
            ['GET', `https://dashboard.........`, null, {tags: {name: 'PrivateCrocs'}}],
            ['GET', `https://dashboard...............`, null, {tags: {name: 'PrivateCrocs'}}],
        ])
    })
}

anyone know how to solve my problem?

Hi there,

your script looks to be on the right track, but you’re not using requestConfigWithTag, so your Authorization header won’t be passed to the http.batch() requests.

Maybe something like:

export default (authToken) => {
  const requestConfigWithTag = tag => ({
    headers: {
      Authorization: `Bearer ${authToken}`
    },
    tags: Object.assign({}, {
      name: 'PrivateCrocs'
    }, tag)
  });

  group('spectrum and dashboard', () => {
      let responses = http.batch([
          ['GET', `https://dashboard.........`, null, requestConfigWithTag({someAdditionalTag: 'mytag'})],
          ['GET', `https://dashboard...............`, null, requestConfigWithTag()],
      ])
  })
}

Forum tip: you might get a quicker response if you post an error or a clear description of the problem you’re having, instead of an open ended request for code that implements your specific test. As the test author, only you know what you want to happen, so any errors preventing you from doing so are much easier to address.

I’m not too familiar with javascript or k6 at this time but I am getting this error from one of my checks, where non of my batch requests are opening, any ideas?

This is all I’ve changed from above

group(‘spectrum and dashboard’, () => {
let responses = http.batch([
[‘GET’,http://dash..........., null, requestConfigWithTag],
[‘GET’,http.dash2.........., null requestConfigWithtag],
])
check(responses, {opened dashboard data’: ® => r.status === 200}];
})
}

responses is an array in your case, so the check should either be check(responses[0], ..., check(responses[1], ..., or the condition inside of it should be r[0].status === 200, r[1].status === 200, etc.

In general, scripting in k6 requires you to know JavaScript. It’s not a difficult language to pick up, but this forum is not the best place for beginner JavaScript questions, sorry. You can’t expect random people on the internet to write your full scripts for you… I suggest spending some time with some JavaScript tutorial and the k6 docs, so you can ask better questions here and receive some more useful help.

In the mean time, maybe the test builder in k6 cloud can be useful for you? You’d need to register an account, but you don’t need to pay us money to use the test builder, even after your trial expires. You can build your tests in that GUI interface, including things like checks and variables, and get the resulting k6 script (see the “script” toggle in the upper right), which you can then execute locally.