How can I pass some variable from one group to another? I need to use some data in the response body that is within group A in the request of group B.
export default function() {
// Declare variables here for global use
let var1;
group("function_1", function(){
let url = "url.com";
let response = http.get(url, params);
var1 = response.json("0.tokens.0.token");
});
group("function2", function(){
const url = "url2.com";
let payload = JSON.stringify({
"token": `${var1}`
});
};
1 Like
I see now that this was a poorly asked question…
I have created the user actions as functions outside the default function. This means that each group inside the default function calls these functions. I can’t seem to use the return value when it is inside the group:
export default function() {
// Declare variables here for global use
let var1;
group("function_1", function() {var1 = doPage1()});
group("function2", function(){doPage2()});
};
function doPage1 () {
let url = "url.com";
let response = http.get(url, params);
var1 = response.json("0.tokens.0.token");
return var1;
};
function doPage2 () {
const url = "url2.com";
let payload = JSON.stringify({
"token": `${var1}`
};
You need to provide it as an argument to the function in the second group like:
import { group } from "k6";
export default function() {
// Declare variables here for global use
let var1;
group("function_1", function() {var1 = doPage1()});
group("function2", function(){doPage2(var1)});
};
function doPage1 () {
return "something";
};
function doPage2 (var1) {
const url = "url2.com";
console.log(JSON.stringify({ "token": `${var1}`}));
};
$ k6 run -q --no-summary var.js
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾\
/ \ | |‾\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
output: -
script: var.js
duration: -, iterations: 1
vus: 1, max: 1
INFO[0000] {"token":"something"}
1 Like
Omg… thanks for pointing out the obvious:)
2 Likes