Home > Back-end >  K6 javascript how to pass xml parameter
K6 javascript how to pass xml parameter

Time:03-10

Below is the code of K6 that parse the XML SOAP request... so I want to pass parameters into XML data, how to do that from CSV or defined variable.

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

const soapReqBody = `
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hs="http://www.holidaywebservice.com/HolidayService_v2/">
<soapenv:Body>
    <hs:GetHolidaysAvailable>
        <hs:countryCode>UnitedStates</hs:countryCode>
    </hs:GetHolidaysAvailable>
</soapenv:Body>
</soapenv:Envelope>`;

export default function () {
  // When making a SOAP POST request we must not forget to set the content type to text/xml
  const res = http.post(
    'http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx',
    soapReqBody,
    { headers: { 'Content-Type': 'text/xml' } }
  );

  // Make sure the response is correct
  check(res, {
    'status is 200': (r) => r.status === 200,
    'black friday is present': (r) => r.body.indexOf('BLACK-FRIDAY') !== -1,
  });

  sleep(1);
}

CodePudding user response:

That's a regular JavaScript template literal (template string). Variables can be interpolated by inserting ${…} into your string:

let yourvariable = 42;
let xml = `<yourxml>${yourvariable}</yourxml>`;
console.log(xml); // <yourxml>42</yourxml>
  • Related