Record your first script
This tutorial shows how to create a recording, set up custom rules, and generate a test script using k6 Studio.
In this tutorial, you will:
- Create a test recording using the https://test-api.k6.io/ service.
- Learn how to create groups to organize your test recordings.
- View the request and response data from a test recording.
- Use the Test Generator to create a correlation and a custom code rule.
- Validate your test script from the Test Generator.
- Export your test script.
Before you begin
To complete this tutorial, you’ll need to:
- Have a Windows or macOS machine.
- Install k6 Studio.
- Install Google Chrome.
Note
This tutorial uses thetest-api.k6.io
service, which is a public shared environment. You can use it and follow along this tutorial, or you can use a service that you own. Thetest-api.k6.io
service is also open source if you’d like to deploy a private instance.
Create a test recording
To create a test recording:
Open the k6 Studio desktop application.
Click Record Flow.
Type “test-api.k6.io” under Starting URL.
Click Start recording. You should see a Google Chrome window open after a few seconds, and the Requests header in the k6 Studio application.
In k6 Studio, click the edit button next to Default group, rename it to “Homepage”, and press Enter.
Next click Create group, name it “Go to Create Crocs”, and press Enter. Groups can help organize your test scripts so they’re easier to edit and maintain in the future, and give you timing metrics for each group in your test script.
In the browser window, under Private APIs, click on /my/crocodiles next to the POST method. You should see an authentication dialog box in the next screen.
For username, enter “studio-user”. For password, enter “password”.
You’ll be logged in to the test service, and this will complete the requests for the “Go to Create Crocs” group.
Create a crocodile
In this next step, you’ll use the form for the test service to create a crocodile.
To create a crocodile:
In k6 Studio, click Create group, name it “Create Croc”, and press Enter.
In the browser window, you should have successfully authenticated in the previous step and be able to see the My Crocodiles page. Fill out the Name, Sex, and Date of birth fields of the HTML form section.
Click POST to submit the API request.
In k6 Studio, click Stop recording.
After you click Stop recording, k6 Studio saves the recording as a HAR file.
Rename the recording
In the sidebar, you can change the name of the recording after it’s created. To do that:
- In k6 Studio, right click on the recording you just created in the left sidebar.
- Rename the recording to
createc crocs
. - Press Enter to confirm the changes.
Inspect response and request data
After you finish a recording, you can visualize the request and response data for each request included in your test recording.
To view the request and response data for a request, click on the request you would like to inspect from the Requests list.
The Request and Response panels have tabs where you can view the headers, payload, or cookies for a request, and the headers, content, and cookies for a response. For the response content tab, you can also view it in different formats, such as the raw data, or a preview.
Generate a script from a test recording
To generate a script from a test recording:
- If you still have the test recording open from the last step, click Create test generator on the top-right.
- You can also click + next to Test Generator on the left side, and then select your recording on the right side under Requests.
A dialog box shows up that lets you select the hosts to use from the recording for generating the script. Select test-api.k6.io
and press Continue.
The Test Generator lets you generate and modify a k6 test script via the user interface, without having to write JavaScript code.
On the right side, you can inspect the recording from this view, similar to the Test Recorder. On the left side, you can see the list of Test rules, with a Verification rule already added. And on the right side, you can inspect the recording, similar to the Test Recorder view.
Test rules are objects that you can add to your generator file to modify the script generated from the recording. The Verification rule that’s created by default modifies the script to add Checks. These checks verify that when you run your script, the status codes you receive from responses are the same as the one you previously recorded. That’s a nice safety measure for your system that you get by default.
You can also inspect the script that would be generated by selecting the Script tab in the right panel where you can see the requests.
Correlate dynamic data
If you inspect the data of the POST request, you can see that it makes use of a csrf
token.
This is a token that was generated from a previous request and that was present in the form for security reasons. That’s an example of a dynamic value that you can’t possibly know when generating the script because that value is generated by the server when you reach the form, and the server expects it back when you submit the form.
You’ll need a way to generate the script so that it knows to get this value at runtime and replace the value from the recording from this extraction. You can do that by using the Correlation Rule.
To add a correlation rule:
- In k6 Studio, press + Add rule and select Correlation from the entries.
- Under Extractor, in the Begin field, type
csrfmiddlewaretoken" value="
. - In the End field, type
"
.
On the right side, you can notice that a new panel appears called Rule preview. This panel shows the request that matches the Filter field for the value extraction, the extracted value, and the matched requests where this value was replaced with the runtime correlation.
The Rule preview panel updates in real-time as you edit your rule. It’s useful to see when you actually have a match and where you are actually replacing values. For the extraction, only the first request matched will extract the value. For replacing it, by default the rule will try to find occurrences of that value and automatically replace those. If you need more control over it you can open the toggle to customize the replacer selector.
With this rule in place, you have added dynamic data correlation to your script without having to touch any code.
Insert a custom JavaScript snippet
You can make use of the Custom Code rule to insert a custom JavaScript snippet in your script.
- In k6 Studio, press + Add rule and select Custom Code from the entries.
- In the Snippet input, type
console.log('hello k6')
.
If you have the script panel open, you can see the script getting updated in real-time with the snippet inserted.
Validate a test script
To validate a test script:
- In k6 Studio, click the menu options icon on the top right.
- Click Validate script.
The Test Validator runs your script in a single iteration and lets you inspect the requests and responses sent, and see any logs and checks from k6.
The checks generated by the default Verification rule that can give you a quick glimpse if the script is working as expected.
In the Test Validator, you can also inspect requests details similar to the Test Recorder and Generator, giving you a powerful tool to debug your script.
Export a test script
To export a test script:
- In k6 Studio, click the menu options icon on the top right.
- Click Export script.
- Enter a name for the script.
- Click Export.
After you save the script, it’ll show up under Scripts on the left side. You can right click on the script and click Open containing folder to find it in your system, and then use k6
to run it.
Summary
To summarize:
- You created a test recording using the https://test-api.k6.io/ service.
- You created groups to organize a test recording into logical steps.
- You learned how to view request and response data from a test recording.
- You created a Test Generator from a test recording, and learned how to use the correlation and custom code rules.
- You learned how to validate a test script after setting up rules in the Test Generator.
- You exported a test script from your test recording and rules setup.
Now you can use the same steps to create a test recording for an application or service that you own and generate test scripts. You can then use those scripts to run performance tests by using the k6 CLI, or Grafana Cloud k6.
Next steps
Now that you learned the basics of how to use k6 Studio, you can:
- Learn how to install and run a test using the k6 CLI.
- Learn how to run your test using Grafana Cloud k6.