Cannot get text from element inside DOM in Cheeriojs

I am using Postman to write UI tests, and need to get a text from the element inside the DOM.

What is the correct syntax to do so? ? The structure of the webpage is div->ul->li->class

The test passes every time, even with the wrong text. I would appreciate any examples.

Blockquote
var cheerio = require('cheerio'); const $ = cheerio.load('<ul class="cards-wrapper">...</ul>'); $.html(); pm.test('Test name', function () { $('.cards-wrapper').text("12345"); });

@sstadnik Welcome to the Community :bouquet:

So did you tried to print and see the

 console.log($.html());

Also in your Test I see that the value to validate against should be given in different format.

var cheerio = require('cheerio'); 
const $ = cheerio.load('<ul class="cards-wrapper">...</ul>'); $.html(); 

pm.test('Test name', function () { $('.cards-wrapper').text().to.eql("12345"); });

});

I have an example using SOAP XML response.

Body is as

<?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>

Under Tests I have written so

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($('m\\:NumberToWordsResponse').attr('xmlns:m')).to.eql('http://www.dataaccess.com/webservicesserver/')
pm.expect($('m\\:NumberToWordsResult').text()).to.eql("five hundred ");
});


});

Please check if its useful to you. If these doesn’t answer your question, please post with few other details :slight_smile: We would love to help!!

@bpricilla Thank you very much for your help.

console.log($.html()); did not produce any results

I think the problem might be in using shadow dom on that website.

@sstadnik, yes please try to parse the response.

Else you can provide the html response here :slight_smile:

May be this source about cheerio would help you in a way.

1 Like