PathNotFoundException in the groovy script SoapUI - groovy

While executing this script in the line 2, error message is getting, instead of null response.
If there is no "success_text" in the response, it should show null value instead of error.
def response = context.expand( '${moneytransfer#Response}' )
def id =parse(response).read('$.success_text')
log.info id
Could you please help me, is there anyway "id" value returns null, if the success_text not found

Since you don't share the parse and read method it's hard to know what is happening under the hood. However in groovy there is a null-safe operator ?., it avoids NPE returning null instead.
I suspect that the problem is inside your methods, however you can try with it:
def response = context.expand( '${moneytransfer#Response}' )
def id =parse(response)?.read('$.success_text')
log.info id

Related

SOAPUI Square brackets around my actual results causing assert to fail

I am writing a Groovy script assertion which is verifying a value from a previous JDBC response step against a value contained in a SOAP response.
When I run my scripts I can see that both values are coming back the same but the actual result value (from the SOAP response) is surrounded by square brackets which in turn makes the assert fail. I'm guessing this is something to do with one being a string and one not?
How do I either strip out the square brackets from actual result or get them added to the expected result value to ensure the assert passes?
Below is my assert script.
Expected result is 001
Actual result is [001]
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def pxml = new XmlSlurper().parseText(context.response)
//grab the expected result from jdbc response
def expectedCodes = context.expand( '${JDBC Request#ResponseAsXml#//*:TW304_PRODHIST.PRODUCT_1}' )
//grab the actual result from the SOAP response
def actualCodes = pxml.'**'.findAll{it.name() == 'CurrHospProductCode'}*.text()
assert expectedCodes == actualCodes
log.info expectedCodes
log.info actualCodes
Because you are expecting a single value as expected where as you are getting an array with single element in it.
You can do it as below if that is right :
assert expectedCodes == actualCodes[0]
On a side note, you may have to check carefully are you really expecting single value only or if there is possible to get list of values.
EDIT: based on your script.
findAll gives you list as result. If you are expecting single element in the xml, then you can change it to find and then you actual code should work as it is.

using an assert with a contains in groovy

I am trying to run an assert with contains but am encountering an issue. I have written the code below using groovy in SOAPUI Pro
def pieceid = context.expand( '${OneDX#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/PIECEID[1]}' )
def TrackingNumber = context.expand( '${OneDX#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/TRACKINGNUMBER[1]}' )
assert {!TrackingNumber.contains(Pieceid)}
The tracking number is 907598985733 and Pieceid is 1820480....therefore the Pieceid is not in the tracking number. However when I run the script it passes. do you know what i'm doing wrong
Looks like a trivial issue in this case.
Changes:
replace ResponseAsXml with Response
removed { .. } in the assert statement, and introduced ( .. )
looks, you used incorrect variable i.e., Pieceid which is also not available - incorrect case.
Here is you go with the changed groovy script snippet:
def pieceid = context.expand( '${OneDX#Response#//Results[1]/ResultSet[1]/Row[1]/PIECEID[1]}' )
def trackingNumber = context.expand( '${OneDX#Response#//Results[1]/ResultSet[1]/Row[1]/TRACKINGNUMBER[1]}' )
log.info "Tracking number is $trackingNumber and Piece Id is $pieceid"
assert (!trackingNumber.contains(pieceid)), "Tracking number contains Pieceid"
You should be able to see the data of both variables in the log as well.
I would also like to recommend you not to use indexes in the xpath. Understand that might be auto generated by the tool. The reason being that if nodes come in different order, that will break your existing assertions for later test executions.

SoapUI, temper request data break when value is read from property file

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,

Setting json key using bind variables in groovy

I've this code by which I am trying to set the value of a Key (a json node id). But its not setting the value. Log.info statement is showing right values.
Key= context.expand( '${#Project#key}' )
Value= context.expand( '${#Project#value}' )
Binding binding = new Binding()
binding.setVariable("v", "$Value")
binding.setVariable("k", "$Key")
log.info(binding.getVariable("v")) // gives me the value 1234
log.info(binding.getVariable("k")) // gives me the value request.id
def SetKey = new GroovyShell(binding).evaluate( "k=v")
Can someone please comment on whats wrong in this code. and how can I correct it.
Edit: Explanation of the issue
In SoapUI I've some json nodes saved in data source like this request.id and request.app.id and there expected values in Value column which I am fetching through Key and Value above. I am hoping to change the value of a json node to its respective value at run time. So for each iteration of data source, it should set the correct value of that particular json node. Befor the above code I've parsed my json request by json slurper and after the above code I am building the json again by Json builder and running the request. Parsing and running the request works fine, its just that I couldnt set the value of the json node.
If your keys are just dotted names, you could simply use some string manipulation, no need to involve the Groovy parser.
For example, if they all begin with request:
def steps = Key.split(/\./)
if (steps.size() < 2 || steps[0] != 'request') {
throw ...
}
def obj = request
if (steps.size() > 2) {
steps[1..-2].each {
obj = obj[it]
}
}
obj[steps[-1]] = Value

groovy - problem parsing xml

I am new to Groovy and I am trying to parse both a valid rest resource and an invalid one.
For example:
this code works fine -
def entity = new XmlSlurper().parse('http://api.twitter.com/1/users/show/slashdot.xml')
println entity.name()
println entity.screen_name.text()
when I run it, I get output:
user
slashdot
but when I pass an invalid url to xmlSlurper, like this
def entity = new XmlSlurper().parse('http://api.twitter.com/1/users/show/slashdotabc.xml')
println entity.name()
println entity.screen_name.text(
)
I get this error message:
Caught: java.io.FileNotFoundException: http://api.twitter.com/1/users/show/slashdotabc.xml
at xmltest.run(xmltest.groovy:1)
Although the url returns an hash code (like below) with an error message which I would like to parse and display it.
<hash>
<request>/1/users/show/slashdotabc.xml</request>
<error>Not found</error>
</hash>
How can I parse a url which returns a 404 but with error information?
Any help will be appreciated.
--
Thanks & Regards,
Frank Covert
The response you want to see is available on the URL connection's errorStream instead of the inputStream. Fortunately, given the InputStream, XmlSlurper.parse can read an InputStream as well.
Here's a sample to switch to reading the errorStream when you don't get a 200 status:
def con = new URL("http://api.twitter.com/1/users/show/slashdotaxxxbc.xml").openConnection()
def xml = new XmlSlurper().parse(con.responseCode == 200 ? con.inputStream : con.errorStream)

Resources