Hi! I need to fetch data from one request (from Response-object or array/list of Response-object in case of using batch method) and put that data into another request (into request body).
Suppose I have such scenario:
describe(`02. POST /someMethod should return 200 OK and correct data`, (t) => {
console.log('02. POST /someMethod:');
let active_user = testdata[0]["user"]
const randomUser = active_user[Math.floor(Math.random() * active_user.length)];
console.log('Random user: ', JSON.stringify(randomUser));
console.log(`02. POST someMethod: active_user: ${randomUser}`)
session.addHeader("user", `${randomUser}`)
session.addTags("POST-someMethod")
let response = session.post("/someMethod",
JSON.stringify({
"field1": `${randomUser}`,
"field2": "qwerty",
"field3": "1234"
}));
t.expect(response.status).as("response status").toEqual(200)
someId = response.json("someId");
anotherId = response.json("map.anotherId");
})
&&
describe(`03. POST /someMethod2`, (t) => {
console.log('03. POST /someMethod2:');
let active_user = testdata[0]["user"]
// console.log('testdata: ', JSON.stringify(testdata));
// Pick a random username/password pair
const randomUser = active_user[Math.floor(Math.random() * active_user.length)];
console.log('Random user: ', JSON.stringify(randomUser));
console.log(`03. POST /someMethod2: active_user: ${randomUser}`)
let someMethod2 = {
method: "POST",
url: `${__ENV.BASE_URL}/someMethod2`,
body: JSON.stringify({
"someId": `${someId}`, // Here is the place where above values should be put
"anotherId": `${anotherId}`, // Here is the place where above values should be put
"field3": "val"
}),
params: {headers: {"accept": "application/json",
"token": "sdfsdfssg",
"user": `${randomUser}`}}
};
let responses = http.batch([someMethod2],
{
tags: {my_tag: "POST-someMethod2"},
});
responses.forEach(response => {
t.expect(response.status).as("response status").toEqual(200)
.and(response).toHaveValidJson()
});
What is the best practice here? I just declare module vars in the init context like this:
export let someId;
export let anotherId;
?
Or there is a better way?
What about http.batch or session.batch responses? How to pass responses’ lists into next request?
P.S.: I know that expect.js deprecated and it is recommended to use ChaiJS lib, but, unfortunately, we don’t have much time to migrate current old code…