Ask a Question

DataSource Loop with variable number of child elements

SOLVED
ripkurrle
Occasional Contributor

DataSource Loop with variable number of child elements

Hi

I've just started using ReadyAPI 3.4.5 after being on SoapUI for a while.

I'm testing an external vendor API and trying to pass in all my scenario data through one DataSource, ideally.

My problem is that each of the test cases has a different number of elements applied to a complex element, e.g. Test 1 may have the following elements for specifying contributions

...

<contributions>

  <contribution>

    <type>Single</type>

    <amount>1000</type>

  </contribution>

    <type>Regular</type>

    <amount>100</type>

    <startDate>1-1-2020</startDate>

  </contribution>

...

The next test might only have one Single contribution defined. There are a few complex elements that I adjust for each test and have a similar problem.

 

I've set the flag for "Remove Empty Content" to try and remove any elements where nothing is passed in from the DataSource but this leaves the parent node empty "<contribution></contribution>" which unfortunately breaks the test.

 

I've researched ways that this might be able to be resolved using Events with RequestFilter.filterRequest but the supplied groovy script doesn't appear to work well with DataSource data, instead it passes the property name in as a string to the request, e.g. ${DataSource#Single-Amount}

See section 4.5 in https://www.soapui.org/docs/scripting-and-properties/tips-tricks/ and https://support.smartbear.com/readyapi/docs/testing/scripts/samples/xml.html

Seems like a similar issue was raised here but the user didn't confirm whether it fixed their problem or not

https://community.smartbear.com/t5/API-Functional-Security-Testing/Remove-Empty-Content/td-p/18426 

 

The only way I can think of getting around this is not using the DataSource at all and, instead, crafting each test step as a specific test. I'm not a tester by profession so perhaps this is the way more seasoned testers craft tests when they have these limitations, I just wanted to control the test data from the one view point.

 

I also thought a RegEx over the expanded request, with DataSource data populated in, just before it gets sent I could replaceAll some of the fields but couldnt work out an event or script to get the XML at this point.

 

Cheers

Alan

6 REPLIES 6
richie
Community Hero

Hey @ripkurrle,

I wouldve picked the same event handler you did to handle the empty root element, but ive never tried events on datasourced data before.

Rather than have to go with the individual test data approach on each test step, im wondering if perhaps your datasource step passed the data to a datasource grid (as an intermediate step before being sourced by the Rest step, perhaps this would work? I know the datasource grid is a datasource step, but it might work?

I.e. datasource step &gt;&gt; datasource grid (with expanded data) event handler processes this data &gt;&gt; REST step sources data from datasource grid

Or, i'm wondering if you have a datasource that writes to a properties file, groovy step to remove the empty elements and then the REST step (sourcing the data from the Properties step via ${Properties#testdata}) to submit the request which doesnt contain any empty elements? Actually id do it this way i think, so my test step object hierarchy would be as follows:

Datasource step
Properties step
Groovy step
Properties2 step (optional)
REST step

Groovy step would contain something like the following (code = courtesy of


def xmlString = """<PROFILE> <NAME>Fin</NAME> <AGA>20</AGA> <CONTACT> <MOBILE>1819</MOBILE> <TELEPHONE></TELEPHONE> </CONTACT> <TEAM> <TEAM1></TEAM1> <TEAM2></TEAM2> </TEAM> </PROFILE>"""

def xml = new XmlParser().parseText(xmlString) def nodes = xml.'**'.findAll{it.name() &amp;&amp; !it.text()} def removeNode = { node -&gt; def parent = node.parent() parent.remove(node) } nodes.each{removeNode(it)} println groovy.xml.XmlUtil.serialize

Ta

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
ripkurrle
Occasional Contributor

Thanks Rich, I'll give it a go.

TanyaYatskovska
SmartBear Alumni (Retired)

Thanks for the help, richie!

@ripkurrle, was the issue resolved? Please let us know if you need more help.

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



ripkurrle
Occasional Contributor

Apologies, I was on leave last week.

 

I'm putting together a simpler isolated test against this API to prove richie's theory.

ripkurrle
Occasional Contributor

Unfortunately, I cant find a public SOAP API that accepts an optional number of inputs to make this easier to see.

ripkurrle
Occasional Contributor

Hi,

I've found an easier scenario I can test this on and setup the test case to use a DataSource type Grid (similar to my original test case with 5 attributes and one of those optional) and other steps below.

 

@richie Apologies but I'm unsure what to do with the properties steps, still new to this. The Groovy script appears to need the request just before its sent out the door to remove those empty elements but I'm unsure how a Property Step (or Property Transfer?) would help get it into this state.

 

ripkurrle_0-1606749315762.png

Get MLAs List service currently returns an empty parent tag after the empty mlaid element was removed by the case level remove empty element flag.

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:GetMlasList>
         <ns:mlas>
            <ns:mla>
               <ns:mlaid>2</ns:mlaid>
            </ns:mla>
            <ns:mla>
               
            </ns:mla>
         </ns:mlas>
         <!--Optional:-->
         <ns:activeOnly>Y</ns:activeOnly>
         <!--Optional:-->
         <ns2:pageSize>10</ns2:pageSize>
         <!--Optional:-->
         <ns2:rowNumber>1</ns2:rowNumber>
      </ns:GetMlasList>
   </soapenv:Body>
</soapenv:Envelope>

 

 

Here is a picture of the two scenarios in the Grid, second one has the empty element you can see above.

ripkurrle_0-1606752926938.png

 

 

Cheers

Alan

cancel
Showing results for 
Search instead for 
Did you mean: