Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'incomingTransCode' of undefined at test script {{URGENT}} #1986

Closed
kbryantkb opened this issue Apr 29, 2019 · 1 comment
Labels

Comments

@kbryantkb
Copy link

kbryantkb commented Apr 29, 2019

Version and environment information

  1. Newman Version (4.4.1)
  2. OS Details( Microsoft Windows 10 pro, Version 10.0.17763 Build 10.0.17763)
    3: Using Newman as a library
    4: Just started using Newman 5 days ago and issue has existed the entire time I've been using Newman
    5: Expected behavior:
    I ran a collection called sales collection runs perfectly fine in Postman Runner, encounters an error when running using Newman. The collection two calls. The first call (Sales Token) works perfectly fine and all the tests return as expected. the second call (Sale) has a couple of environment variables that are passed from the response and is wrapped in a conditional statement based on whether the or whether not the response returns a 201 status or a 422. I also written some test scripts that run whether or whether not the status returned is 422 or 201. Sale also has some environment variables that are passed in from a CSV and are set as an environment variable. The problem is that whenever I run the collection using Newman, (Sale) returns a 422 and also returns a TypeError Cannot read property 'incomingTransactionCode' of undefined at test-script inside "Sale" for all iterations. This error does not occur in the Postman Runner.
    Command used to run Newman:
    node command_line_runner
    Below is the code in the command_line_runner.js file:

newman.run({ collection: 'https://api.getpostman.com/collections/xxxxxxxxxxxxxxxxxx?apikey=xxxxxxxxxxxxxxxxxxxx', environment: 'https://api.getpostman.com/environments/xxxxxxxxxxxxxxxxxx?apikey=xxxxxxxxxxxxxxxxxx', iterationData: './postman_test_data/submit_sales_test.csv', reporters: 'cli' }, function(err, summary){ if (err) {throw err;} console.log(summary.run.stats); });

Required parts of the body:

{ { "DeviceGuid": "{{Device}}", "Amount": "{{amount}}", "OrderNumber": "ABCXYZ-PARENT", "SendReceipt": false, "RiskProcessingOnly": false, "Transactions": { "ExpectedPayments": "{{expectedPayments}}" },"Card": { "CardHolderName": "Joe Cardholder", "CardNumber": "4111111111111111", "ExpirationDate": "1901", "Cvv2" : "{{cvvNumb}}", "Customer": { "FirstName": "Joe", "LastName": "Cardholder", "Email": "joe.cardholder@mailinator.com", "Address1": "123 Main St", "Address2": "Suite 500", "City": "Beverly Hills", "State": "CA", "Zip": "90210" } }

Prereqs:

`var cvvNumb, amount, expectedPayments;

//data feed
cvvNumb = data.CvvNumb;
pm.environment.set("cvvNumb",cvvNumb);

amount = data.Amount;
pm.environment.set("amount", amount);

expectedPayments = data.ExpectedPayments;
pm.environment.set("expectedPayments", expectedPayments);`

Test Scripts:

`var ITC, saleGuid, responseCode, cvv, jsonRespData, reqBody;

//local vars
jsonRespData = pm.response.json();

reqBody = JSON.parse(pm.request.body.raw);

responseCode = pm.response.code;

cvv = reqBody.Card.Cvv2;

//conditional vars

/*
The conditional vars are based on whether or whether not the response code returned is 201.
If so, then the variables are given a value, if not then the values remain null.
This is to prevent undefined variables that are expected to have values causing the tests to abort test runs.
*/
if (responseCode == 201){
if (cvv == 999) {

//local conditional vars
ITC = jsonRespData.Transaction.incomingTransCode;

saleGuid = jsonRespData.guid;

//environment conditional vars
pm.environment.set("IncomingTransactionCode", ITC);

pm.environment.set("saleGuid", saleGuid);
}
}
//Global Vars

//Tests
//condition based on cvv code being 999
/*
will only run the following tests:

  • response status test(201)
  • ITC test

based on the Cvv value being 999
*/
if(cvv == 999) {

//response status test(201)
//will return pass if response code is 201
pm.test("Response is 201", function () {
pm.response.to.have.status(201);
});

//ITC test
// will return pass if ITC is returned
pm.test("ITC test", function(){
pm.expect(ITC).to.not.be.null;
});

console.log(saleGuid);

//condition based on cvv code being 996
/*
will only run the following tests:

  • response status test(422)
  • invalid Cvv test

based on the Cvv value being 996
*/
}else if(cvv == 996) {
//response status test(422)
// will pass if response code is 422
pm.test("Response is 422", function () {
pm.response.to.have.status(422);
});

//invalid Cvv test
//will return pass if returns Cvv verification error
pm.test("CVV Error Message", function() {
pm.expect(jsonRespData.message).to.eql("Error code D2020. CVV2 verification failed");
});
}`

Data CSV:

CvvNumb,Amount,ExpectedPayments,Comments 999,35,3,should return a successful sale and create a ITC 996,70,4,should return a 422 error that states that the Cvv number is invalid and should not create an ITC

Below are the results when running in Newman:

newman response

Below are the results in running in Postman Runner:

results in postman runner

@kbryantkb kbryantkb changed the title Canno read property 'xxx' of undefined at test script Cannot read property 'xxx' of undefined at test script {{URGENT}} Apr 29, 2019
@kbryantkb kbryantkb changed the title Cannot read property 'xxx' of undefined at test script {{URGENT}} Cannot read property 'incomingTransCode' of undefined at test script {{URGENT}} Apr 29, 2019
@codenirvana
Copy link
Member

@kbryantkb Cannot read property x of undefined is because of unsafe access to a property which in your case is incomingTransCode in jsonRespData.Transaction object.

Which means response it received in Newman is different (Transaction is undefined) from what it got in the collection runner.
I see you used two different data file (CSV) as well, might be that's the issue.

I suggest you add console.log (or add tests) for request body sent and the response received to verify those first for every iteration.

Closing this issue because it's not related to Newman, maybe you could post this question in https://community.getpostman.com/ to get help around how to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants