# Assert the existence of an xml element with cheerio and chai

**URL:** <https://community.postman.com/t/assert-the-existence-of-an-xml-element-with-cheerio-and-chai/18855>\
**Category:** Help Hub\
**Tags:** soap, chai, cheerio\
**Created:** [January 5, 2021, 12:17pm UTC](https://community.postman.com/t/assert-the-existence-of-an-xml-element-with-cheerio-and-chai/18855 "2021-01-05T12:17:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![magnus\_r](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/magnus_r/32/6816_2.png) [@magnus\_r](https://community.postman.com/u/magnus_r)\
**Post date:** [January 5, 2021, 12:17pm UTC](https://community.postman.com/t/assert-the-existence-of-an-xml-element-with-cheerio-and-chai/18855/1 "2021-01-05T12:17:58Z")

</div>

Hello,

I am using Postman to test a SOAP API. In the tests I use the cheerio library to assert the contents of the xml response. So far it has been easy to use cheerio. I can assert that an element has certain text content or that an element has a certain attribute. For example, the test below works just fine.

```
pm.test("Response status is 'ok'", function () {
        pm.expect($("response > status").text()).to.eq("ok");
    });

```

But for some strange reason I haven´t been able to assert that an element exists or not.

The API I am testing is expected to return a report given the right inputs. With wrong inputs an 200 response is still sent, but there is no report element

When I researched I found this SO post: [How to check whether the element exist using chai? - Stack Overflow](https://stackoverflow.com/questions/36931400/how-to-check-whether-the-element-exist-using-chai) which explains that chai-jquery library is needed to assert DOM elements.

Being very hopeful I tried testing anyway using _to.exist_ but I ended up with a test that always passes even if I point to nonexciting elements.

```
pm.test("This ought to fail, but it doesn't", function() {
    pm.expect($("response > NonsenseReport")).to.exist; 
});

```

With cheerio and chai I am already able to traverse the XML document nicely, but yet asserting that a element exists is strangely tricky. I also prefer not to pull in another library in Postman. The contents of the report can change from time to tome so I don’t want to rely on asserting a text content or attribute within the report either. Any ideas how I can easily assert that an xml element exists in Postman?

---

<div class="post-metadata">

**Author:** ![praveendvd](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/praveendvd/32/6796_2.png) [@praveendvd](https://community.postman.com/u/praveendvd)\
**Post date:** [January 5, 2021, 12:33pm UTC](https://community.postman.com/t/assert-the-existence-of-an-xml-element-with-cheerio-and-chai/18855/2 "2021-01-05T12:33:09Z")

</div>

```
pm.test("This ought to fail, but it doesn't", function() {
    pm.expect($("response > NonsenseReport").length).to.eql(0); 
});

```

you could use length property

---

<div class="post-metadata">

**Author:** ![magnus\_r](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/magnus_r/32/6816_2.png) [@magnus\_r](https://community.postman.com/u/magnus_r)\
**Post date:** [January 5, 2021, 12:55pm UTC](https://community.postman.com/t/assert-the-existence-of-an-xml-element-with-cheerio-and-chai/18855/3 "2021-01-05T12:55:01Z")

</div>

Thanks @praveendvd. Using the length property worked great.

Since I want to assert that the report is created, I ended up asserting that the length is not equal to 0. Something like this:

```
pm.test("ExpectedReport is generated", function() {
    pm.expect($("response > ExpectedReport").length).to.not.equal(0);
});

```

Thanks again!
