cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic request creation with Groovy - element Create

SOLVED
Highlighted
Contributor

Dynamic request creation with Groovy - element Create

As per my project, i have to create part of SOAP request as per input data, 

I have developed below code and not able to get, could some one help me.

 

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil

class DR{
//This Method is used to Create DRequestElement element as per request
def DRE(def accountdetails){

// change the account string to list
def acclist = accountdetails.tokenize(',');
def builder_acc = new StringBuilder()
for (item in acclist){
builder_acc.append("RequestElement{");
builder_acc.append( "accountnumber(")
builder_acc.append(item);
builder_acc.append( ")");
builder_acc.append( "}");
}

return builder_acc;

}
}

def createRequest(def accountNumbers){

//Define all your namespaces here
def nameSpacesMap = [
soapenv: 'http://schemas.xmlsoap.org/soap/envelope/',
ns: 'ABC',
]

def builder = new StreamingMarkupBuilder()
builder.encoding ='utf-8'
def soapRequest = builder.bind {
namespaces << nameSpacesMap

soapenv.Envelope {
soapenv.Header{}
soapenv.Body {
//example for element attribute
ns.TOPPER{
RequestHeader{
MessageId("ABC");
}
new DR().DRE(accountNumbers);
}
}
}
}
return XmlUtil.serialize(soapRequest);
}

def accountdetails = "84144135,84023193";

log.info createRequest(accountdetails);

 

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="ABC">
<soapenv:Header/>
<soapenv:Body>
<ns:TOPPER>
<RequestHeader>
<MessageId>ABC</MessageId>
</RequestHeader>
<RequestElement>
<accountnumber>84144135</accountnumber>
</RequestElement>
<RequestElement>
<accountnumber>84023193</accountnumber>
</RequestElement>
</ns:TOPPER>
</soapenv:Body>
</soapenv:Envelope>

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Dynamic request creation with Groovy - element Create

Here is the script:

 

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
def createRequest = {accountNumbers ->
def nameSpacesMap = [
soapenv: 'http://schemas.xmlsoap.org/soap/envelope/',
ns: 'ABC',
]
def builder = new StreamingMarkupBuilder()
builder.encoding ='utf-8'
def soapRequest = builder.bind {
namespaces << nameSpacesMap
soapenv.Envelope {
soapenv.Header{}
soapenv.Body {
ns.TOPPER{
RequestHeader{
MessageId("ABC")
}
accountNumbers.each { actNo ->
RequestElement {
accountnumber(actNo)
}
}

}
}
}
}
XmlUtil.serialize(soapRequest)
}
def accountdetails = [84144135,84023193]
log.info createRequest(accountdetails)

 

The same can be tried from here



Regards,
Rao.

View solution in original post

8 REPLIES 8
Highlighted
Community Hero

Re: Dynamic request creation with Groovy - element Create

Not sure of the exact problem.

Please specify what is the source of input and input data format?

And what is the expected output?


Regards,
Rao.
Highlighted
Contributor

Re: Dynamic request creation with Groovy - element Create

In the below Groovy Script, I need to loop RequestElement element as per input,

I am passing Account details to method and should create out with number Account for RequestElement.

 

import groovy.xml.StreamingMarkupBuilder

import groovy.xml.XmlUtil

def createRequest(def accountNumbers){

//Define all your namespaces here
def nameSpacesMap = [
soapenv: 'http://schemas.xmlsoap.org/soap/envelope/',
ns: 'ABC',
]
def builder = new StreamingMarkupBuilder()
builder.encoding ='utf-8'
def soapRequest = builder.bind {
namespaces << nameSpacesMap

soapenv.Envelope {
soapenv.Header{}
soapenv.Body {
//example for element attribute
ns.TOPPER{
RequestHeader{
MessageId("ABC");
}
RequestElement{
accountnumber("84144135");
}
RequestElement{
accountnumber("84023193");
}
}
}
}
}
return XmlUtil.serialize(soapRequest);
}
def accountdetails = "84144135,84023193";
log.info createRequest(accountdetails);


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="ABC">
<soapenv:Header/>
<soapenv:Body>
<ns:TOPPER>
<RequestHeader>
<MessageId>ABC</MessageId>
</RequestHeader>
<RequestElement>
<accountnumber>84144135</accountnumber>
</RequestElement>
<RequestElement>
<accountnumber>84023193</accountnumber>
</RequestElement>
</ns:TOPPER>
</soapenv:Body>
</soapenv:Envelope>

Highlighted
Community Hero

Re: Dynamic request creation with Groovy - element Create

Can you check the below link if that is helpful?
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/BuildSoapRequestFromCsv.groovy


Regards,
Rao.
Highlighted
Contributor

Re: Dynamic request creation with Groovy - element Create

Hi nmRao Good Morning,

 

thanks for your link, But i am looking part the Request need to repeat as per input, I followed your one of the link

http://groovyconsole.appspot.com/script/5169502497013760. But no luck. While create SOAP Request i need to call another method to repeat.

 

Thanks

 

Highlighted
Community Hero

Re: Dynamic request creation with Groovy - element Create

Here is the script:

 

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
def createRequest = {accountNumbers ->
def nameSpacesMap = [
soapenv: 'http://schemas.xmlsoap.org/soap/envelope/',
ns: 'ABC',
]
def builder = new StreamingMarkupBuilder()
builder.encoding ='utf-8'
def soapRequest = builder.bind {
namespaces << nameSpacesMap
soapenv.Envelope {
soapenv.Header{}
soapenv.Body {
ns.TOPPER{
RequestHeader{
MessageId("ABC")
}
accountNumbers.each { actNo ->
RequestElement {
accountnumber(actNo)
}
}

}
}
}
}
XmlUtil.serialize(soapRequest)
}
def accountdetails = [84144135,84023193]
log.info createRequest(accountdetails)

 

The same can be tried from here



Regards,
Rao.

View solution in original post

Highlighted
Community Hero

Re: Dynamic request creation with Groovy - element Create

@babusr01, have you got chance to try the solution provided in earlier reply?



Regards,
Rao.
Highlighted
Community Hero

Re: Dynamic request creation with Groovy - element Create

@babusr01, looks you never comeback for the issues, just you let others spend on your problem, not good.


Regards,
Rao.
Highlighted
Contributor

Re: Dynamic request creation with Groovy - element Create

Sorry Rao,

 

the solution which you provided is worked me. thanks for you help.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors