Newman with variable

I’ve been using newman to do some automation, but I’m now beginning to hit a wall when I use it with node.js. Here’s the node.js code

const newman = require('newman');

newman.run({
     collection: require('./my.json'),
     iterationData: require('./my.csv'),
     iterations: 5,
     reporters: 'cli'
}).on('beforeRequest', function (error, args) {
     if (error) {
         console.error(error);
     } else {
         // Log the request body
         console.log(args.request.body.raw);
     }
}).on('request', function (error, args) {
     if (error) {
         console.error(error);
     }
     else {
         // Log the response body
         console.log(args.response.stream.toString());
     }
});

When I run this code, it cannot process the URL which includes a variable with double curly braces, i.e. http://myserver/{{variable}}

When I run node.js with a the above javascript. I get

ReferenceError: variable is not defined

it’s not a parameter, so it’s not a key-value pair.

Thoughts?

Not really sure what’s happening without more details but iterations should be iterationCount. As you’re using a data file, that’s not really needed as that should control that value.

Are you able to share the full error message you see? What is variable and is that contained in your collection somewhere?

The variable is the last part of the URL in the curly braces above. It’s not a key value pair. As far as the full error:

/Users/pr85/Downloads/my.csv:1
variable
^

ReferenceError: variable is not defined
at Object. (/Users/pr85/Downloads/my.csv:1:2)
at Module._compile (node:internal/modules/cjs/loader:1102:14)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1131:10)
at Module.load (node:internal/modules/cjs/loader:967:32)
at Function.Module._load (node:internal/modules/cjs/loader:807:14)
at Module.require (node:internal/modules/cjs/loader:991:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object. (/Users/pr85/Downloads/test.js:5:20)
at Module._compile (node:internal/modules/cjs/loader:1102:14)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1131:10)
at Module.load (node:internal/modules/cjs/loader:967:32)
at Function.Module._load (node:internal/modules/cjs/loader:807:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47

When I run newman with -d option, same file, it works as expected and substitutes the variable correctly.

P.

In that context, that’s not really interacting with the Collection because it’s not getting to that point yet.

If it was, you would be seeing the CLI output from the Newman run but it’s not even running it.

It’s more than likely the .csv file itself, what does that look like? Can you show an example?

Have you also tried this without wrapping that with those require() methods?

@dannydainton thanks the csv file is simply, it’s this:

variable,value1,value2,valuei3,value4

I’ll try removing the require.

P.