How to assert xml response

My question:
Hello there

I’m trying to create assert in case an empty tag is received in the response or just closed tag like that

pm.test("PASSED", () => {

var responseJson2 = xml2Json(responseBody);

console.log(responseJson2);

var incomeassert = responseJson2['soap:Envelope']['soap:Body']['query']['incomes'];

pm.environment.set("incomeassert", incomeassert);

pm.expect(pm.responseBody.incomeassert()).to.eql(null);

});

But im getting error TypeError: Cannot read property ‘incomes’ of undefined. What am I doing wrong?

Found problem in parse (wrong tag), but now im getting error TypeError: Cannot read property ‘incomeassert’ of undefined

@khan_aktas We would need some screenshot or the sample response to suggest you what went wrong :blush:

2 Likes

Hey @khan_aktas

Try console.log() for each variable created and check the values through console and see what is going south here.

Also, in the last line, expect syntax, I wonder if it is pm.expect(pm.responseBody.incomeassert).to.eql(null); instead of pm.expect(pm.responseBody.incomeassert()).to.eql(null);

3 Likes
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <query>
            <incomes/>
        </query>
    </soap:Body>
</soap:Envelope>

Here typical response

Now im getting AssertionError: expected ‘’ to deeply equal null

var incomeassert = responseJson2['soap:Envelope']['soap:Body']['query']['incomes'];

pm.environment.set("incomeassert", incomeassert);

pm.expect(pm.responseBody.incomeassert()).to.eql(null);

assume, here incomeassert got value 5, now you are trying to access

 pm.responseBody.incomeassert

which will resolve to:

pm.responseBody.5

Also there is no property called responseBody for pm object

there is a global object called responseBody that returns response as text instead of json

 console.log(responseBody)

so i am not sure what you are trying to do when calling

 pm.responseBody.incomeassert 

could you please some more information , reading documentation would help you a bit

2 Likes

I’m trying to come up with a way to check if the parse succeeded and if the script was able to assign a non-null value to a variable.

If it was not possible to assign a non-zero value to a variable, then I would like to receive information about this.
Now I can only check the status of the code, but there are times when the system returns an empty value (just closed tag), because there was no receipt for the object.

Here my typical script when im trying to query some info about obj in systems

pm.test("Regnum assert", function () {
postman.setNextRequest(null);
pm.response.to.have.status(200);
postman.setNextRequest();
var responseJson = xml2Json(responseBody);
console.log(responseJson);
var Regnum = responseJson['soap:Envelope']['soap:Body']['QueryStatus']['SummaryList']['Summary']['registrationNumber'];
pm.collectionVariables.set("Regnum ", Regnum );
});

please help @praveendvd

I figured out how I can do it, here is an example

var responseJson1 = xml2Json(pm.response.text());
console.log(responseJson1);
pm.expect(pm.response.text()).to.include("sometext or tagname");
pm.expect(pm.response.text()).to.include(pm.variables.replaceIn('{{somevalue}}'));
1 Like

If you want to write tests for xml response, you should convert response to text, then you will able to search for the necessary words/tags/data in the text.

If you want to parse xml response and write data to variables, it will be easier for you to convert response to json.

Use this function to parse the XML Response

function xmlStringToJSON(xml, primaryTag = 'query') {
    if (!xml) return {};
    const regex = new RegExp(`<${primaryTag}.*[^>]>((?:.*\n)*(?=<\/${primaryTag}>))`, 'gmi');
    xml = regex.exec(xml)[1].replace(/&amp;/g, "&")
        .replace(/&lt;/g, "<")
        .replace(/&gt;/g, ">")
        .replace(/&quot;/g, "\"")
        .replace(/&#039;/g, "'")
        .replace(/\t/g, '')
    if (!xml) return {};
    if (xml.indexOf('<?xml') > -1) xml = xml
        .replace(xml.slice(xml.indexOf('<?xml'), xml.indexOf('?>') + 2), '');
    xml = xml.split('\n').filter(itm => !!itm.trim());
    return (function recursiveParse(lines, currentLine = 0, object = {}, parent = null) {
        if (currentLine === lines.length) return object;
        const line = lines[currentLine],
            openTag = line.match(/<([^\/\s>]+)(\s|>)+/),
            closeTag = line.match(/<\/([^\s>]+)(\s|>)+/),
            tagContent = line.match(/(?<=>)(.+)(?=<\/)/);
        currentLine++;
        if (!tagContent && openTag && !closeTag)
            object[openTag[1]] = recursiveParse(lines, currentLine, {}, object);
        else if (tagContent && openTag && closeTag) {
            object[openTag[1]] = tagContent[0];
            recursiveParse(lines, currentLine, object, parent);
        } else if (!tagContent && !openTag && closeTag)
            recursiveParse(lines, currentLine, parent);
        return object;
    })(xml);
}