Correlation of parameter in K6

I use Gatling as a perf test tool as until now, and want to try to reproduce my script using K6.

.exec(http("request_url")
.get("https://url")
.check(css("a[id='Test IDP']"**,** "href").saveAs("idp"))
.headers(*headers_1*)
.check(*status*.is(expected = **200**)))

How could I code the same logic (to save the idp value) in K6?

Hi @fugmag, welcome to the forum!

There’s a few different ways to parse values from a response:

This will attempt to parse the response as an HTML document that can then be queried with Selectors that mimic jQuery.find. Do note, however, that it does not support CSS selectors (as mentioned here). You would need to use a different selection strategy.

This is probably the easiest function to use as it operates on a string; you just need to specify what appears immediately in front of and after the thing you are wanting to extract.

Here’s an example using Response.html() (note that you would need to modify the selector):

import { check } from 'k6';
import http from 'k6/http';

export default function () {
  const res = http.get('https://httpbin.test.k6.io/');

  // probably a good idea to first check that the response is OK before extracting anything
  check(res, {
    'is status 200': (r) => r.status === 200,
  });

  // parse the response as an HTML document
  const html = res.html();

  // and then grab the required value using selectors
  const linkHref = html.find('a').first().attr('href');
  console.log(linkHref);
}

Here’s what extraction might look like when using findBetween (you would be passing in res.body as the first parameter instead of the res string - this is just an example):

import { findBetween } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';

export default function () {
  const res = `<html><body><a id="TestIDP" href="https://idp.somewhere.com/"</a></body></html>`;

  const idp = findBetween(res, 'id="TestIDP" href="', '"');
  console.log(idp);
}