3

I have a mocha javascript file in which I have require function to login to the application in headless browser mode, login using the crendentials and return the jwt authentication.

I want to call this script through K6. But as I understand, calling node module java script from K6 is not possible?

Is there an alternative to this?

irvis
  • 136
  • 1
  • 6
Jay
  • 339
  • 1
  • 7
  • 23

1 Answers1

7

I have also just started implementing k6 and had same step to do ;) Here is how I have done it.

  • you need to know how to authenticate to the API you want to use. I assume we have it, as you wrote you want to use node modules.
  • second, use appropriate method to communicate with API
  • next, catch token and append it to next requests headers
  • finally, test API with requests you want

I found code snippets on web page with k6 samples for APIs. I have shorten a bit, sample code and end up with:

import {
  describe
} from 'https://jslib.k6.io/functional/0.0.3/index.js';
import {
  Httpx,
  Request,
  Get,
  Post
} from 'https://jslib.k6.io/httpx/0.0.2/index.js';
import {
  randomIntBetween,
  randomItem
} from "https://jslib.k6.io/k6-utils/1.1.0/index.js";

export let options = {
  thresholds: {
    checks: [{
      threshold: 'rate == 1.00',
      abortOnFail: true
    }],
  },
  vus: 2,
  iterations: 2
};

//defining auth credentials
const CLIENT_ID = 'CLIENT_ID';
const CLIENT_SECRET = 'CLIENT_SECRET';
let session = new Httpx({
  baseURL: 'https://url.to.api.com'
});

export default function testSuite() {

  describe(`01. Authenticate the client for next operations`, (t) => {

    let resp = session.post(`/path/to/auth/method`, {
      //this sections relays on your api requirements, in short what is mandatory to be authenticated
      grant_type: GRANT_TYPE,
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
    });

    //printing out response body/status/access_token - for debug
    //    console.log(resp.body);
    //    console.log(resp.status);
    //    console.log(resp.json('access_token'));

    //defining checks
    t.expect(resp.status).as("Auth status").toBeBetween(200, 204)
      .and(resp).toHaveValidJson()
      .and(resp.json('access_token')).as("Auth token").toBeTruthy();


    let authToken = resp.json('access_token');
    // set the authorization header on the session for the subsequent requests.
    session.addHeader('Authorization', `Bearer ${authToken}`);

  })

  describe('02. use other API method, but with authentication token in header ', (t) => {

    let response = session.post(`/path/to/some/other/post/method`, {
      "Cache-Control": "no-cache",
      "SomeRequieredAttribute":"AttributeValue"
    });


    t.expect(response.status).as("response status").toBeBetween(200, 204)
      .and(response).toHaveValidJson();
  })

}
irvis
  • 136
  • 1
  • 6
  • Thanks for helping me here. As you can infer, I am trying to do authentication through UI rather than using API . I don't know if it will help me in this regard – Jay Nov 30 '21 at 10:45