cancel
Showing results for 
Search instead for 
Did you mean: 

Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datasourc

Highlighted
Contributor

Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datasourc

My testing situation involves reading a file that contains JSON and examining particular items within that file using a JSONpath expression.  I have been writing Groovy scripts that use JSONSlurper to pull in the JSON, and then examining the resulting Map objects (not using JSONpath).  It was suggested that I could use a Datasource test step of type "File" to read in the JSON, and then a subsequent Datasource test step of type "JSON" to examine it.

 

I created a File Datasource test step, and have successfully read in the file (I believe this because I see the file contents in the Data Log tab).  But in the Properties for this test step, I see a property whose value is just a single squiggly bracket.  I've tried putting different things in the "separator" box (a comma, nothing, left or right squiggly brackets, xxxx) to no avail.

 

Can anyone direct me on how to use these Datasource test steps this way, or should I just go back to typing Groovy scripts?

 

EDIT:

I tried to use a File datasource, and the resulting property just contained the last line of the file.

My file is not comma-separated or anything like that, it’s JSON.  I couldn’t figure out any value to put in the “separator” box that would work.

Is this because I used File and not Filewait?

13 REPLIES 13
Highlighted
Staff

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi, tgeliot

 

I suggest you try File Wait test step for reading your JSON file instead. It can wait for a file to appear, but more importantly it reads its content to fileContent property, which you later can address to in JSON DataSource.

 

File DataSource is useful when you have a file with easily separated property values (comma-separated, etc).


Anton Egorov
ReadyAPI developer
Highlighted
Occasional Contributor

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

hi  Anton Egorov

 

I'm trying to use File Wait test step for reading a JSON request

But don't know if I'm usinf well.

What do you mean by  "it reads its content to fileContent property"?

 

Thanks & regards

Anna

 

 

Highlighted
Staff

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi Anna,

 

File Wait test step has custom property fileContent. After the step is executed, this property will have the content of your file. You can access it in the following test steps using property expansion string - "${File Wait#fileContent}", where "File Wait" is the name of your File Wait step. In most cases Get Data item in context menu will generate this string for you.

 

In case of JSON DataSource you should choose your File Wait step in Source Step and fileContent in Source property (see http://readyapi.smartbear.com/structure/sources/json/start). You should run the test case at least once to be able to select nodes from the actual file, using buttons next to Row Path and Column Paths fields.


Anton Egorov
ReadyAPI developer
Highlighted
Occasional Contributor

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi Anton,

 

Thanks for your answers !!


Checking the link you send me. I realized that probably there is another way to do what I'm trying to do.

 

Have a look the screenshot attached. I'm doing assertions with groovy scripts. What I need is to read a JSON file from the file system. The best way, it would be with "File Wait " or It might be better "Groovy Data Source"

 

Thanks in advance
Anna

Highlighted
Staff

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi Anna,

 

It depends on what you're going to do next with the content of the file. DataSource steps are generally used with DataSource Loop step. JSON DataSource is useful when you have an array in JSON file (in RowPath node) and need to perform several test steps in a cycle for each array element (Column Paths). In Groovy DataSource you can manually generate each subsequent value - a script will be executed each iteration.

 

However, judging by the screenshot you want to use file content as body of the request. For this just add File Wait before the request and enter property expansion ${File Wait#fileContent} to Body field. To access the file content from Groovy script use

def fileContent = context.expand( '${File Wait#fileContent}' )


Anton Egorov
ReadyAPI developer
Highlighted
Occasional Contributor

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi Anton!

 

Can you cofirm, please if "file wait is proper of PRO SOAPUI version?

Thanks !!

 

Anna

Highlighted
Staff

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi, Anna.

 

No, this step was added only in Ready! API, but it should work with SoapUI Pro license and have lots of new features, so I suggest you give it a try.


Anton Egorov
ReadyAPI developer
Highlighted
Occasional Contributor

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi all !

 

Another question related:

 

We are using JSON files to load all the body parameters for a Test Suit with a lot of Test Steps. Most of this parameters are diferent for each Test Step, but they have some common parameters. And every time that some change is done, we should modify every single JSON.

So, We want to automate the JSON's preloading

We are considering to upload files with "file Wait", and before executing the test we want to predefine this common fields.

Which is the best way to do it? Some idea before starting?

 

Example, All bellow Test Cases have common parameters, but every once in a while we need to change them :
departure: "BCN",
arrival : "LGW",
departureDateTime : "2017-01-04",

 

Thanks very much
Anna

Highlighted
Staff

Re: Using a File datasource to read a file full of JSON (and then to use that JSON with a JSON datas

Hi, Anna!

 

If I understood you correctly, you want to load a JSON that should go to request body from a file, but then change some of its fields during test execution. So, in your example departure, arrival and departureDateTime already exist in JSON file, but you want to change them, while leaving the rest of the parameters as they are in the file, right?

 

For this scenario you can use File Wait and then Property Transfer with several transfers: the first one should move the file content to the whole request body, and others should move each of your predefined values for common fields to the specific request fields via JSONPath. As transfers are executed in the order of their positions in the list, this should work fine.

 

Say, your TestCase have 2 custom properties: departure and arrival (you can change its values in groovy script test step at any stage of your test). Then you need one FileWait step in the beginning of your test. Then goes PropertyTransfer, and then - your POST Request.

 

PropertyTransfer will have 3 transfers (make sure path expressions are correct for your JSON):

 

1) wholeRequest (Source: FileWait, Property: fileContent; Target: POST Request, Property: Request);

 

2) departureTransfer (Source: TestCase, Property: departure; Target: POST Request, Property: Request, Path Language: JSONPath, Path: $.departure);

 

2) arrivalTransfer (Source: TestCase, Property: arrival; Target: POST Request, Property: Request, Path Language: JSONPath, Path: $.arrival).

 

To execute a request several times with different predefined values, it's more convenient to create a DataSource Loop with some type of DataSource (e.g. Grid, where your can just type your values in the table). DataSource should have properties that correspond your predefined parameters (departure and arrival in my case). Target step for DataSource Loop should be PropertyTransfer. And in Property Transfer for departureTransfer and arrivalTransfer you'll just need to change Source step to DataSource.


Anton Egorov
ReadyAPI developer
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors