Postman test script : Need Help to test a SOAP Response

Hi,

I would like to code a post script test but I am quite a beginner in postman an java.
Also, I will appreciate some help.

Some time, my request can generate this kind of response :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV=“http://schemas.xmlsoap.org/soap/envelope/”>

*<SOAP-ENV:Header/>*

*<SOAP-ENV:Body>*

    *<SOAP-ENV:Fault>*

        *<faultcode>SOAP-ENV:Server</faultcode>*

        *<faultstring>Failed to execute program! \[Error during execution: 'xxxxx'\]</faultstring>*

    *</SOAP-ENV:Fault>*

*</SOAP-ENV:Body>*

</SOAP-ENV:Envelope>

I have tried to follow some samples written in the Postman documentation but with no success.

Here is below my post-script :

pm.test(“Status code is 200”, function () {

pm.response.to.have.status(200);

});

const xml2js = require(‘xml2js’);

let jsonResponse;

xml2js.parseString(pm.response.text(),{explicitArray: false}, (err, result)=>{

***jsonResponse = result;***

});

console.log(jsonResponse);

pm.test(“Fault string is empty”, function () {

pm.expect(jsonResponse.Envelope.Body.Fault).to.be.empty;

});

The first test is OK. But I don’t know how to test that there ise no Fault or faultstring in the response.

Thank you for your help.

The following script can be used as a base for your scenario.

const response = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>Failed to execute program! \[Error during execution: 'xxxxx'\]</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>"

// XML without the Fault keys.
const response2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope>"

// XML with an empty Faultstring
const response3 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring></faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>"

// const response = pm.response.text();

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

var parseString = require('xml2js').parseString;
var stripPrefix = require('xml2js').processors.stripPrefix;

parseString(response, { // change response to test various response
    ignoreAttrs: true,
    explicitArray: false,
    tagNameProcessors: [stripPrefix]
}, function (err, result) {
    // console.log(result);

    // https://stackoverflow.com/questions/1098040/checking-if-a-key-exists-in-a-javascript-object
    pm.test("Fault key does not exist", function () {
        // Does the fault key exist?
        pm.expect(result.Envelope.Body.hasOwnProperty("Fault")).to.be.false;
    });

    // https://stackoverflow.com/questions/154059/how-do-i-check-for-an-empty-undefined-null-string-in-javascript
    pm.test("Faultstring is empty", function () {
        // Does the fault key exist?
        pm.expect(!!result.Envelope.Body.Fault.faultstring).to.be.false;
        // Will returns false for null, undefined, 0, 000, "", false.
    });
});

We’re going to remove the prefix from the XML string when converting it into a JavaScript object. This will make the data easier to work with and understand.

I’ve added two example tests:

  • The first test checks if there’s a key called “Fault”.

  • The second test checks if the “faultstring” contains any content. (Keep in mind: if “faultstring” doesn’t exist, this test will throw an JavaScript exception.)

What the API returns may vary, but these two tests should help you get started. You can also check the provided links for more details and examples.