Search code examples
shelljenkinsxmllint

How to fetch the specific values in the html file using shell script


I have a html file that will be generated based on the test suite execution. It contains test run details like how many test cases are passed and failed in the specific run. I'm very new to shell script and not sure how to extract the value from the .html that's been generated in the jenkins $Workspace.

HTML snippet : (Copied only specific tag instead of copying the entire .html file):

<ul class="quick-summary--list---2_80W">
    <li class="quick-summary--item---bfSQ0 quick-summary--passes---3IjYH" title="Passed">
       <button type="button">
           <i class="material-icons quick-summary--icon---TW1oG quick-summary--circle-icon---1HDS7"></i>
             9
      </button>
    </li>
   <li class="quick-summary--item---bfSQ0 quick-summary--failures---14s29" title="Failed">
       <button type="button">
          <i class="material-icons quick-summary--icon---TW1oG quick-summary--circle-icon---1HDS7"></i>
              0
       </button>    
    </li>
</ul>

In the above sample .html file I'm looking the number in the below xpath:

.//*[@title='Passed']/button  // It should return 9 

.//*[@title='Failed']/button  // It should return 0

How can I get these number from the html file which is generating the values in the above specific XPATH.

Please help me to achieve this.

Updated:

Tried below one but It return empty:

$ xmllint --html  --xpath "//*[@title='Passed']/button" result.html
XPath set is empty

Solution

  • The problem seems to occur because of the unusual characters in the<i>nodes in your html snippet (not sure what this are).

    One somewhat convoluted way to get around the characters is this:

    xmllint --html  --xpath "substring-after(normalize-space(//li[@title='Passed']/button),' ')"   result.html
    

    and

    xmllint --html  --xpath "substring-after(normalize-space(//li[@title='Failed']/button),' ')"   result.html
    

    Outputs are indeed 9 and 0.