Hi, I have a script and need to capture the response error for failed api requests to print on html report to show the issue of particular api request but I’m not able to capture. could you please any one suggest where I’m doing the mistake.
few requests getting TypeError: Cannot read property ‘json’ of undefined or TypeError: Cannot read property 0 of undefined error.
Please find my script
import http from "k6/http";
import { check, sleep } from "k6";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
import papaparse from "https://jslib.k6.io/papaparse/5.1.1/index.js";
import { SharedArray } from "k6/data";
var dateFormat = new Date();
var getDateFormat = dateFormat.toString().substring(4,25).replace(" ","_").replace(" ","_").replace(" ","_").replace(":","_").replace(":","_").trim();
const loginCredentials = new SharedArray('users', function () {
return papaparse.parse(open('./testData/login_credentials.csv'), { header: true }).data;
});
const aTestData = JSON.parse(open("./testData/sbt_testdata.json"));
export const options = {
//stages: [{ duration: "10m", target: 1000}],
stages: [{ duration: "1m", target: 100}],
//stages: [{ duration: "1s", target: 1}],
};
export default function () {
//console.log("getDateFormat "+getDateFormat);
const baseURL = "";
const xapikey =
"";
let responseError = "";
let params = {
timeout: '120s'
};
let newXsrfToken = "";
let XSRFTOKEN = "";
let cookieData = "";
var sleepValue = 0.6;
const payLoad = {
clientId: randomUser.companyID,
userId: randomUser.username,
password: randomUser.pwd,
};
sleep(Math.random() * sleepValue);
let response_about = http.get(baseURL + "about");
sleep(Math.random() * sleepValue);
console.log("response_about "+response_about.status);
if(response_about.status == 200){
console.log("### inside condition "+response_about.status);
cookieData = response_about.cookies;
XSRFTOKEN = cookieData["XSRF-TOKEN"][0].value;
//console.log(cookieData);
}
if(XSRFTOKEN != ""){
//sleep(Math.random() * 30);
//console.log("response_about body "+response_about.body);
console.log("XSRFTOKEN ### "+cookieData["XSRF-TOKEN"][0].value);
console.log("response_about status "+ response_about.status);
}else{
throw 'XSRFTOKEN ' + XSRFTOKEN + 'is not generated.';
}
responseError = response_about.error.json;
console.log("responseError "+responseError);
}
}