How To Fix Common Errors In Postman

In the previous articles on Postman Tutorial, we have covered “How To Generate Advanced HTML Reports, While Using Newman“
In this “How To Fix Common Errors In Postman” article, I will be demonstrating how you can implement this concept and get a tight grip over this.
Common Errors In Postman
Common Error in Postman #1:
If we have an environment variable as {{url}. We cannot write inside script as pm.sendRequest({{url}}/item/). The syntax {{url}} works only inside the request builder and not in scripts.
Example:
var requestUrl = pm.environment.get(("url") + "/item/"); pm.sendRequest(requestUrl, function(err, res){ //Write test here });
Common Error in Postman #2:
If we want to trigger another request from a pre-request script, we can do it by using postman.setNextRequest
Example:
Postman.setNextRequest('request name as saved in Postman');
Common Error in Postman #3:
If we want to compare already saved variables (eg. Username) with values from another API response, we can use a method.
Example:
pm.test ("Your test name", function(){ var jsonData = pm.response.json(); pm.expect(jsonData.value).to.eql(pm.globals.get("username")); });
Common Error in Postman #4:
If we have a script like this:
pm.test("Test Name", function () { var jsonData = pm.response.json(); pm.expect(jsonData.name).to.eql('John'); }); pm.globals.set('name', jsonData.name);
Sometimes we get the error “ReferenceError: jsonData is not defined” while setting the global variable. To fix this, we need to define jsonData outside the function.
var jsonData = pm.response.json(); pm.test("Name should be John", function () { pm.expect(jsonData.name).to.eql('John'); }); pm.globals.set('name', jsonData.name);
Or we can set the environment or global variable inside the function.
pm.test("Name should be John", function () { var jsonData = pm.response.json(); pm.expect(jsonData.name).to.eql('John'); pm.globals.set('name', jsonData.name); });
Common Error in Postman #5:
If we want to set a delay while running a collection in Newman, we can use — delay parameter and specify delay in milliseconds.
newman run collection.json -- delay 10000
Common Error in Postman #6:
If Jenkins shows weird characters in the console, it means output is in Unicode format. To fix this, either use View as Plain text option or add following flags: — disable-unicode
Example:
newman run collection.json – disable-unicode
Next steps:
Learn “GUID in Postman” in the next tutorial.
Related posts: