I am writing tests collections for endpoints and I want the test to check if the response param estadoAula has the same value as the request param estadoAula so I can test everything went as intended. The param need to be sent in the body and not in the URL
Request Body
{
"estadoAula": "1"
}
Response Body
{
"idAula": "8d4cf346-cda0-47ca-acae-33981738b4b6",
"estadoAula": "1"
}
Test
pm.test("Estado modificado correctamente",function(){
var data = pm.response.json();
let estadoAula = pm.request.body.estadoAula; <--- this doesn´t work, I need to get request param 'estadoAula'
pm.expect(data.estadoAula).to.eql(estadoAula);
});
You'll need to parse the request body, I am assuming you've set it to RAW along with 'JSON' as type.
This script should work for you:
pm.test("Estado modificado correctamente",function(){
let data = pm.response.json(),
requestBody = JSON.parse(pm.request.body.raw);
pm.expect(data.estadoAula).to.eql(requestBody.estadoAula);
});
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);
});
In pre-req script I wrote logic:
var obj = pm.request.body.toJSON();
var rawObj = JSON.parse(obj.raw);
var token =rawObj.token;
if(token==1){
pm.environment.set("status_code", 200);
pm.environment.set("msg", "token-1");
}else if(token==2){
pm.environment.set("status_code", 202);
pm.environment.set("msg", "token-2");
}
For response I wrote,
{"status_code":{{status_code}}, "msg":{{msg}}}
But in response the values are not passing, it prints above line as string.
How to send the environment value as response for mock server api request?
Let's say my API call URL is www.example.com/quiz with POST method.
And I get the response body like this. And
var jsonData = pm.response.json();
pm.collectionVariables.set("cv_quiz_order", quiz_order)
if(!jsonData.is_end){
// TODO: request next question using `quiz_order`
}else{
// TODO: finish this API and go to the next request.
}
When I use Run Collection. I want it(the Apis) tests one by one in regular sequence. And only this Api repeats until its is_end is true.
How can I do this?
var jsonData = pm.response.json();
pm.collectionVariables.set("cv_quiz_order", quiz_order)
if(!jsonData.is_end){
postman.setNextRequest(pm.info.requestName)
}else{
// TODO: finish this API and go to the next request.
}
this will keep sending the same request until is_end is true
postman.setNextRequest allows you to set the next reqeust to be executed , pm.info.requestName gives thecurrent request Name , so you are saying run this request as next request
I have a GET request to OKTA to retrieve some information that uses some variables etc. It returns a body. I have a second request of type PUT where I manually paste the BODY and make a change to one variable. I am trying to determine if I can remove the manual process of pasting in the response body from the 1st GET request onto the second PUT request.
As an example, I have a URL:
GET https://{{myurl}}/api/v1/apps/{{instanceid}}
This returns some dyanmic JSON data in the payload like so
"blah":{ some more blah
},
"signOn": {
"defaultRelayState": null,
"ssoAcsUrlOverride": ""
"audienceOverride": null,
"recipientOverride": null
}
what I am hoping to do is:
PUT https://{{myurl}}/api/v1/apps/{{instanceid}}
{replay entire body from 1st request with the modification of
"ssoAcsUrlOverride": "{{some var that points to a new url}},
}
I have looked at some articles that show:
Using Tests to send a GET request with a static body and replaying that exact body. In this case, I am looking to modify a parameter not replay as=is
I tried this thread here (In postman, how do I take a response body and use it in a new request within Tests
postman-how-do-i-take-a-response-body-and-use-it-in-a-new-request-within-tes) but I get an error stating that responseBody is not defined
First of all, let's validate the JSON response first. Here is the valid JSON with some dummy data.
{
"blah": "some more blah",
"signOn": {
"defaultRelayState": "1",
"ssoAcsUrlOverride": "www.google.com",
"audienceOverride": "true",
"recipientOverride": "yes"
}
}
1) Save first request's response into a environment variable req_body as follows,
var jsonData = pm.response.json();
pm.environment.set("req_body", jsonData);
2) In the PUT request, take another environment variable replace_this_body in body.
3) Get the value of E'variable req_body we had set in the first request in Pre-request script. Then change the value of it and set current request's body variable.
var requestBody = pm.environment.get("req_body");
requestBody.signOn.ssoAcsUrlOverride = "https://www.getpostman.com";
pm.environment.set("replace_this_body", JSON.stringify(requestBody));
Finally, you will get updated request data into PUT request!
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");