Forum Discussion
- TanyaYatskovskaSmartBear Alumni (Retired)
Hi Anichat26,
You need to add DataSource to your test first. After that, you will be able to get the data from it during each iteration. We have an online tutorials explaining how to set up Data-Driven tests in a project - refer to it:
- anichat26New Contributor
Hi TanyaGorbunova,
Hope you are doing well. I am sorry but I cannot accept this as my answer. I think you probably didn't understand my question. Let me clarify it once again.
Suppose I have a JSON
{
"supplierId":"2",
"supplierName":"Test Supplier",
"items":[
{ "itemcode":1, "quantity":5},
{ "itemcode":2, "quantity":6},
.............. //this array of items can have multiple "itemcode" and "quantity" and the count of "items" depends on the number of items I provide for a supplier.
]
}
If it would have been a single item instead of an array, I could have mapped the datasource with individual property of JSON but when the count of data is unknown how can I map it?
If there is any link for an exact solution, please provide.
- gilugopiContributor
DataSource is not the answer for your need. You need to write sort of goovy script to add more items to your Json. Given below is an example.
Refer to online documentation of groovy libraries JsonSlurper, JsonBuilder or JsonOutput so that you can write script specific for your need. Given below is an example script for demonstration.
def jsonString = """ { "items":[ { "itemcode":1, "quantity":5}, { "itemcode":2, "quantity":6} ] } """ def json = new groovy.json.JsonSlurper().parseText(jsonString) class item{ def itemcode; def quantity; item(def itemcode, def quantity){ this.itemcode = itemcode this.quantity = quantity } } def item3 = new item(3,7) def item4 = new item(4,8) def jsonBuilder = new groovy.json.JsonBuilder(json); List items = jsonBuilder.content.items items.add(item3) items.add(item4) jsonBuilder.content.items = items; println jsonBuilder.toPrettyString()
Output
{
"items": [
{
"itemcode": 1,
"quantity": 5
},
{
"itemcode": 2,
"quantity": 6
},
{
"itemcode": 3,
"quantity": 7
},
{
"itemcode": 4,
"quantity": 8
}
]
}Cheers !
Gilu Gopi
Related Content
- 2 years ago
- 8 years ago
Recent Discussions
- 22 days ago