Forum Discussion

mkalboneh's avatar
mkalboneh
Occasional Contributor
11 years ago

Remove xml Node using Groovy Script

Let's say we have the following example XML :


<cars>
<my:car>
<ns:make>Honda</ns:make>
<ns:model>Civic</ns:model>
<ns:year>2012</ns:year>
<ns:paint>
<ns:color>red</ns:color>
<ns:color>blue</ns:color>
<ns:paint>
</my:car>

</cars>


I would like to be able to delete all <ns:color> nodes from a test step request using a groovy script step in soapUI. Any help?

6 Replies

  • mkalboneh's avatar
    mkalboneh
    Occasional Contributor
    Thank you for the reply nmrao,

    I can't provide exact xml because it's for work. What I can do is describe the flow:

    I have a soap UI test that consist of the following steps:

    1- "Add Cars" step will send an add cars request, then come back with a response with IDs for the cars added
    2- Groovy step that extract those IDs from the first step response, then append them to the next step request
    3- This step will call with Car IDs to verify that cars were added

    Now, I would like to run those steps over and over for load testing purposes. The problem is:
    That request for the third step (verification) where I am appending the IDs, still contain the IDs from the last time it ran.

    I need to add to the groovy script in step 2, so it checks and deletes all existing car IDs in the third step request, so they won't be sent again for verification.
    So every time that third step will run, it only contains IDs for cars that were just added during this run.

    The xml provided is just an example, but it does describe my end goal here.
    So, I would like to be able to remove those nodes iteratively throughout the whole request before appending the new ones.
    <ns:color></ns:color>


    I hope this helps a little answer my question.
    Thanks You!
  • nmrao's avatar
    nmrao
    Champion Level 3
    It is understand and not expected to post your work related data, but the issue may get addressed by some one if the details are enough to provide you valuable to suggestions. Please do not get me wrong.

    Coming to the issue:
    1. When add car request is added, you may add script assertion to the response itself so that it extracts the generated ID from the response and set as property to test case/testsuite/project level based on your need.
    Here is how you can set a project property


    //do an xpath query result on response of car id and assign to a variable say ID
    // this link will help you here to extract 'car id' from your add car response
    /*
    [url]
    http://www.soapui.org/Functional-Testing/script-assertions.html
    [/url]*/
    project.setProperty('CARID', ID)


    2. In the verification step, use above property as value while sending query with newly created car ids
    For eg: <carId>${#Project#CARID}</carId>

    There should not be any issue while running load test now.
    This way you may be able to achieve what you are looking for.
  • mkalboneh's avatar
    mkalboneh
    Occasional Contributor
    Hello,

    Thank you for your reply!
    Here is where I am with this issue:

    I was able to create the Groovy script to extract all the
    <ns:color>
    tags from the Add Cars Response.
    I was also able to append them into the request for the verification step, All using a single groovy step script.

    So my test runs fine as far as the test it self. When I run my test, I can add a list of cars, get their creation Ids, append them into the request for verification (using my awesome script)
    Then finally send verification request. So I am in good shape here.

    All I really need for that script (to be more awesome) is to remove ALL existing
    <ns:color>
    nodes in the request before adding new ones, No matter where they are.
    At this point, I do that manually every time I am about to run my test.
    If I use a property transfer step, I believe it will only take one value; where in my case I don't know how many car Ids I get back. It will be different every time the test is run.

    I have tried doing that using xmlSlurper, which works great for everything I need! Except that xmlSlurper will not let me call the Remove() on the nodes I need removed.

    Something like this flow would do the job:

    def root = (the request xml we want to add to)
    root.each{
    // If it.name() == "<ns:color>";
    root.remove(it)
    }


    I hope this simplifies things a little.
  • mkalboneh's avatar
    mkalboneh
    Occasional Contributor
    Just a follow up on this matter:

    I was able to use the following groovy code to remove the nodes I want:


    // get XmlHolder for request
    def reqHolder = groovyUtils.getXmlHolder( "TestStepName#Request" )

    // Remove all <<ns:color>> nodes from the TestStep request
    reqHolder.removeDomNodes("//<ns:color>")


    I think the issue I ran into was trying to remove nodes from the xmlSlurper object which does not allow for removing nodes.
    Hope this will help.