How to get user input using k6?

Hi

I’d like the user of my load testing script to be able to input a string before the script is ran.

Is this possible somehow? I tried readline() but that produced an “not defined” error.

Sure, you can pass environment variables to k6 script via the --env key=value flag, and then access their values via __ENV[key] in your script. You can read more about it here: Environment variables

And if you want to actually input things in the terminal, you can wrap the k6 run call in a shell script like this:

echo "Type whatever data you need:"

read mydata

k6 run --env mydata="$mydata" script.js
2 Likes