I want to write a code for Response assertion using groovy script , for the Response data like this:
[
{
"fieldId":"947bb60f",
"id":"e7b8ad2b",
"name":"field",
}
]
Tried using the below groovy script for which i am getting error(failure message).
if (!jsonResponse.keySet().containsAll(["fieldId","id","name"] )) {
failureMessage += "The json response body has wrong structure or error msg.\n\n";
}
The same script working fine with the single tree structure as below. Appreciate your help on this with groovy script .
[
"fieldId":"947bb60f",
"id":"e7b8ad2b",
"name":"field",
]
So, you are getting a list of items returned (containing a single item)
Assuming you never expect more than one item, you can check the size of it with:
if (jsonResponse.size() != 1) {
failureMessage += "Expected one item, got ${jsonResponse.size()}.\n\n";
}
Then, you can grab the first element with:
def jsonElement = jsonResponse[0]
And check the field names with:
if (jsonElement.keySet() != ["fieldId","id","name"] as Set) {
failureMessage += "Unexpected fields in response ${jsonElement.keySet()}.\n\n";
}
Related
Please don't hate me, yes I want to do something really stupid.
I want to get null on every attribute if it does not exist. I found out that I can create the propertyMissing method:
class User {
String name = "A"
}
Object.metaClass.propertyMissing() {
null
}
u = new User();
println u?.name
println u?.namee
This prints:
A
null
Now I have the "great" Hybris system in my back :D
If I add the propertyMissing part on top of my script and run this in the Hybris groovy console, I still get the MissingPropertyException.
Is there another way to avoid the MissingPropertyException exception without having to work with hundreds of try catch? (or hundreds of println u?.namee ? u.namee : null isn't working)
/Edit: 1
I have the following use case (for the Hybris system):
I want to get all necessary information in a dynamic output from some pages. Why dynamic? Some page components have the attribute headline other teaserHeadline and some other title. To avoid to create each time an try catch or if else, I created a function which loops through possible attributes and if it's null it skips that one. For that I need to return null on attributes which doesn't exist.
Here is an example which should work, but it doesn't (don't run it on your live system):
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.SearchResult;
flexibleSearch = spring.getBean("flexibleSearchService")
FlexibleSearchQuery query = new FlexibleSearchQuery("select {pk} from {ContentPage}");
SearchResult searchResult = flexibleSearch.search(query);
def i = 0;
def max = 1;
searchResult.result.each { page ->
if (i < max) {
gatherCMSPageInformation(page)
}
i++;
}
def gatherCMSPageInformation(page) {
page.class.metaClass.propertyMissing() {
null
}
println page.title2
}
Weird thing is, that if I run it a few times in a small interval, it starts to work. But I can't overwrite "null" to something else like "a". Also I noticed, to overwrite the Object class isn't working at all in Hybris.
/Edit 2:
I noticed, that I'm fighting against the groovy cache. Just try the first example, change null with a and then try to change it again to b in the same context, without restarting the system.
Is there a way to clear the cache?
why don't you use the groovy elvis operator?
println u?.namee ?: null
I have the below Groovy script which i need to place it in centralized Groovy Library and then access the class mentioned in the Groovy from any script in my Ready API Project
Path: D:\GroovyLib\com\Linos\readyapi\util\property\propertyvalidation
//Change the name of the Properties test step below
def step = context.testCase.testSteps['Properties']
//Parse the xml like you have in your script
def parsedXml = new XmlSlurper().parse(file)
//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry -> map[entry.ErrorCode.text()] = entry.Description.text(); map }
log.info "Error details from response : ${errorDetails}"
def failureMessage = new StringBuffer()
//Loop thru properties of Property step and check against the response
step.properties.keySet().each { key ->
if (errorDetails.containsKey(key)) {
step.properties[key]?.value == errorDetails[key] ?: failureMessage.append("Response error code discription mismatch. expected [${step.properties[key]?.value}] vs actual [${errorDetails[key]}]")
} else {
failureMessage.append("Response does not have error code ${key}")
}
}
if (failureMessage.toString()) {
throw new Error(failureMessage.toString())
}
I have created the code in Library as below:
package com.Linos.readyapi.util.property.propertyvalidation
import com.eviware.soapui.support.GroovyUtils
import groovy.lang.GroovyObject
import groovy.sql.Sql
class PropertyValidation
{
def static propertystepvalidation()
{
//Change the name of the Properties test step below
def step = context.testCase.testSteps['Properties']
//Parse the xml like you have in your script
def parsedXml = new XmlSlurper().parse(file)
//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry -> map[entry.ErrorCode.text()] = entry.Description.text(); map }
log.info "Error details from response : ${errorDetails}"
def failureMessage = new StringBuffer()
//Loop thru properties of Property step and check against the response
step.properties.keySet().each { key ->
if (errorDetails.containsKey(key)) {
step.properties[key]?.value == errorDetails[key] ?: failureMessage.append("Response error code discription mismatch. expected [${step.properties[key]?.value}] vs actual [${errorDetails[key]}]")
} else {
failureMessage.append("Response does not have error code ${key}")
}
}
if (failureMessage.toString()) {
throw new Error(failureMessage.toString())
}
I am not sure what to mention in the def static method. I am new to this process and haven't done that yet. Can someone please guide me! I've read the documentation on Ready API! website. But I'm not clear on that.
ReadyAPI allows users to create libraries and put them under Script directory and reuse them as needed.
Please note that, ReadyAPI does not allow to have a groovy script in the Script directory, instead it should be Groovy classes.
Looks you were trying to convert a script, answered from one of the previous questions, to class.
Here there are certain variables available in Groovy Script by SoapUI. So, those needs to passed to library class. For instance, context, log are common.
And there can be some more parameters related to your method as well. In this case, you need to pass the file, property step name etc.
Here is the Groovy Class which isconverted from the script. And the method can be non-static or static. But I go with non-static.
package com.Linos.readyapi.util.property.propertyvalidation
class PropertyValidator {
def context
def log
def validate(String stepName, File file) {
//Change the name of the Properties test step below
def step = context.testCase.testSteps[stepName]
//Parse the xml like you have in your script
def parsedXml = new XmlSlurper().parse(file)
//Get the all the error details from the response as map
def errorDetails = parsedXml.'**'.findAll { it.name() == 'IntegrationServiceErrorCode'}.inject([:]){map, entry -> map[entry.ErrorCode.text()] = entry.Description.text(); map }
log.info "Error details from response : ${errorDetails}"
def failureMessage = new StringBuffer()
//Loop thru properties of Property step and check against the response
step.properties.keySet().each { key ->
if (errorDetails.containsKey(key)) {
step.properties[key]?.value == errorDetails[key] ?: failureMessage.append("Response error code discription mismatch. expected [${step.properties[key]?.value}] vs actual [${errorDetails[key]}]")
} else {
failureMessage.append("Response does not have error code ${key}")
}
}
if (failureMessage.toString()) {
throw new Error(failureMessage.toString())
}
}
}
Hope you already aware where to copy the above class. Note that this also has package name. So copy it in the right directory.
I have suggestion here regarding your package name which is so long, you can change it to something like com.linos.readyapi.util. Of course, upto you.
Now here is how you can use / call the above class or its methods from a Groovy Script test step of a test case in the different soapui projects:
Groovy Script step
import com.Linos.readyapi.util.property.propertyvalidation.PropertyValidator
def properties = [context:context, log:log] as PropertyValidator
//You need to have the file object here
properties.validate('Properties', file)
Ah! a utility library you mean?
Assuming you have placed your groovy library file in this path
D:\GroovyLib\com\Linos\readyapi\util\property\propertyvalidation
If you are using soapui then set above path as the value of below field,
File>preferences>SoapUiPro>Script Library
If you are using ready api then,
File>preferences>Ready! API>Script Library
Then call your method by initializing the class first
PropertyValidation classObject = new PropertyValidation()
classObject.propertystepvalidation()
//you might need to pass some parameters which are declared/initiated outside this class
It seems that in the following piece of code:
def formattedPaths = affectedFiles.collect {
"${it.editType.name} ${it.path}"
}
at least sometimes formattedPaths evaluates to a GString instead of a List. This piece of code is a fragment of a larger Jenkins Workflow script, something like:
node {
currentBuild.rawBuild.changeSets[0].collect {
"""<b>${it.user}</b> # rev. ${it.revision}: ${it.msg}
${affectedFilesLog(it.affectedFiles)}"""
}
}
def affectedFilesLog(affectedFiles) {
println "Affected files [${affectedFiles.class}]: $affectedFiles"
def formattedPaths = affectedFiles.collect {
"${it.editType.name} ${it.path}"
}
println "formattedPaths [${formattedPaths.class}]: $formattedPaths"
formatItemList(formattedPaths)
}
def formatItemList(list) {
if (list) {
return list.join('\n')
}
return '(none)'
}
Running this script in Jenkins produces output:
Running: Print Message
Affected files [class java.util.ArrayList]: [hudson.scm.SubversionChangeLogSet$Path#5030a7d8]
Running: Print Message
formattedPaths [class org.codehaus.groovy.runtime.GStringImpl]: edit my/path/flow.groovy
(...)
groovy.lang.MissingMethodException: No signature of method: java.lang.String.join() is applicable for argument types: (java.lang.String) values: [
]
And this makes me believe that in the code:
println "Affected files [${affectedFiles.class}]: $affectedFiles"
def formattedPaths = affectedFiles.collect {
"${it.editType.name} ${it.path}"
}
println "formattedPaths [${formattedPaths.class}]: $formattedPaths"
affectedFiles is ArrayList (script output Affected files [class java.util.ArrayList]: [hudson.scm.SubversionChangeLogSet$Path#5030a7d8] in the output)
but result of running collect method on it - assigned to formattedPaths - is a GString (output: formattedPaths [class org.codehaus.groovy.runtime.GStringImpl]: edit my/path/flow.groovy)
Shouldn't the collect method always return a List?
After the discussion in the comments pointing that it may be some side effects done by the Jenkins Workflow plugin, I decided to use a plain for-each loop:
def affectedFilesLog(affectedFiles) {
println "Affected files [${affectedFiles.class}]: $affectedFiles"
def ret = ""
for (Object affectedFile : affectedFiles) {
ret += affectedFile.path + '\n'
}
println("affectedFilesLog ret [${ret.class}]: $ret")
if (!ret) {
return '(brak)'
}
return ret;
}
EDIT 19/11/2015:
Jenkins workflow plugin mishandles functions taking Closures, see https://issues.jenkins-ci.org/browse/JENKINS-26481 and its duplicates. So rewriting the code to a plain Java for-each loop was the best solution.
You cannot currently use the collect method. JENKINS-26481
I guess your code is not thread safe. If you pass some objects as paramters to some other functions, do not change this. Always create and return a new changed object. Do not manipulate the original data. You should check where your objects live. Is it just inside a function or in global scope?
I'm trying to let a script assertion fail, when a variable has another value than defined. My goal: Mark the TestStep as red, if the TestStep will fail. Please note that I am using a script assertion for a TestStep - not a separate Groovy-Script TestStep.
My script looks like this:
httpResponseHeader = messageExchange.responseHeaders
contentType = httpResponseHeader["Content-Type"]
log.info("Content-Type: " + contentType)
if (contentType != "[image/jpeg]"){
log.info("ERROR! Response is not an image.")
//Script Assertion should fail.
//TestStep should be marked red.
} else {
log.info("OK! ResponseType is an image.")
}
Is there any way, how to let the script assertion fail depending on a property?
I've tried to use the getStatus() method, but this is only available for the testrunner object. Unfortunately the testRunner-object can not be used within a script assertion concerning this post: http://forum.soapui.org/viewtopic.php?f=2&t=2494#p9107
Need to assert so that test fails or passes automatically based on the condition.
def httpResponseHeader = messageExchange.responseHeaders
def contentType = httpResponseHeader["Content-Type"]
log.info("Content-Type: " + contentType)
assert contentType.get(0) == "image/jpeg","Content Type is not an image"
EDIT: Earlier paid attention to how to throw error, assuming you have the rest in place. Now changed last line of code based on the input.
I am having troubles using XMLSlurper to update an XML document. Most things work, but in some situations a "find" doesn't find a Node I just appended (appendNode). The new Node is there at the end of processing, but is not found when I am in the middle of adding children.
I found a post about XMLSlurper that says that finding the new Node requires calling parseText again and/or StreaMarkupBuilder (see below). Really?! That seems so kludgy that I thought I'd verify on SO.
Here is a code snippet. The "find" gets NoChildren even though the Node was just added.
codeNode.appendNode {
'lab:vendorData'() {}
}
vendorNode = codeNode.children().find { it.name() == "vendorData" }
"appendNode doea not modify the slurped document directly. The edit is applied "on the fly" when the document is written out using StreamingMarkupBuilder."
http://markmail.org/message/5nmxbhwna7hr5zcq#query:related%3A5nmxbhwna7hr5zcq+page:1+mid:bkdesettsnfnieno+state:results
Why can't I find my new Node?!
This is what I got to work. Is not elegant, but got past "update" problem:
...
codeNode.appendNode {
'lab:vendorData'() {}
}
//-- must re-slurp to see appended node
labDoc = new XmlSlurper().parseText(serializeXml(labDoc))
codeNode = getResultNodeFor( nextResult.getCode() );
vendorNode = codeNode.children().find { it.name() == "vendorData" }
...
def String serializeXml(GPathResult xml){
XmlUtil.serialize(new StreamingMarkupBuilder().bind {
mkp.declareNamespace("lab", "www.myco.com/LabDocument")
mkp.yield labDoc
} )
}