Hello,
I want to read lines from my local xml/json file in Pre-request script to add its value for my variables.
For example:
I have a file called OPC.xml, wanna attribute this file to a variable and find lines which have “name” in itself. E.g: “<Name)Qwerty (/Name>” I used ) because right formula makes it
invisible. Qwerty and other variables should be stored in one array.
How can I do this?
Thanks for any help.
HI @mkolomanski,
I’m afraid Postman can’t easily access local files directly because it runs in a sandbox . However, there are a few workarounds as follows
- Postman can access data through API’s, so if you have the skills to create a local API (eg using Node.js & Express) then you can simply fetch the file using a request, store the data in a global, and use it in subsequent requests. (This isn’t as hard as you might think, I ran a workshop with complete novices to create a simple API - see slides 7-14)
- You can create a tool that formats your data into a csv or json file suitable to use with the collection runner, or Newman.
- You could write some code to inject your data into an environment or globals file prior to running it with Newman.
You’re probably going to have to write some code to get the data from your file into something that Postman can access, but that said, I’ve had to do this in the past and found it easier than I thought it might be (and I am not a dev by any estimation )
Let us know which one you think might suit you best, and if you get stuck we can help you.
Hello, I am very grateful for your complex response. But I already have dealt with it in other way.
Although parasing local files will be very useful. I am looking forward to have this option in Postman Have a nice day!
Hello mkolomanski,
Great that if you share how you managed to do it in other way. We have a similar problem.
Best Regards
I just wanted to share a workaround to this problem.
Storing local JSON or XML files is nice for separating and organising your test data. Since there is no native way to do that, if you’re willing to compromise slightly and keep your test data stored in the body of a Postman request, this solution might work for you.
If feeding Postman a JSON file for a collection run is not an option (like it was not for me), you can instead store the JSON inside a POST request that sends raw JSON to the Postman echo API:
- Set up a POST request that sends data to
https://postman-echo.com/post
- Define the raw JSON body, maybe something like this:
{
"user": {
"email": "[email protected]",
"password": "password",
"username": "UserName",
"first_name": "Grayed",
"last_name": "Fox",
"new_password": "!orAng3",
"new_username": "{{$randomUserName}}",
"new_first_name": "{{$randomFirstName}}",
"new_last_name": "{{$randomLastName}}"
}
}
- inside the pre-request script, clear any variables and do any setup you need to, mine looks like:
// Before sending the request we clear all collection variables
pm.collectionVariables.clear();
- Set the JSON data in the test script as an environment variable like so:
const jsonData = pm.response.json().data;
pm.collectionVariables.set("collectionData", jsonData);
Now from inside any request you can access the data using the moustache/double curly brackets technique, i.e. you could send a different request with the following JSON body:
{
"email": "{{email}}",
"password": "{{password}}",
"first_name": "{{first_name}}",
"last_name": "{{last_name}}"
}
To access the data from within a test, use destructuring assignment and take what you need:
const { email, first_name, last_name } =
pm.collectionVariables.toObject();
It’s not reading a local file, but you can easily duplicate the request to the Postman echo service and basically use that endpoint as a sort of “setup” request that clears old collection values and sets new ones, so you still get the advantage of splitting up test data into organized parts.
Hi! Thanks for the info and detailed explanation. I am needing to do something like this, but slightly different. We have a test case where we want to hit a “settings” endpoint that will retrieve app settings from the app’s json configuration file (duh…), and we need to validate that the endpoint is in fact responding with the proper data (maybe it’s kind of a silly testcase, but anyway… )…
We are trying to see if there is a way to pickup that json file from postman (it’s in the filesystem’s app path) in order to compare it with the response…
Would this approach that you shared be applicable to that scenario?
Thanks!
EDIT: Sorry, stupid question… I didn’t even take time to read what you were doing in your workaround…
Hi! I know this is an old post, but I found myself in a scenario where I need to pickup a config file from the filesystem and then hit and endpoint and compare the endpoint response with the actual file read by the endpoint.
I guess that the way to go would be your #3 workaround, which if I understand correctly, would imply creating a script that will allow newman to load the json file to an environment variable in order to have its content available from the tests… right?
Do you have any examples on how to achieve that?
Thanks!
@jprealini setting your collection environment variable(s) before sending the request would do what you need. You don’t actually read a JSON file from within Postman or Newman, you just store the JSON data inside a dedicated utility request which you fire off first.
-
set the JSON data you are sending to your server as a Postman variable, i.e.
pm.collectionVariables.set("myDataEnv", jsonData)
-
send the JSON data to the endpoint you wish to test
-
in the tests section of your request, compare the results returned by the endpoint with the environment variable(s) you set, here is an example using destructuring:
const { email, first_name, last_name } = pm.collectionVariables.toObject();
You can refer to my earlier answer for a more detailed explanation that basically walks you through the same thing