From below Response, I want to fetch the value of "responseCode" and store temporarily. If a value is 1 then on Console, I want to write "Test PASS". Can anyone share code for this test?
{
"data":{
"transactionId":"$1"
},
"responseMessage":"Transaction successfully done. Transaction Id : txn_15594028419901124218",
"responseCode":1
}
I tried to use the following code to set the variable:
var jsonData = JSON.parse(responseBody);
pm.globals.set("responseCode",jsonData.data.responseCode);
This basic test would check that value in the response, store the variable and also write Test PASS to the console
pm.test("Check the Response Code is 1", () => {
pm.expect(pm.response.json().responseCode).to.eql(1);
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
});
This doesn't account for the test failing and writing Test FAIL to the console, you kind of get that anyway in the Postman UI.
If you didn't want to wrap this in a test, you could just do something like:
if(pm.response.json().responseCode === 1){
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
}
else {
console.log("Test FAIL")
}
Related
How can I validate a response item is an instance of a collection variable where the collection variable is an array in postman?
Here first I'm making an array from a response from a GET request.
let arr = [];
for (item of response.books) {
arr.push(item.isbn);
}
pm.collectionVariables.set("Books_ISBN", arr);
console.log(arr);
Now I want to evaluate a response data of a POST request with the "Books_ISBN" collection variable.
My POST request response is this
{
"books": [
{
"isbn": "9781449325862"
}
]
}
I'm trying to do that like this but it is showing me error.
var response = JSON.parse(responseBody);
pm.test(pm.expect(response.books[0].isbn).to.be.an.instanceof(Books_ISBN));
Postman uses the Chaijs assertion library internally. to.be.an.instanceof checks if the type is an Array. You want to use the oneOf method(Docs) like this:
const Books_ISBN = pm.variables.get("Books_ISBN");
pm.test("my test", () => {
pm.expect(response.books[0].isbn).to.be.oneOf(Books_ISBN);
});
You also maybe want to look at the postman documentation for writing tests and the documentation on how to use variables in scripts.
Save array, object to as a varible, you should stringify first
pm.collectionVariables.set("Books_ISBN", JSON.stringify(arr));
Variable is not existed in script, you have to get it first, don't forget to parse.
let Books_ISBN = JSON.parse(pm.collectionVariables.get("Books_ISBN"));
pm.test("my test", () => {
pm.expect(response.books[0].isbn).to.be.oneOf(Books_ISBN);
});
how can I use environment variables for get method in postman, I have customer id's with me but I need to send different customer id's n check the response, but I want to send customer ID's through data driven in postman tool
Imagine there is a customerId in env variables. To send this value in body or as param, just replace the static value with {{customerId}}.
usage:
To update the variable, imagine we have the following response:
{
"success": true,
"data": {
"id": "1234"
}
}
in the test tab update it this way:
var data = JSON.parse(responseBody);
if(data && data.data && data.data.id){
pm.environment.set('customerId', data.data.id)
}
In order to save the response from postman API calls, I am executing Postman collection using newman run.
The collection runs when running using
newman run C:\TasteBud\NewMan\JSON-Order_collection.json --env-var unique_order_id=OD-06-07-I2Q5-JYRY5ARPN --environment C:\TasteBud\NewMan\EnvironmentJSON.json
However when I run the same collection as part of javascript or nodejs script.
node writeToFile.js
it throws error as node "1⠄ JSONError in test-script " refer attached image. I need to pass the auth token generated by login request to subsequent request. So I have variable assignment in the "test".
let response = pm.response.json();
pm.environment.set("auth_token", response.data.auth_token);
console.log(pm.response.json().data.auth_token);
Why cant I have "test" ? if no then how can I pass/set these environment variable for subsequent API call ?
Code inside writeToFile.js is here. writeToFile.js
Always use status code validation in your token generation. because every request is based on the token, if we receive negative status code, terminate the build.
postman.setNextRequest(null);
var jsonData = JSON.parse(responseBody);
if(responseCode.code === 200){
tests["Status code is 200"] = responseCode.code === 200;
postman.setEnvironmentVariable("AT", jsonData.oauth2.accessToken);
console.log("AccessToken : " +jsonData.oauth2.accessToken);
}
else{
postman.setNextRequest(null);
console.log("Error generating user token");
console.log("Status code : "+responseCode.code);
console.log("Status description : "+jsonData.statusDescr);
}
I require help to execute a postman test which requires a response output from another test. I have checked with various forums but a solution is not available for this particular scenario.
Example
Test 1 response:
{
"items": [
{
"email": "archer+qa01#gmail.com",
"DocumentName": "tc",
"type": "URL",
"url": "https://localhost:8443/user/terms?statusno=a5f2-eq2wd3ee45rrr"
}
]
}
Test 2:
I need to use only the a5f2-eq2wd3ee45rrr part of the response data from Test 1, this can be seen in the url value above. I need to use this value within Test 2
How can I make this work with Postman?
Not completely sure what the response data format is from the question but if it's a simple object with just the url property, you could use something simple like this:
var str = pm.response.json().url
pm.environment.set('value', str.split('=', 2)[1])
This will then set the value you need to a variable, for you to use in the next request using with the {{value}} syntax in a POST request body or by using pm.environment.get('value') in one of the test scripts.
Edit:
If the url property is in an array, you could loop through these and extract the value that way. This would set the variable but if you have more than 1 url property in the array it would set the last one it found.
_.each(pm.response.json(), (arrItem) => {
pm.environment.set('value', arrItem[0].url.split('=', 2)[1])
})
If you get JSON response and then send JSON in body, I would do the following:
1) In test script(javascript):
var JsonBody = pm.response.json();
var strToParse = JsonBody.url;
var value = strToParse.slice(indexOf("?status=")+"?status=".length);//parse string
//manually but you can google for a better solutions.
pm.environment.get("varName" , value)
2) You can use it! In scripts like: pm.environment.get("varName"), and everywhere else using {{varName}}
I want to exeucute Tests script in postman based on IF condition.
for eg:
var data = JSON.parse(responseBody);
if(responseCode.code === 404 ){
tests["TEst1: Invalid ID passed"];
}
When I use the above one it is not executing. How to use if condition on tests
To test the response code, you can do it directly.
tests["Status code is 400"] = responseCode.code === 400;
You have to use below line when you want to test the data inside the Json.
var data = JSON.parse(responseBody);
tests["Success --> Request & Response account first name matched"] = data.firstName === postman.getEnvironmentVariable("firstName");