How to rewrite variables in Newman? I have a file with my environment variables that are rewritten after a request is executed, everything works in Postman (RUN FOLDER), but in Newman all tests crash. Many of my variables do not have initial values, they only acquire them when a test passes. For example there is a POST request that adds a user, the body of the response will return to me:
{
"id": null,
"result": {
"id": "c9497eb1-e4ea-47f0-a00e-a46cf8ef07a7"
},
"success": true
}
I take the value of the ID, and override the variable with the following command:
var jsonData = pm.response.json();
pm.environment.set('newUserId', jsonData.result.id);
When running Newman, everything crashes. Please help, if you would be so kind.
What key should I use when starting Newman? Or how to achieve positive results in NEWMAN to be like in Postman, is there such an option for processing variables?
Many tests drops when I use env value in URL path, like
https://some-url/check/{createdUserId}
.
You can export also environment variable too.
The 'newman' running no problem if using exported environment variable
newman run my.postman_collection.json -e my.postman_environment.json
Demo
#1 running a local API server for adding user and getting user
#2 postman call
#3 running collection
#4 newman running
Save as 'server'js'
const express = require('express');
const cors = require('cors');
const uuid = require('uuid');
const app = express();
const port = 3000;
app.use(express.json());
app.use(cors());
const users = {}; // Memory storage for user data
// API 1: Create(Add) user by POST
app.post('/createUser', (req, res) => {
const { first_name, last_name, email } = req.body;
const userId = uuid.v4(); // Generate a new UUID for the user
users[userId] = { first_name, last_name, email }; // Store user data in memory
res.status(201).json({
id: null, // You can set this to any specific value if needed
result: {
id: userId,
},
success: true,
});
});
// API 2: Get user by GET
app.get('/getUser/:user_id', (req, res) => {
const userId = req.params.user_id;
const user = users[userId];
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json({
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
});
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
npm install express cors uuid
node server.js
Create User 1 and get User 1, checked user information (name and email)
Key is using user-id
environment variable for POST (Adding)/GET(Getting) user pair.
If not matched Adding user/ Getting user, something wrong.
{
"id": null,
"result": {
"id": {"uuid"}
},
"success": true
}
http://localhost:3000/getUser/{{user-id : uuid}}
Response
{"first_name":"Steve","last_name":"Jobs","email":"steve_jobs@example.com"}
It can confirmed created user and get user is same or not by calling GET with uuid
This format
Tests
tab for adding user
POST call
http://localhost:3000/createUser
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("user-id", jsonData.result.id);
console.log(pm.environment.get("user-id"));
Body
{
"first_name": "Steve",
"last_name":"Jobs",
"email": "steve_jobs@example.com"
}
Tests
tab for adding user
GET call
http://localhost:3000/getUser/{{user-id}}
var jsonData = JSON.parse(responseBody);
console.log(jsonData);
pm.expect(jsonData.first_name).to.eql("Steve");
pm.expect(jsonData.last_name).to.eql("Jobs");
pm.expect(jsonData.email).to.eql("steve_jobs@example.com");
Add user 2 with different user information
newman
running on terminalnewman run 1-1-demo.postman_collection.json -e demo.postman_environment.json