ContributionsMost RecentMost LikesSolutionsHow to Parse a JSON Response Using Groovy (Examples!) This code can be copied directly into a Groovy Script test step and run to demonstrate a few ways to get data from a JSON REST response: // Some Examples of How to Parse a JSON Response Using Groovy // set the example json response string (for a REST Request step assertion, use "def json = messageExchange.response.contentAsString") def json = '[{"firstName":"Bob","lastName":"Smith","uniqueId":146732,"thisIsAlwaysNull":null,"jobInfo":{"title":"","type":"Peon","code":42},"reviews":[{"date":"2017-06-01","type":"Regular","rating":"Adequate"},{"date":"2017-09-15","type":"Special","rating":"Other"}]},{"firstName":"Jack","lastName":"Jones","uniqueId":746381,"thisIsAlwaysNull":null,"jobInfo":{"title":"Big Boss","type":"Management","code":1},"reviews":[{"date":"2007-11-05","type":"Initial","rating":"Spectacular"}]},{"firstName":"Will","lastName":"Tell","uniqueId":574831,"thisIsAlwaysNull":null,"jobInfo":{"title":"Sweeper","type":"Peon","code":452},"reviews":[]}]' // parse json string using JsonSlurper - basically converts it to a groovy list def parsedJson = new groovy.json.JsonSlurper().parseText(json) // get data log.info " Count of people returned: " + parsedJson.size() log.info " Was Will's info returned (exists)? " + ( parsedJson.find { it.firstName == "Will" } != null ) log.info " Was Alice's info NOT returned (not exists)? " + ( parsedJson.find { it.firstName == "Alice" } == null ) log.info " First person's first name: " + parsedJson[0].firstName log.info " Index of person with ID 746381: " + parsedJson.findIndexOf { it.uniqueId == 746381 } log.info " Info for person with last name Tell: " + parsedJson.find { it.lastName == 'Tell' } log.info " Jack's ID: " + ( parsedJson.find { it.firstName == 'Jack' } ).uniqueId log.info " Jack Jones's job title: " + ( parsedJson.find { it.firstName == 'Jack' && it.lastName == 'Jones' } ).jobInfo.title log.info " All peon job type people's first names: " + ( parsedJson.findAll { it.jobInfo.type == 'Peon' } ).firstName log.info " Is Will's thisIsAlwaysNull null? " + ( ( parsedJson.find { it.firstName == 'Will' } ).thisIsAlwaysNull == null ) log.info " Will Tell's had this many reviews: " + ( parsedJson.find { it.firstName == 'Will' && it.lastName == 'Tell' } ).reviews.size() log.info " Bob's 2017-06-01 review rating: " + ( ( parsedJson.find { it.firstName == 'Bob' } ).reviews.find { it.date == '2017-06-01' } ).rating Hope this helps someone else! SolvedRe: How to write property value in setup script for test suiteS? Since you are in the Test Suite Setup Script, the "project" object is not available (and you need to define the 'Value' property, if you haven't). Try: String Value = "Some Value" testSuite.setPropertyValue( "PropName", Value ) Re: Assertion to Verify Repeating Attribute has Specific Value Domain? Try: assert json.data.Name.every{ ['Food', 'Wines', 'Aromatised Wines', 'Spirits'].contains( it ) } Re: Insert Prefix to a Groovy Generated Substring value? Try: namePropertyvaluePartial = "~" + namePropertyvaluePartial // or namePropertyvaluePartial = "~${namePropertyvaluePartial}" Re: Groovy Substr Method - Is it Possible To Parameterize the Left /Right Hand Value? I'm not entiirely certain what 'namePropertyValue' actually contains, but if it's something like "/api/1/geographical-indication/product-group?Name=~chi", then this will get everything after the equals sign: def namePropertyvaluePartial = namePropertyvalue.substring( namePropertyvalue.indexOf( "=" ) ) Re: How to print Testcase name for Failed TestSteps Using a groovy assert statement, anything after the colon is printed when the assertion fails. So, in a Groovy Step (where "testRunner" is available: String expected = "Bob" ; String actual = "Alan" ; assert actual == expected : testRunner.testCase.name + ": Actual does not match Expected" ; And in a Script Assertions in a an Assertion Step (where only "context" is available): String expected = "Bob" ; String actual = "Alan" ; assert actual == expected : context.testCase.name + ": Actual does not match Expected" ; Re: How to write property value in setup script for test suiteS? I found this thread when I was trying to accomplish the same thing. In a project's Setup Script and TearDown Script, we have direct access to the 'project' object. So, setting a custom project property is simply: project.setPropertyValue( "PropName", Value ) Getting all project custom properties: def allProps = project.getProperties() And removing a specific project custom property: project.removeProperty( "PropName" ) Tip: context.expand() works on multiple properties in one string! This may be obvious to everyone else, but I'd always used context.expand() on single properties, e.g.: value = context.expand( '${#TestSuite#TsCustomProp}' ). Today I realized that I could have multiple properties in a string, and using context.expand() on the entire string would expand all the properties in that string: String checkMe = 'This string has properties ${#TestSuite#TsCustomProp} and ${#TestCase#TcCustomProp} and ${#Env#DATASOURCE_USERNAME} and ${#Project#ProjCustomProp}' checkMe = context.expand( checkMe ) log.info " checkMe: '${checkMe}'" Which outputs Tue Apr 10 09:32:31 EDT 2018:INFO: checkMe: 'This string has properties MyTestSuiteCustomName and MyTestCase and CHARLES and thisisgreat' Awesome! SolvedRe: Data driven testing-Data source JDBC This link lists the IBM DB2 SQL Error Codes; clicking on one of them opens a new page with more information (I use this all the time when debugging my SQL): http://publib.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/dsnmcj10/2.1.6?DT=20040216120637 The -104 code indicates there was something in the SQL that DB2 didn't recognize/expect; in the error string, look for the semi-colon and the unrecognized/unexpected something will be right before it, with possible fixes/alternatives after the semi-colon. In this case, "=nbr=and" is unrecognized; DB2 was expecting something like "AND". Re: how to format a date Yes, the last six numbers are milliseconds. With a DB2 database, the output format for a timestamp (when queried) is "2014-02-11 22:57:12.650224", but if you need to set a timestamp, you have to format it "2014-02-11-22.57.12.650224". Since this is just a string, try: def ts = "2014-02-11 22:57:12.650224" // Replace space with dash ts = ts.replaceAll( " ", "-" ) // Replace colons with periods ts = ts.replaceAll( ":", "." ) log.info ts