How to compare CSV to Postman JSON Response values? - postman

How to compare CSV to Postman JSON Response values?
I want to compare CSV values to Postman Response value.
For Ex:
My CSV:
Iteration, City, Ramen
1,"Vancouver",100
2,"San Francisco",84
My End Point:
a link
My Tests:
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.args.ramen).to.eql(pm.iterationData.get("Ramen"));
});
Note: I get files from below URL. Only tests are mine.
a link
As of now I'm getting below error:
Your test name | AssertionError: expected '84' to deeply equal 84

Try casting the csv integer value to a string and using to.equal instead of to.eql (which is equivalent to to.deep.equal):
pm.expect(jsonData.args.ramen).to.equal(new String(pm.iterationData.get("Ramen")));

Your expected value for "Ramen" from your CSV Datafile is 84 of type Number.
But your Service returns the property "ramen" with value "84" of type String.
Your test fails for a good reason.
Of course you can change your testdata by casting. But this isn't a good test, because you are changing your expected test-data during runtime to become tests green.
If the response is ok, an the testdata not:
You need to store the the "Ramen" Values in your CSV as a String:
1,"Vancouver","100"
2,"San Francisco","84"
and so on.
If the testdata is ok, an the response not:
You have to fix the wrong Servie behaviour.

Related

Nunit assert AreEqual dif because quotes

I´m writing automation test for Api Rest.
In the body response to return:
"New Current Account"
I do the follow validation:
Assert.AreEqual("New Current Account", response.Content);
But it doesn´t work the Nunit return failed beacuse:
Message:
Expected string length 19 but was 21. Strings differ at index 0.
Expected: "New Current Account"
But was: ""New Current Account""
-----------^
Can someone help me?
Apparently, the string being returned actually contains quotes.
The proper way to reference this is by escaping the quotes that are part of the data in the string you use for an expected value.
Assert.AreEqual("\"New Current Account\"", response.Content);
This is preferable to using logic to trim off the quotes, because you are comparing actual to expected data without modifying either.
I resolved with follow alternative.
Assert.AreEqual("New Current Account", response.Content.Trim('"'));

How to extract a particular string value from response header in postman?

I have below string and want to extract the value of code. I used split function but which runs fine in postman but when i execute same in newman it gives error.
header1=https://debugger.com/ultradebugcode?code=EgxIZAAU3uHVt20pc9qqpv-xNcAWxitpB0vgMiulNLG2hkysukmjR04Fxxnuz9Yb&state=ABC
I want to extract the value of code. which in this case is
EgxIZAAU3uHVt20pc9qqpv-xNcAWxitpB0vgMiulNLG2hkysukmjR04Fxxnuz9Yb
the code i am using is
var str= pm.response.headers.get('header1');
var str1= str.split('code=', 2)[1];
var code= str1.split('&', 2)[0]; // get the code
It worked fine in postman but why newman is giving error here?
This worked for me:
let str = pm.response.headers.get("header1").split("code=")[1]
console.log(str.split("&")[0])

Concrete5 Community Store (Square) not processing payment as an integer

This error is occurring each time I try to put a payment through.
Log shows:
JSON from response body:
{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"EXPECTED_INTEGER","detail":"Expected
an integer value.","field":"amount_money.amount"}]}
I modified line 123 of CommunityStoreSquarePaymentMethod.php from:
"amount" => StoreCalculator::getGrandTotal()*100,
to:
"amount" => number_format(StoreCalculator::getGrandTotal()*100,'',''),
This gave me the error:
Exception Occurred:
/public_html/packages/community_store_square/src/CommunityStore/Payment/Methods/CommunityStoreSquare/CommunityStoreSquarePaymentMethod.php:123
number_format() expects parameter 2 to be integer, string given (2)
So it looks like it's being read as a string instead of an integer. What's the best way to rectify this? I've tried set_type but not sure I understand how to use it as it returns a boolean.

Store Data from Postman request in variables to use in tests

Im currently trying to get used to POSTMAN and i was wondering if there is a way to store variables from my request JSON Body via Pre Request in some environment variable so ican resuse it in the tests for response value cheks
This is how my json File might look like
{
"text" : "myText",
"attachments": {
"text": "myText2",
"anotherText" : "myText3"
}
So i want to get all Values, store them in a variable before sending my request, and then test if they match the expected value in my response
(example: myText2 gets mapped to green, myText3 gets mapped to red and so on)
That would make it possible to write one test for several request
Thanks a lot!
You can write the following in your script:
let body = JSON.parse(pm.request.body);
_.forEach(body, (value, key) => pm.environment.set(key, JSON.stringify(value)));
This will set each key and it's associated value as an environment variables.
Note you'll need to JSON.parse the value in the test script before using it for testing.
For eg in your test script you'll need to do something like this:
let attachments = JSON.parse(pm.environment.get('attachments'));
pm.test('All attachments are of correct value', function () {
// ...write your test here using the `attachments` variable
});

Using Postman, unable to reference CSV file for JavaScript test

I am using a REST API with a POST request. I have created a CSV file to load in various inputs and using the Collection Runner to submit my requests and run the associated JavaScript Tests iteratively. I am trying to figure out how I can also have an entry in each row of the CSV to reference for my JavaScript Test in order to make the JavaScript dynamic. I've searched the Postman documentation and forums, as well as Google and Stack Overflow, but I haven't found anything that works. Here is a basic example of what I'm trying to accomplish.
Let's say I have a basic adding API. Here is my Request:
{
"Numbers": {
"Value_1": {{val1}},
"Value_2": {{val2}},
}
}
The CSV file is as follows:
val1,val2,sum
1,1,2
2,2,4
3,3,6
For this example, lets assume that the API returns a response that includes the sum of val1 and val2; something like this:
{
"Numbers": {{sum}},
}
I am able to load val1 and val2 into my request and iterate through the request for each row, but I am having trouble incorporating the sum values (from the same CSV) into the JavaScript Test.
I am trying to do something like the test below where I can reference the sum value from my spreadsheet, but Postman doesn't like my syntax.
pm.test("Adding machine", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.Numbers === {{sum}});
});
Does anyone have any suggestions? Is this even possible to do?
You could use the pm.iterationData().get('var_name') function and create a check like this?
pm.test("Sums are correctly calculated", () => {
pm.expect(pm.response.json().Numbers).to.equal(pm.iterationData.get('sum'))
})