I am trying to automate using groovy script. Here is my script. I am not able to mention namspace.
<ns2:contactPref xmlns="namespace 1" xmlns:ns2="name space 2">
<ns2:Information>
<value>Pass</value>
</ns2:Information>
<ns2:contactPreference>
<ns2:contactPointRel>
<contactPoint xs:type="Tele" xmlns:xs="namespace 3">
<cat>mob</cat>
<med>Int</med>
</contactPoint>
</ns2:contactPointRel>
</ns2:contactPreference>
</ns2:contactPref>
Now I want to fetch attribute at contactPoint tag
for this I have tried like this
groovyUtils=new com.eviware.soapui.support.Groovyutills(context)
def xPath=XPathFactory.newInstance().newXPath()
def type=context.expand('${GetResponse#Response#//*:contactPoint/#xs:type}')
log.info type
tried this
def type=context.expand('${GetResponse#Response#//*:contactPoint/#type}')
and I also tried this
def resp=groovyUtils.getXmlHolder("GetResponse#Response")
def type1=(String)xPath.evaluate('//:contactpoint/#xs:type',resp,xPathConstants.STRING)
but no help. please some one tell me how to do this??
try using this it should work
#{namespace 3}type
if you see xs:type="Tele" xmlns:xs="namespace 3" in contactPoint then xs which has value namespace 3 and {type} is Tele
Edited - You could use something like this - with xmlString as XML
def xml = new XmlSlurper().parseText(xmlString)
println
xml.contactPreference.contactPointRel.contactPoint."#{namespace
3}type"
Related
Following groovy script is not working as expected.
def xml="<Collection><CustomerQuote><ID>99988877766</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote><CustomerQuote><ID>99988877755</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote></Collection>"
def completeXml= new XmlSlurper().parseText(xml);
def IDs = completeXml.Collection.CustomerQuote.findAll{node-> node.name() == 'ID' }*.text();
I am trying to copy all the ID value in xml in the IDs
Output
IDs[]
Expected Output
IDs[99988877766,99988877755]
I am not sure what i am doing wrong here.
Can anyone guide.
Thank you
Regards
Prat
The root node has to be omitted when using XmlSlurper, and you don't need to use findAll for this.
def xml="<Collection><CustomerQuote><ID>99988877766</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote><CustomerQuote><ID>99988877755</ID><TypeCode>2059</TypeCode><ApprovalStatusCode>4</ApprovalStatusCode></CustomerQuote></Collection>"
def completeXml= new XmlSlurper().parseText(xml);
def IDs = completeXml.CustomerQuote.ID*.text();
Will output:
[
"99988877766",
"99988877755"
]
Try it in the Groovy Web Console
I am trying to replace a test in pom.xml using a groovy script. These are my two approaches. The text should be replaced is {env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}
Approach one
File mainPomXml = new File(rootDir,'/pom.xml')
mainPomXml.text.replace('{env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}','${env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}');
Approach two
def mainPomXml = new File(rootDir,'/pom.xml')
def mainPom = mainPomXml.text.replace('{env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}','${env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}');
mainPomXml.write(mainPom);
But none of these approaches work. That means both executes but the test is not get replaced. How to fix this issue?
Change the mainPom part as below.
def mainPomXml = new File(rootDir, '/pom.xml')
def mainPom = mainPomXml.text.replace('AM_SERVER_CREDS_USR', '${env.AM_'+ env.toUpperCase() +'_SERVER_CREDS_USR}')
mainPomXml.write(mainPom)
I have a groovy script as the first test step inside a test case, part of it looks like:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder("SampleTestt#Request").getXml()
log.info holder
When SampleTest test step has all element values hardcoded, the request xml can be printed fine.
However if some of the request values is read from a test case property, like the following for example
${#TestCase#Id}
The the above groovy script through error as:
org.apache.xmlbeans.XMLException: error: Unexpected character encountered : '$'
Can you please help?
Thanks.
You can use context.expand() to evaluate the properties inside your request and then parse the result to xmlHolder, your code could looks like:
// get your request replacing the properties inside by their values
def xmlRequest = context.expand('${SampleTestt#Request}')
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(xmlRequest)
log.info holder.getXml()
Note that I use SampleTestt as your test step request name, but I think that the last t could be a typo... check if it's the correct request name before use the code.
Hope this helps,
"I have a code that is working as expected but now I have to find the element in different format. Example is below
<car-load>
<car-model model="i10">
<model-year>
<year.make>
<name>corolla</name>
</year.make>
</model-year>
</car-model>
</car-load>
I have to find the value of "corolla" from this XML. Please reply.
You can run this in the Groovy console
def text = '''
<car-load>
<car-model model="i10">
<model-year>
<year.make>
<name>corolla</name>
</year.make>
</model-year>
</car-model>
</car-load>'''
def records = new XmlSlurper().parseText(text)
// a quick and dirty solution
assert 'corolla' == records.toString()
// a more verbose, but more robust solution that specifies the complete path
// to the node of interest
assert 'corolla' == records.'car-model'.'model-year'.'year.make'.name.text()
I am having 2 test cases in my suite.
First test case contains 1 test step with xml request.
Second test case contains 1 test step with groovy script. I want to run the 1st test case from this groovy script a number of times. Every time I want to change the input XML. I am unable to update the input XML in TestCase 1.
I have the following the code for the groovy script:
import com.eviware.soapui.support.XmlHolder
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def tsuite = testRunner.testCase.testSuite
def acctInq_tstepName = tsuite.getTestCaseAt(1).getTestStepAt(0).getName()
def acctInq_requestHolder = tsuite.getTestCaseAt(1).testSteps[acctInq_tstepName].testRequest.getRequestContent()
def acctInq_req = groovyUtils.getXmlHolder("$acctInq_requestHolder")
acctInq_req["//soapenv:Envelope[1]/soapenv:Body[1]/v2:AcctInqRq[1]/ifx:DepAcctId[1]/ifx:AcctId[1]"] = "0009917812344"
acctInq_req.updateProperty()
I also tried using
tstep.setPropertyValue("request",cStr(acctInq_req))
In either case the XML is not getting updated. Please help.
Try set node value like below and check if it fixed the issue:
acctInq_req.setNodeValue("//*:ifx:AcctId[1]", "0009917812344")