Reading excel column into property, parsing REST response for property member
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Reading excel column into property, parsing REST response for property member
In a test case, I have a data source step. It reads a column range into a property.
When I run the step, the data log shows each row from the desired column range.
I have a REST request after data step. I would like to make assertions to the response to check that each row member of the property from the data step appears in the response. This does not need to be directly checking items in the same index, as the order may change. I want to check only that each item from data property is in the JSON response.
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you would like to add "Contains" assertions(taking example), add that assertion to your response, after open that window ,give right click, will see "GetData" option, click on that, you will see all the Testcases etc.., please select your data source step, then select property which you would like to assert from "Properties" grid of "GetData" popup.
For EX: If your Data source name, "DataSorce" and property name "Name", then assertion value will be looks like ${DataSource#Name}
And we have to add multiple assertions depends on fields( if you would like to go with all fields of each row from your excel of your data source).
If you have have couple of them, better to go with inbuilt assertions(Contains, JSON path match etc..), but if your excel row contains many fields(Cells) for assertion, personally I would say groovy script is better option
Let me know, if you need any help in that groovy script(If you have to go with that).
Thanks!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This case is specifically many rows (cells) of data being read into the property in the data source step.
The request response needs to assert each cell value (attribute) exists in the response.
The assertion method suggested creates the assertion using index, which I'd prefer not to use.
When I've tested building the assertion using Get Data from the property, it fails:
$['Elements'][0]['column_name']
${Read_Excel_Sheet#attribute_name}
I get an error that:
Comparison failed. Path: ['$Elements'][0]['column_name']]; Expected value: []; Actual value: [actualvalue].
the issue I have is that the property appears to be NULL although when tested in the excel datasource, it does return all the desired rows/cells.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Can you please check the box for use as regular expression and give like
$['Elements'][*]['column_name'].
If you have many rows, I would suggest to go by writing assertion in groovy script.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If I've understood that suggestion correctly - I've added assertion, Contains type, then in the the dialogue selected - Use token as Regular Expression.
Then in the contains box, I've added in the $['Elements'][*]['column_name']
that results in a missing token response.
I had also already been looking at capturing the property from the test step in a groovy step, but that returns null.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's little confusing for me, what I understand, from previous, your excel looks like below
and when you hit REST call the response gives all the above fields. I mean to say your response contains AAA, 28,AA1, AAB,29, AA2, etc....
If it's correct, please confirm.
If it's not correct, could you please show your excel(Not the exactly one you are using, just give me a sample for idea), so then I can answer exactly without back and forth.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Got it now,
Test Step Structure:
Please keep your test steps structure like below.
and when you select "data source loop" select options like below,
and please keep below code in groovy script step (Data validation in above image)
Code:
//import library to parse JSON
import groovy.json.JsonSlurper
//get the JSON response from the test step that makes the call to some test step to get a JSON response
def response = testRunner.testCase.getTestStepByName("GetEmployees").getPropertyValue("Response")
def responseJSON = new JsonSlurper().parseText(response)
//verify the slurper isn't empty
assert!(responseJSON.isEmpty())
def attrName = context.expand( '${DataSource#attribute_name}' )
assert(responseJSON.toString().contains(attrName)): "attrName $attrName doesn't exists in response"
Note: if your response have multiple arrays, and objects. and the assertion attribute coming in particular array/object please navigate to that and do assertion so it will reduce time complexity.
Ex: assert(responseJSON[i].employees[j].accounts.toString().contains(attrName)): "attrName $attrName doesn't exists in response"
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
