Sub : how to do regular expression assertions for variable elements in an array
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2020
08:17 AM
11-30-2020
08:17 AM
Sub : how to do regular expression assertions for variable elements in an array
Description : Trying to write an assertion for fields code and unit.
code can be an integer or empty
and unit can be a string or empty.
i.e code and unit has no fixed values.
And have to assert based on assertion using regex.
the assertion have to match all the object properties in the response array.
[
{
"code": "0",
"unit": "meter"
},
{
"code": "1",
"unit": "meter"
},
{
"code": "2",
"unit": "meter"
},
{
"code": "",
"unit": ""
}
]
Solved! Go to Solution.
5 REPLIES 5
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2020
09:49 AM
11-30-2020
09:49 AM
Hey @jaya_kiran,
I can do this, but only in 2 separate assertions, not 1 assertion.
One of the real groovy scripters on the forum (@HimanshuTayal, @nmrao, @ChrisA) might be better placed to answer. I'll check tomorrow to see yours and their responses and go from there.
Cheers
Rich
I can do this, but only in 2 separate assertions, not 1 assertion.
One of the real groovy scripters on the forum (@HimanshuTayal, @nmrao, @ChrisA) might be better placed to answer. I'll check tomorrow to see yours and their responses and go from there.
Cheers
Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2020
07:32 PM
11-30-2020
07:32 PM
sure, 2 assertions are fine for me.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2020
07:34 PM
11-30-2020
07:34 PM
need solution thanks.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2020
03:48 AM
12-01-2020
03:48 AM
Hi,
Add a script assertion to the test step of interest.
The below will get you started, but be warned, it won't work first time. You will need to tailor this to your needs.
import groovy.json.JsonSlurper
import groovy.json.*
import java.util.regex.*
// User Json Slurper to get the JSON from the response.
def jsonSlurper = new JsonSlurper().parseText(messageExchange.response.responseContent)
// Just log how many items to check. If you get an error here, replace message with
// root node of interest in the response.
log.info("Checking ${jsonSlurper.message.size()} items in response.");
// Let's iterate over the array of items...
jsonSlurper.message.each{
// Have a look at the current items
log.info("Item. Code ${it.code}. Unit ${it.unit}.);
// Check it.code for numeric only.
if (it.code.matches("[0-9]+") && it.code.length() > 0) {
log.info("Code ${it.code} passes");
} else {
log.info("Code ${it.code} fails")
}
// Check it.unit for lowercase chars only.
if (it.unit.matches("[a-z]+") && it.unit.length() > 0) {
log.info("Code ${it.unit} passes");
} else {
log.info("Code ${it.unit} fails")
}
// You decide how to report failure. E.g. dump to a file
// or assert.
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2020
10:50 AM
12-01-2020
10:50 AM
thank you for the response.
