0

Here is my XML code, what I would like to do is check that within the <testcase> element there is the <failure> element with an if to put conditions.

<testsuites disabled="0" errors="0" failures="1" tests="20" time="102.012">
   <testsuite name="name1" timestamp="2021-01-27T10:20:51" hostname="localhost" time="79.312" errors="0" tests="12" skipped="1" disabled="0" failures="1">
      <testcase classname="nametescase" name="STEP_1: Date filter" time="3.776">
         <failure></failure>
      </testcase>
   </testsuite>
</testsuites>

How can I do this condition in nodejs?

1 Answer 1

1

You could use XPath in Nodejs with the following packages: xmldom and xpath

A simple example

var xpath = require('xpath')
var dom = require('xmldom').DOMParser

const xml = `
<testsuites disabled="0" errors="0" failures="1" tests="20" time="102.012">
   <testsuite name="name1" timestamp="2021-01-27T10:20:51" hostname="localhost" time="79.312" errors="0" tests="12" skipped="1" disabled="0" failures="1">
      <testcase classname="nametescase" name="STEP_1: Date filter" time="3.776">
         <failure></failure>
      </testcase>
   </testsuite>
</testsuites>
`

const doc = new dom().parseFromString(xml);
var failureNode = xpath.select("//testcase/failure", doc); //<--- xpath
console.log(failureNode.length);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.