ContributionsMost RecentMost LikesSolutionsRe: Groovy To Extract Comma Separated List Into Separate Properties Hey nmrao, Apologies for not responding sooner. I've been dealing with a family emergency. I'll be responding tomorrow. Cheers, Rich Groovy To Extract Comma Separated List Into Separate Properties Hi, So this is on the end of one of my previous posts - nmrao spoonfed me until I got there (so all cred to him) - this is the next step. I have a string of comma separated values stored as a custom testSuite level property. I want to extract the individual values from the single property into multiple separate properties for later use. I've managed to do it - but it's shockingly bad groovy - so I'm wondering if anyone can guide me into a way of doing it more efficiently The testSuite level 'MY_SUBLIST' property has the value of: 3K684,3K686,3K688,3U3750 As stated above, I want to separate out each comma separated value into it's own testSuite level property. As stated above - I've managed it - but it's poor groovy. Please see as follows: //get the comma separated list in my testSuite level property entitled MY_SUBLIST and save it to the mySubListToString variable def mySubListToString = context.testCase.testSuite.getPropertyValue('MY_SUBLIST') log.info mySubListToString // log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K684,3K686,3K688,3U3750 //use split() to extract the first route_code value and save this value to the splitString1 property def splitString1 = mySubListToString.split(',')[0].toString() log.info(splitString1) //log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K684 //save splitString1 value to a new testSuite level property entitled splitString1 testRunner.testCase.testSuite.setPropertyValue( "splitString1", splitString1) //use split() to extract the second route_code value and save this value to the splitString2 property def splitString2 = mySubListToString.split(',')[1].toString() log.info(splitString2) //log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K686 //save splitString2 value to a new testSuite level property entitled splitString2 testRunner.testCase.testSuite.setPropertyValue( "splitString2", splitString2) //use split() to extract the third route_code value and save this value to the splitString3 property def splitString3 = mySubListToString.split(',')[2].toString() log.info(splitString3) //log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3K688 //save splitString3 value to a new testSuite level property entitled splitString3 testRunner.testCase.testSuite.setPropertyValue( "splitString3", splitString3) //use split() to extract the fourth route_code value and save this value to the splitString4 property def splitString4 = mySubListToString.split(',')[3].toString() log.info(splitString4) //log response was --> Thu May 23 00:20:30 BST 2024: INFO: 3U3750 //save splitString4 value to a new testSuite level property entitled splitString4 testRunner.testCase.testSuite.setPropertyValue( "splitString4", splitString4) As you can see - I'm not going to win any awards with the code above. Can anyone advise on a more efficient/effective way of doing it? It's rubbish to have to explicitly define a variable and a new property for each comma separated value - so I'd welcome if anyone has a more efficient way I can do this. As you can see - what I've done gets the job done - but it's a shonky way of doing it! Cheers! rich Re: GroovyScript - Extract specific number of attribute values from variable content response nmrao saves the day (despite the fact you don't even have ReadyAPI to debug your code) //Script Assertion - all code below courtesy of Rao //extract the request's response and pass it to the variable entitled json def json = new groovy.json.JsonSlurper().parseText(context.response) //find all 'id' attribute values def id_list = json.data.'id'.findAll() //List the 'id' attribute item count log.info id_list.size //define starting index value def startingIndex=0 //Set this value, how many values to be stored def endIndex=4 //This is to ensure not to grab more than present in the list assert endIndex <= id_list.size(), 'Choose the endIndex less than list size' //Now get the sub list from original list def mySubList = id_list.subList(startingIndex,endIndex) log.info mySubList //In order to save data as custom property, coerce it to String def mySubListToString = mySubList.join(',') log.info mySubListToString //save off the comma separated string values as a custom testSuite Property entitled MY_SUBLIST context.testCase.testSuite.setPropertyValue('MY_SUBLIST', mySubListToString) This is brilliant - exactly what I need. I'm gonna need to extract the values for later use - I might have a question or two on that - just tried a quick go using split() and it didn't work like I'd hoped it would, but lemme give it a good go first and I might be getting back to you. Marking this as fixed and your solution as The Solution. Again - thanks so much, rich Re: GroovyScript - Extract specific number of attribute values from variable content response Hey nmrao My bad- I'm sorry for causing any confusion - I'm working with 60 different REST APIs which provide all the data for the 3 tiered web app I'm working on and all these APIs are starting to merge into one. I've been playing around with the Script Assertions on a different GET API - which is why I've caused confusion. Also - the question I'm asking is so transferrable when the number of records in your response payload changes - once I get this sorted I'm gonna be using this snippet of groovy for a lot of things. So - to make things straightforward - lets keep going with the original API I mentioned before GET /v2/plans which returns a number of 'plans'. (I've attached the same payload as the original one I attached in my first post). Each plan has a unique identifier with JSONPATH--> x.data*.id (where the first record's id attribute has JSONPATH x.data[0].id) Spent the last 4 hours going through all my notes and googling and I managed to get a little further with the following code: //Script Assertion def json = new groovy.json.JsonSlurper().parseText(context.response) // Log the first id log.info('First (index zero) id = ' + json.data[0].id) // Log all ids with index identifier (it's JSON so it starts at ZERO) json.data.eachWithIndex(){ prop, idx -> log.info('id ' + idx + ' = ' + prop.id) } So at this point, I have logged all the id values in the response along with it;s index identifier. I've been googling to try and understand what this '{ prop ->' represents in groovy - I see it a lot but I've never really understood what it actually means, but I'm trying to understand so I can reuse it in the future but I didn't really understand. It said that -> is used in a closure and this separates the "argument list from the body" Anyway - sorry going off at a tangent. - so I've now got a list of id's logged - but because of the user of this '{ prop ->' - I don't know how to convert the indexed id's (displayed in the logging) to a custom property. I've reminded myself of the getProperty() and setProperty() methods, but I'm struggling on this point, cos my list of indexed id's aren't actually assigned to a variable - so I'm a little lost there. Once I can do that - I was thinking I need to somehow assign the x.data[0].id attribute in my property list (once it's been extracted) to the startingIndex variable you have defined in the code you gave me (def startingIndex = 0) so then I can continue to use the rest of the code you gave me (see below) - I think - I'm sure I'm missing several important issues like just cos I've a variable named as startingIndex with a numeric value - the groovy compiler isn't going to know that I want : x.data[0].id to be assigned startingIndex=0, x.data[1].id assigned startingIndex=1 x.data[2].id assigned startingIndex=2 x.data[3].id assigned startingIndex=3 def range = 1..10 log.info "Items in the range: $range" def startingIndex=0 //Set this value, how many values to be stored def endIndex=4 //This is to ensure not to grab more than present in the list assert endIndex <= range.size(), 'Choose the endIndex less than list size' //Now get the sub list from original list def mySubList = range.subList(startingIndex,endIndex) log.info mySubList //In order to save data as custom property, coierce it to String def mySubListToString = mySubList.join(',') log.info mySubListToString //Save this at suite level //Convert string back to list when needed later def stringToList = mySubListToString.split() log.info stringToList So that's where I am right now. I'm actually having a lot of fun - this is way more interesting than the rest of my job at the moment, so if you can give me a couple of pointers to steer me where I'm going wrong - I'd really appreciate it. thanks man - as always - can't thank you enough, Oh - I should also apologise - I've been kindof jumping all over the place trying to find a way through so I've trying various different approaches to do what I need - that probably isn't helping you understand what I'm talking about either! rich Re: How to create Identical GET request with different parameters in same project Hey nmrao I think I marked the original answer as the solution before I'd even seen your data driven approach. I'm also working 12/14 hour days right now so I was probably rushing. I've changed which answer is the solution as your data driven approach is definitely the better option than just recloning the whole OAS back into the same project! holdit - you're answering groovyscript questions without even being able to debug your code within tool? That's quite impressive - wish I could do that! I'll start work on this as soon as I get some free time - cos this is definitely the way to do this! Cheers, Rich Re: Executing test case loop multiple times Hey ChrisAdams I saw this post but couldn't think of a way to do with without groovy - you've taught me something new - nice one! Rich Re: GroovyScript - Extract specific number of attribute values from variable content response Hey nmrao Ok - I'm still struggling - if I'm understanding the code correctly - your code starts off as follows via a Script Assertion: def json = new groovy.json.JsonSlurper().parseText(context.response) log.info json def startingIndex=0 //Set this value, how many values to be stored def endIndex=4 assert endIndex <= range.size(), 'Choose the endIndex less than list size' //Now get the sub list from original list def mySubList = range.subList(startingIndex,endIndex) log.info mySubList //In order to save data as custom property, coierce it to String def mySubListToString = mySubList.join(',') log.info mySubListToString //Save this at suite level so the JSONPATH of the first route_code out of the 4 I care about is x.json.data[0].route_code HOW do I match the startingIndex variable (which you've got declared above) to json.data[0].route_code) so that the other 3 route_code values (with values json.data[1].route_code, json.data[2].route_code, json.data[3].route_code) are extracted also? Your comment on the line declaring the mySubListToString variable is the bit where I save my data as a custom testsuite property - but at this point I've got no data - I haven't yet extracted the x number of route_codes (4 in the case of the above script defined by the endIndex variable) - this is the bit I'm struggling with. Just so you know I'm not just letting you do all the heavy lifting without trying to solve myself - I did try playing around with the following to see if I could get a little further along (or at least increase my understanding) - cos the code below is based on what you've helped me out with before - I was hoping I could edit it to do what I need, but it only logs the first instance of the route_code value - I thought lines 5 & 6 might extract all the route_codes - but I realised I'm not using the it() method - I did try adding .it() in various places but I couldn't get it to work - I really need to buy a groovy for dummies book! // Log the first route_code log.info('First (index zero) route_code = ' + json.data[0].route_code) // Log all route_codes json.data.every{ prop -> log.info('route_code = ' + prop.route_code) } // Log all route_code with index json.data.eachWithIndex(){ prop, idx -> log.info('route_code ' + idx + ' = ' + prop.route_code) } Been years since I've used ReadyAPI! when you were helping me with my groovy before and it's really showing! As always - appreciate your help! Cheers, Rich Re: How to create Identical GET request with different parameters in same project Hey nmrao This is a cracking idea - hadn't even considered using a data driven approach to this. This is great - haven't touched ReadyAPI! for 4 years and this is really helping me get back into it. I'm still working on building the groovy using the snippets you provided in my other post. Once I've finished that and I've got it working, I'm definitely coming back to this - cos the your approach is invaluable and transferrable for a multitude of other projects AND it will help improving my groovy too (which lets face it, I need all the help on that score! ;) ) I'll definitely be coming back with some questions on this Rao - this will really help me going forwards. The help you provide is the difference between me getting my job done and not - so really - can't thank you enough Cheers, rich Re: Error connecting to Postgress DB in ReadyAPI 3.52.0 Hey @abhinaykumar15 it's not a timeout, right? You're definitely not timing out and instead the connection is refused? That leads me to think you've got some of the details correct but not all. Some RDBMSs include some optional parameters that are needed to be specified in the JDBC connection string (thinking mostly SQLServer here - but other RDBMSs do include some optionals as well). Can you confirm the JDBC connection string details from any of your work colleagues? Whenever I'm trying to setup a DB connection, I tend to check my JDBC connection details using a DB interrogation tool first (e.g. TOAD, DBeaver, DBVisualiser, etc.) rather than mess around in ReadyAPI! first. If the string works in one of these tools, then the string should work in ReadyAPI! By the way - whenever I have to setup a DB connection - I run through the following steps: Identify the Vendor and version of RDBMS I'm trying to connect to Identify the correct JDBC drivers for the version of RDBMS I want to connect o Identify the version of JRE/JDK that supports the relevant the JDBC drivers I need to use (making sure I'm using the correct 'bit type' - either 32bit of 64bit) Determine the JDBC connection string needed to connect to relevant RDBMS (including any of the optional parms if necessary) Cheers, rich Re: How to create Identical GET request with different parameters in same project Hey troyyerJP Ha! Been doing this for so long I can't even remember I answered that! I tried cloning the resource, I tried cloning the method and I tried cloning the request - all of them generated validation issues hindering having different parameter TYPES (enabling/disabling the HEADER parm from HEADER to PLAIN and back AGAIN) for the same API. Admittedly, this approach isn't perfect cos my OpenAPISpec has 50 different APIs in it - I'll need to clone the Service for each different negative test - but this is a way around what I need - so full cred to you! ;) Cheers! Rich