Forum Discussion

smoothe19's avatar
smoothe19
New Contributor
12 years ago

[Resolved] GroovyScript updating XML

This is a soapUI load test and a REST service request with a media Type of: text/plain;charset=UTF8

I have a payload that is as follows
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<Header>
</Header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<Invoice>
</Invoice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>



It is then altered by the following groovy script
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("InvoiceTestRequest1#request")

Random r = new Random()
def refnum = r.nextInt(999999)
while (refnum < 100000) {
refnum = r.nextInt(999999)
}

def cor = r.nextInt(999999)
while (cor < 100000) {
cor = r.nextInt(999999)
}


def mes = r.nextInt(999999)
while (mes < 100000) {
mes = r.nextInt(999999)
}

holder.setNodeValue( "//Header/MessageId" , ""+mes);
holder.setNodeValue( "//Header/CorrelationId" , ""+cor);

holder.updateProperty()


However by the time the groovy script is done executing the "<?xml version="1.0" encoding="UTF-8"?>" in the payload request is stripped away and it causes the request to fail against my proxy service. How may I prevent ths from being removed or add it back to the top of my xml?

1 Reply

  • smoothe19's avatar
    smoothe19
    New Contributor
    Hi I resolved my own issue after 3 days lol...

    Incase anyone else encounters the xml pragma being stripped here is what i did to the groovy..


    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def holder = groovyUtils.getXmlHolder("InvoiceTestRequest1#request")

    Random r = new Random()
    def refnum = r.nextInt(999999)
    while (refnum < 100000) {
    refnum = r.nextInt(999999)
    }

    def cor = r.nextInt(999999)
    while (cor < 100000) {
    cor = r.nextInt(999999)
    }


    def mes = r.nextInt(999999)
    while (mes < 100000) {
    mes = r.nextInt(999999)
    }

    holder.setNodeValue( "//Header/MessageId" , "LoadTest"+mes);
    holder.setNodeValue( "//Header/CorrelationId" , "LoadTest"+cor);
    holder.setNodeValue( "//Invoice/InvoiceHeader/InvoiceReferences/PurchaseOrderReference/PurchaseOrderNumber/Reference/RefNum" , "LoadTest"+ refnum);

    def input = '<?xml version="1.0" encoding="UTF-8"?> ' + holder.xml;
    //holder.updateProperty()
    testRunner.getTestCase().getTestStepByName("InvoiceTestRequest1").getProperty('Request').setValue(input)
    //context.requestContent = input