TruClient - Evaluate JS on Object to return array

940 views Asked by At

My application has a table with a "Status" column. I am using "Evaluate JS on Object" to scan the column to determine if any of the rows is in "Pending" status and take the appropriate action. The object identification works fine, as TruCLient highlights all items in the column:

Multiple objects Identified

What I am trying to do is similar to web_reg_save_param in the HTTP protocol that returns an array when more than one match exists. The problem is that, instead of returning an array of objects, TruClient fails with "multiple objects were found that are similar to the target object".

<table>
<thead>
    <tr>
        <th>Details</th>
        <th>Number</th>
        <th>Status</th>
        <th>Date</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><a href="/001">View</a></td>
        <td>20-001</td>
        <td>Completed</td>
        <td>01/01/2020</td>
    </tr>
    <tr>
        <td><a href="/002">View</a></td>
        <td>20-002</td>
        <td>Cancelled</td>
        <td>01/02/2020</td>
    </tr>
    <tr>
        <td><a href="/003">View</a></td>
        <td>20-003</td>
        <td>Cancelled</td>
        <td>01/03/2020</td>
    </tr>
</tbody>
1

There are 1 answers

0
user19736252 On

TruClient step operates on a single object. In case you would like to iterate over the elements and perform an action for each object, it would require multiple script steps:

  1. Find out how many elements exist with the required status - you could use an "Evaluate JavaScript" step to evaluate an XPath with count and store it to variable. Something like:

     const evalResult = document.evaluate('count(//td[text()="Pending"])', 
     document, null, XPathResult.ANY_TYPE, null);
     var statusCount = evalResult.numberValue;
    
  2. Add a for loop step from 1 to statusCount (including).

     var i = 1
     i <= statusCount
     ++i
    
  3. Inside the for loop step, add 'Generic Object Action' step with JavaScript ID Method and the following code

     evalXPath("(//td[text()=\"Pending\"])[" + ArgsContext.i + "]");
    

ArgsContext.i allows you to access variable "i" defined in the for loop from within Object Identification section of the step.