-3

I have runtime XML file which has predefined format and Its being created in Jenkins workspace. I just want to parse the XML payload by using shell script.

Sample Payload:

 <test-results>
     <test-method status="FAIL" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">
       <params>
         <param index="0">
           <value>
           <![CDATA[ account_number:22988419 ]]>
           </value>
         </param>
         <param index="1">
           <value>
           <![CDATA[ txn_id:6wdadfsad2134330L ]]>
           </value>
         </param>
         <param index="2">
           <value>
           <![CDATA[ amount:1100 ]]>
           </value>
          </param>
          <param index="3">
            <value>
            <![CDATA[ currency:USD ]]>
            </value>
          </param>
          <param index="4">
            <value>
            <![CDATA[Id:11a09 ]]>
            </value>
          </param>
          <param index="5">
            <value>
            <![CDATA[Name:Consumer [testId=AS1-TC2, description=Txn amount; wallet -Bal, BA,CC,VISA Credit; Consumer - CC;No other preference set]]]>
            </value>
          </param>
        </params>
     </test-method>
     <test-method status="PASS" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">
         <params>
           <param index="0">
              <value>
              <![CDATA[ account_number:22988419 ]]>
              </value>
           </param>
           <param index="1">
             <value>
             <![CDATA[ txn_id:6wdadfsad2134330L ]]>
             </value>
           </param>
        </params>
      </test-method>
      <test-method status="FAIL" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">
        <params>
           <param index="0">
              <value>
              <![CDATA[ account_number:22988419 ]]>
              </value>
           </param>
           <param index="1">
              <value>
              <![CDATA[Name:Consumer [testId=AS1-TC3, description=Txn amount; wallet -Bal, BA,CC,VISA Credit; Consumer - CC;No other preference set]]]>
              </value>
           </param>
         </params>
      </test-method>
  </test-results>

The above payload I have to read the status of test-method if it's "FAIL" then I need to get the "testId" value from that particular test method. The above payload I have 3 test methods only two had status Failed I need to fetch both Test id and assign to variable as like below

Expected Output:

  fetchResult = AS1-TC2,AS1-TC3

I just need to fetch the "testId" of failed test-methods and assign it to variable with comma separated using shell script.

I tried the below lines but It doesn't return the entire test-method tags

   failedTC=`grep "test-method.*FAIL" results.xml | sed -e 's/^.*test-instance-name="(.*)-----.*/\1/'

Output:

   <test-method status="FAIL" name="flowVISA" duration-ms="843329" data-provider="533w43werwer" finished-at="2018-11-01T23:30:48Z">

I want to return entire <test-method> ... </test-method> for status="FAIL"

Any leads....

5
  • Your file is not valid XML. Commented Nov 13, 2018 at 5:38
  • This is a big payload. I can't upload entire payload here. So I just cut the piece where I need to extract values. Commented Nov 13, 2018 at 5:39
  • I suggest to add a root node in your example. Commented Nov 13, 2018 at 5:40
  • Sure. I done the same. Commented Nov 13, 2018 at 5:42
  • 1
    I suggest to use an XML/HTML parser (xmlstarlet, e.g.). Commented Nov 13, 2018 at 5:52

1 Answer 1

0

You could use the following xmlstarlet and sed command:

xmlstarlet sel -T -t -c "test-results/test-method[@status='FAIL']/params/param/value[contains(.,'testId')]" file | sed -n 's/.*testId=\([^,]\+\),.*/\1/p'

-T: raw test (without XML node).
-t: template
-c: Xpath expression

[@status='FAIL'] is the test on the attribute status.

value[contains(.,'testId')] tests if the value node contains that specific string.

The sed command extracts the wanted string.

Sign up to request clarification or add additional context in comments.

5 Comments

xmlstartlet is not identified in Jenkins
Is it possible to get the current entire XML values using bash command. like the entire <test-method> ...</test-method> for status=FAIL
@ArrchanaMohan You likely need to install the tool on your Jenkins host...
It's remote box. I dnt have access to install tools over there. Pls give me some suggestion to do that. I used below lines but It's not return as expected Example: failedTC=`grep "test-method.*FAIL" results.xml | sed -e 's/^.*test-instance-name="(.*)-----.*/\1/' but Its not return entire <test-method> tags values
Updated the question

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.