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

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84

1 Answers1

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.

oliv
  • 12,690
  • 25
  • 45