Let's take a regular test statement:
pm.test("response is ok", function () {
pm.response.to.have.status(200);
});
I need to customize the statement with a variable:
pm.test("response for Customer Number" + {{CustomerNumber}} + " is ok", function () {
pm.response.to.have.status(200);
});
Where CustomerNumber is a defined environment variable.
I have tried with {{$CustomerNumber}} but nothing seems to work, it throws an error.
Try this. Works for me.
pm.test("response for Customer Number" + pm.environment.get("CustomerNumber") + " is ok", function () {
pm.response.to.have.status(200);
});
Related
Hi I have the following assertion in postman but it is failing
pm.test("Body matches string", function() {
pm.expect(pm.response.text()).to.include('Success')
|| pm.expect(pm.response.text()).to.include('Request already submitted');
});
My Response contains text success or Request already submitted. Please help.
pm.test("Body matches string", function () {
let a = "Success"
"Request already submitted" === a ? pm.expect("Request already submitted").to.be.equal('Request already submitted') : pm.expect(a).to.be.equal('Success')
});
pm.test("Body matches string", function () {
let a = "Success"
try {
pm.expect("Request already submitted").to.be.equal('Request already submitted')
}
catch (e) { pm.expect(a).to.be.equal('Success') }
});
pm.test("Body matches string", function () {
let a = "Success"
pm.expect(a).to.be.oneOf(['Success', 'Request already submitted']);
});
expect does not return a boolean it throws an error, so either catch it or use the oneof methed, or first check a condition and then assert
I have a test scenario in postman where I have to stop the execution of next request if an envrionment variable value is null. Below is the piece of code and collection description:
Collection details: I have two request in a collection test1 & test2. If in below scenario variable "document" has value then I want request "test2" to be executed otherwise request "test1" should keep on executing as per the data being passed from the csv sheet.
pm.test("Verify the the: " + data.value + " exist in " + data.source, function() {
_.each(pm.response.json(), (arrItem) => {
pm.expect(arrItem.id).to.be.equal(data.value);
pm.expect(arrItem.source).to.be.equal(data.source);
findDocument(arrItem);
})
})
function findDocument(arr) {
arr.links.forEach(a => {
if (a.id.indexOf('A90') > 0) {
pm.environment.set('document', a.id)
} else
postman.setNextRequest(null)
});
}
I have a response body like
{
"agreementId": "agreement900",
"Status": "ONHOLD"
}
The value of the status can be one of
['PAID','CANCELLED','COOLINGOFF','ONHOLD','COOLINGOFF','PAID']
I need to write a generic test to verify that the body.Status is always among the specified array.
I tried something like this
var data = ['PAID','CANCELLED','COOLINGOFF','ONHOLD','COOLINGOFF','PAID'];
pm.test("Verify Body value", function () {
let testResult = data.find((each)=>{
pm.expect(each.payoutStatus).to.equal(jsonData.payoutStatus)
});
});
But received the following error: Verify Body value | AssertionError: expected undefined to equal 'ONHOLD'
Deepak, welcome to SO
I am not sure about edge cases nor performance, but this can be a way of achieving it:
var myStatus = pm.response.json().Status;
var myEnum = ['Pig','Chicken','Cow'];
pm.test("Status belongs to the ENUMs", function () {
pm.expect(myEnum).to.include(myStatus);
});
I am testing an API with a GET request that returns the following data:
{
"Verified": true,
"VerifiedDate": 2018-10-08
}
I am trying to test that the first field comes back true, and the second field has a value. I have the following code:
pm.test("Verified should be true", function () {
var Status = pm.response.json();
pm.expect(Status.Verified).to.be.true;
});
pm.test("Returns a verified date", function () {
var Status = pm.response.json();
pm.expect(Status.VerifiedDate).to.not.eql(null);
});
The assert on true is failing for the following reason:
Verified should be true | AssertionError: expected undefined to be true
Why is the first test failing?
I am running the same test on a post command without any issues.
Any ideas?
thanks
Root cause:
Your result is an array but your test is verifying an object. Thus, the postman will throw the exception since it could not compare.
Solution:
Use exactly value of an item in the list with if else command to compare.
var arr = pm.response.json();
console.log(arr.length)
for (var i = 0; i < arr.length; i++)
{
if(arr[i].Verified === true){
pm.test("Verified should be true", function () {
pm.expect(arr[i].Verified).to.be.true;
});
}
if(arr[i].Verified === false){
pm.test("Verified should be false", function () {
pm.expect(arr[i].Verified).to.be.false;
});
}
}
Hope it help you.
You could also just do this:
pm.test('Check the response body properties', () => {
_.each(pm.response.json(), (item) => {
pm.expect(item.Verified).to.be.true
pm.expect(item.VerifiedDate).to.be.a('string').and.match(/^\d{4}-\d{2}-\d{2}$/)
})
})
The check will do a few things for you, it will iterate over the whole array and check that the Verified property is true and also check that the VerifiedDate is a string and matches the YYYY-MM-DD format, like in the example given in your question.
I am working on an app that makes heavy use of JavaScript. I need to unit test this code. In an effort to do that, I'm relying on Jasmine.
Some of my JavaScript code throws JavaScript Error objects. Those objects assign values to the message and name property of the Error object. I assign a type of exception to the name property. For instance, sometimes the name is set to OutOfRangeException, sometimes its ArgumentException, etc.
How do I use the toThrowError function in the Jasmine framework to test if a thrown error has a specific name? Currently, my JavaScript looks like the following:
function getRandomNumber(max) {
if ((!isNaN(parseFloat(max)) && isFinite(max)) === false) {
var error = new Error('You must provide a number');
error.name = 'ArgumentException';
throw error;
}
if ((max === null) || (max < 1) || (max > 100)) {
var error = new Error('The maximum value must be greater than 0 and less than 100.');
error.name = 'ArgumentOutOfRangeException';
throw error;
}
return Math.floor(Math.random() * max) + 1;
}
function ArgumentException(message) {
this.name = 'ArgumentException';
this.message = message || '';
}
ArgumentException.prototype = new Error();
ArgumentException.prototype.constructor = ArgumentException;
How can I write a Jasmine test that checks for an ArgumentException error or an ArgumentOutOfRangeException error?
Thank you!
Checking exception for a function with parameter is not supported in jasmine. But you can use below workaround to overcome this limitation and test your functions.
describe('toThrowError test case', function() {
it('test getRandomNumber function for undefined', function() {
expect(function() {
getRandomNumber(undefined);
}).toThrowError("You must provide a number");
});
it('test getRandomNumber function for 0', function() {
expect(function() {
getRandomNumber(0);
}).toThrowError("The maximum value must be greater than 0 and less than 100.");
});
});
toThrowError matcher takes 1 or 2 parameters
1 Parameter - Either exception message or exception type
2 Parameters - Exception type and Exception message
Example to check based on exception type:
function getRandomNumber(max) {
throw new SyntaxError();
}
describe('toThrowError test case', function() {
it('test getRandomNumber function for undefined', function() {
expect(function() {
getRandomNumber(undefined);
}).toThrowError(SyntaxError);
});
});
Refer link for different types of exceptions.
Custom Error Message
Below mentioned snippet gives a sample for using the custom error messages.
function getRandomNumber(max) {
throw new ArgumentException();
}
function ArgumentException(message) {
this.name = 'ArgumentException';
this.message = message || '';
}
ArgumentException.prototype = new Error();
ArgumentException.prototype.constructor = ArgumentException;
describe('toThrowError test case', function() {
it('test getRandomNumber function for undefined', function() {
expect(function() {
getRandomNumber(undefined);
}).toThrowError(ArgumentException);
});
});