SOAP request Assertions using Cheerio

@odanylewycz , After watching your video I got a good understanding of the Cheerio usage in SOAP assertions. But I am facing some issues.

Below is the code snippet,

var cheerio = require(‘cheerio’);

pm.test(“Cheerio”,() => {

const $ = cheerio.load(responseBody,{

    ignoreWhitespace: true,

    xmlMode: true

});

//Render XML

console.log(“Entering logic”);

console.log($.xml());

pm.test("Using Cheerio ", function ()

{

pm.expect($(‘NumberToWords’).attr(‘xmlns’)).to.eql(‘http://www.dataaccess.com/webservicesserver/’)

});

});

Using the Public SOAP API Calculator -> “Add” folder

Getting the error "Using Cheerio | AssertionError: expected undefined to deeply equal ‘http://www.dataaccess.com/webservicesserver/’"

Hi @bpricilla!

Welcome to the community :clap:!

Thanks for watching my video! I’m glad it was helpful thus far.

As for your assertion error, can you explain what you’re trying to achieve? It looks like you’re trying to see if xmlns value equals that website text, right?

Can you print out the xml and the attribute statements? That should help us see if the values are being properly referenced and pulled out from the xml.

Best,
Orest

Also, @bpricilla, looks like doing a POST request to

https://www.dataaccess.com/webservicesserver/NumberConversion.wso

With an empty request body returns the following JSON

{
“id”: 101
}

So using cheerio here would not be useful, unless I am missing something here with my request? And if so, let me know.

Yes body is as below

Request Body:

<?xml version="1.0" encoding="utf-8"?>

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

soap:Body

<NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">

  <ubiNum>500</ubiNum>

</NumberToWords>

</soap:Body>

</soap:Envelope>

Response:

<?xml version="1.0" encoding="utf-8"?>

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

<soap:Body>

    <m:NumberToWordsResponse xmlns:m="http://www.dataaccess.com/webservicesserver/">

        <m:NumberToWordsResult>five hundred </m:NumberToWordsResult>

    </m:NumberToWordsResponse>

</soap:Body>

</soap:Envelope>

I am just trying to add some assertions using Cheerio, instead of accessing the response nodes directly. like below:

pm.test("Using Cheerio ", function ()

{

pm.expect($(‘m:NumberToWordsResponse’).attr(‘xmlns’)).to.eql(‘http://www.dataaccess.com/webservicesserver/’)

pm.expect($(‘m:NumberToWordsResult’)).to.eql(“five hundred”)

});

You can try any assertion to this response, using cheerio.

Hi @bpricilla,

Thanks for the details! I can’t seem to get the same response that you have, given the information provided. I receive a 415 Unsupported Media Type Error, refer to the screenshot below:

However, since you provided the response body, I was able to test against it by defining a static variable to contain the xml.

After enough testing, I was able to figure it out. To get value you are looking for, try this:

$('m\\:NumberToWordsResponse').attr('xmlns:m')

That should get you the text value of the xmlns:m attribute.

And applying it in a test as you have seemed to work as well.

I hope this helps!

Orest

1 Like

Thanks a lot for your time and effort :slight_smile:

I am just using the public SOAP API from

Also the snippet you shared is working, but when I try the same to do for other assertions its not working .

pm.test("Using Cheerio ", function ()

{

pm.expect($(‘m\:NumberToWordsResponse’).attr(‘xmlns:m’)).to.equal(‘http://www.dataaccess.com/webservicesserver/’)

pm.expect($(‘m\:NumberToWordsResult’)).to.eql(“five hundred”)

});

Please advice. Am I missing something?

Also should I save the response body directly to a variable or I should I store it in text format.

Hi @bpricilla,

Thanks for showing me what you’re using. Not sure why it wasn’t working for me, but nonetheless when I see you assertion for the screenshot, it looks like you want to compare the actual text from the tag. In this case, you want it to look like the following:

pm.expect($(‘m\\:NumberToWordsResult’).text()).to.eql(“five hundred”)

That should get you the inner text you are looking to compare.

https://cheerio.js.org has some great documentation as well that will be really helpful :blush:.

1 Like

@odanylewycz Thanks a lot, its working :slight_smile: Great help!!

1 Like

You’re welcome @bpricilla! Glad it’s working as expected and always glad to help! :blush: