I am trying to read a JSON file in as a DataSource test step (see JSON below). The number of nodes of the properties (named "columns") varies with each tableName in the JSON file. Is it possible to read in row by row and end up with the simple result posted below? It seems like this should be simple, but I've been struggling for hours! Thanks!
INPUT JSON FILE
{ "ontologyName": "Procedure", "linkedResources": [{ "tableName": "ProcedureTable1", "columns": [ "columnAA", "columnBB", "columnCC" ] }, { "tableName": "ProcedureTable2", "columns": [ "columnAA", "columnBB" ] }, { "tableName": "ProcedureTable3", "columns": [ "columnAA" ] } ] }
DESIRED OUTPUT
ProcedureTable1,columnAA ProcedureTable1,columnBB ProcedureTable1,columnCC ProcedureTable2,columnAA ProcedureTable2,columnBB ProcedureTable3,columnAA
Solved! Go to Solution.
Try changing below statement
if (row+1 < data.size)
To
if (row < data.size)
def str = '''{ "ontologyName": "Procedure", "linkedResources": [{ "tableName": "ProcedureTable1", "columns": [ "columnAA", "columnBB", "columnCC" ] }, { "tableName": "ProcedureTable2", "columns": [ "columnAA", "columnBB" ] }, { "tableName": "ProcedureTable3", "columns": [ "columnAA" ] } ] }''' def json = new groovy.json.JsonSlurper().parseText(str) def data = json.linkedResources.inject([]) { list, table -> list << table.columns.collect { ['table': table.tableName, 'column': it]}; list.flatten() } data.each { log.info "${it.table}, ${it.column}" }
Is this what you needed?
You can test this online
If you want to implement it in your case, you need make few modifications to the above script.
In the Data source step, use Groovy type. And have above script in place
1. instead of fixed data, you can use json file and read that and assign it to str variable
2. need to define a variable and assign current row count
3. loop thru variable data and assign it variable result
In the test request step, where the data is required use table, column respectively.
Refer documetation link.
https://support.smartbear.com/readyapi/docs/testing/data-driven/types/groovy.html
Thanks Rao.
Hi @mpartyka , did you have a chance to try the suggestion?
This is awesome ... exactly what i was looking for to loop through and read the JSON.
I am now working on this step "2. need to define a variable and assign current row count" and am having trouble with getting the data source test step to read only a single row of the output at a time ... the step is reading all 6 rows in a single pass (of data loop) instead of only 1 row. I am also having an issue getting the test step to save the property. Can you help ? Thanks
ROW 1 - ProcedureTable1, columnAA
ROW 2 - ProcedureTable1, columnBB
ROW 3 - ProcedureTable1, columnCC
ROW 4 - ProcedureTable2, columnAA
ROW 5 - ProcedureTable2, columnBB
ROW 6 - ProcedureTable3, columnAA
here is groovy data source i am currently trying to get to work .... Thanks for any help !!!
def str = context.expand( '${DataSource-readJSON#JSONstring}' ) // Get current row def row = testRunner.testCase.testSteps["GroovyDataSource"].currentRow; log.info ("row = " + row) def grid = []; // Get a list of items in the grid array def json = new groovy.json.JsonSlurper().parseText(str) def data = json.linkedResources.inject([]) { list, table -> list << table.columns.collect { ['table': table.tableName, 'column': it]}; list.flatten() } data.each { log.info "${it.table}, ${it.column}" } // *********************************** //{ // // Add the name of the file to the list // data -> grid.add(it.table,it.column); //} // *********************************** if (row < grid.size) { // Return the name to data source's "File" property result["table"] = it.table[row] result["column"] = it.column[row] }
Thanks for trying and implementing your case.
I couldn't test this, would you please give it a try?
def str = context.expand( '${DataSource-readJSON#JSONstring}' ) // Get current row def row = testRunner.testCase.testSteps["GroovyDataSource"].currentRow log.info "row = $row" // Get a list of items in the grid array def json = new groovy.json.JsonSlurper().parseText(str) def data = json.linkedResources.inject([]) { list, table -> list << table.columns.collect { ['table': table.tableName, 'column': it]}; list.flatten() } if (row+1 < data.size) { result["table"] = data[row].table result["column"] = data[row].column }
Thanks again Rao ... I implemented your suggestion above ... almost there. Missing ROW 6 .
ROW 1 - ProcedureTable1, columnAA
ROW 2 - ProcedureTable1, columnBB
ROW 3 - ProcedureTable1, columnCC
ROW 4 - ProcedureTable2, columnAA
ROW 5 - ProcedureTable2, columnBB
ROW 6 - ProcedureTable3, columnAA - this row is not processed
By reviewing the script log ... i can see that
Thanks again for all your help.
Try changing below statement
if (row+1 < data.size)
To
if (row < data.size)
Thanks so much Rao - it works perfectly now! That was it ... should have caught that myself :smileyembarrassed:
Subject | Author | Latest Post |
---|---|---|