Forum Discussion

SPA-CSW's avatar
SPA-CSW
New Contributor
12 years ago

[Resolved] Create rrequest xml tags with data driven testing

Hi,

I'm currently implementing some data driven tests and i want to dynamically create some xml tags based on the data that comes from the excel file.
For example, if i have data in a specific excel column i want to sent the xml tag with that data, if that column does not have data i do not want to send that specific xml tag in the request.
How can i implement this behavior?
SoapUI Pro already has some functionality that can deal with this type of situations?

Thanks a lot in advance
  • I've managed to solve the problem.
    Basically I used the SoapUI Pro event (RequestFilter.filterRequest), and implemented a simple groovy script that filters the requests before they are sent to the server. So the request is created based on the data that i have in the excel sheet and then the event deletes the xml tags that I don't want to send.
    I created two different event scripts, one for individual XML tags and other for xml nodes. With this approach is possible to have only one xml request template and the users can test all the combinations changing only the data in the excel file.
  • nmrao's avatar
    nmrao
    Champion Level 3
    Not sure about pro features in this regard.

    I believe that it needs to understand that how mapping between datasource columuns, and request happens. Does one needs to write any code or uses some other mechanism?

    If groovy is used for mapping and update the request for each rung, then, i believe, it should be possible what are you looking for.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    As Rao is suggesting, you need to write some kind of Groovy Script that transformes the data extracted from the Excel sheet to XML,
    if the XML is not already defined in the WSDL.
    Maybe you could give us an example, or even better, provide us with a sample project so we could get a clearer understanding of what you are trying to achive.


    --
    Regards

    Erik
    SmartBear Sweden
  • SPA-CSW's avatar
    SPA-CSW
    New Contributor
    I've managed to solve the problem.
    Basically I used the SoapUI Pro event (RequestFilter.filterRequest), and implemented a simple groovy script that filters the requests before they are sent to the server. So the request is created based on the data that i have in the excel sheet and then the event deletes the xml tags that I don't want to send.
    I created two different event scripts, one for individual XML tags and other for xml nodes. With this approach is possible to have only one xml request template and the users can test all the combinations changing only the data in the excel file.
  • nmrao's avatar
    nmrao
    Champion Level 3
    Glad you could resolve it. Any thing(sample script) that can be shared with community?
  • URN123's avatar
    URN123
    Occasional Contributor
    I am also trying to do something similar...Would you mind sharing your groovy scripts here?
  • Critical_Softwa's avatar
    Critical_Softwa
    Occasional Contributor
    Below are the scripts used to solve the problem described above based on my specific needs.

    Groovy script used to remove specific XML tags based on the data presented in the excel sheet:

    import com.eviware.soapui.support.xml.XmlUtils

    def request = context.expand( '${--TEST-STEP-NAME--#Request}' )
    def parsedXml = new XmlParser().parseText(request)
    def buffer = new StringWriter()
    new XmlNodePrinter(new PrintWriter(buffer)).print(parsedXml)
    def filteredRequest = buffer.toString()
    filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
    filteredRequest = filteredRequest.replaceAll( "<(.+)>--REMOVE--<(.+)>", "")
    filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
    context.requestContent = filteredRequest


    XML tags that have the string “—REMOVE--” will be deleted from the original request. The “—REMOVE--” string and all other necessary data comes from the excel sheet and is used to create a test request, then this script is called by the soapui event RequestFilter.filterRequest to filter the request contents.


    Groovy script used to remove XML nodes based on the data presented in the excel sheet:
    import com.eviware.soapui.support.xml.XmlUtils

    def nodeRemoveFlag = context.expand( '${DataSource#removeXmlNode}' )

    if ( nodeRemoveFlag == 'Yes'){
    def request = context.expand( '${--TEST-STEP-NAME--#Request}' )
    def parsedXml = new XmlParser().parseText(request)
    def nodeToRemove = parsedXml.children()
    def nodeFind = parsedXml.find { it.text() == '--XML-NODE-TO-REMOVE--' }
    nodeToRemove.remove(nodeFind)
    def buffer = new StringWriter()
    new XmlNodePrinter(new PrintWriter(buffer)).print(parsedXml)
    def filteredRequest = buffer.toString()
    filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
    filteredRequest = XmlUtils.prettyPrintXml(filteredRequest)
    context.requestContent = filteredRequest
    }


    If the remove flag is set to “Yes” in the excel sheet it's possible to use this script to remove a specific xml node. In this case the node that can be removed it’s hard coded on the script, but if needed the script can easily be adapted to remove different xml nodes based on the data driven approach.

    Regards
    Tiago Neto Rodrigues
    • desh's avatar
      desh
      New Contributor

      Similar to above discussed case I have problem in creating tags dynamically, The problem I have is repeating a tag multiple times dynamically based on the input. During creating a reservation if the input is 2 adults I have to repeat person info twice and populate values for those and then hit the server.

       

      Example..

       

      <rad1:Passengers>
      <!--Zero or more repetitions:-->
      <rad1:Person>
      <rad1:PersonOrgID>-214</rad1:PersonOrgID>
      <rad1:FirstName>Rikant0</rad1:FirstName>

      <rad1:Person>

      <rad1:Passengers>

       

      I have to repeat the Person block inside Passengers Block dynamically based on the number of passengers. 

       

      Please provide solution on this...

       

      • nmrao's avatar
        nmrao
        Champion Level 3

        Would suggest you to open a new topic along the details as much as possible as it is recommended that not to cross post.