Skip to content

Commit 7aeeec1

Browse files
Llandy3dheitortsergentgoing-confetti
authored
Update k6 Studio Recording your first script tutorial (#1922)
* update recording your first script * Apply suggestions from code review Co-authored-by: Uladzimir Dzmitračkoŭ <[email protected]> * Add step for validating script failure * Small edit * Remove second "Validate your test script" heading * Update screenshots * Update record-your-first-script.md * Small edit --------- Co-authored-by: Heitor Tashiro Sergent <[email protected]> Co-authored-by: Uladzimir Dzmitračkoŭ <[email protected]>
1 parent 2038237 commit 7aeeec1

File tree

1 file changed

+87
-57
lines changed

1 file changed

+87
-57
lines changed

docs/sources/k6-studio/record-your-first-script.md

Lines changed: 87 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ This tutorial shows how to create a recording, set up custom rules, and generate
1010

1111
In this tutorial, you will:
1212

13-
- Create a test recording using the https://test-api.k6.io/ service.
13+
- Create a test recording using the https://quickpizza.grafana.com/ service.
1414
- Learn how to create groups to organize your test recordings.
1515
- View the request and response data from a test recording.
16-
- Use the Generator to create a correlation and a custom code rule.
16+
- Use the Generator to create a correlation, parameterization, and a custom code rule.
1717
- Validate your test script from the Generator.
1818
- Export your test script.
1919

@@ -27,7 +27,7 @@ To complete this tutorial, you'll need to:
2727

2828
{{< admonition type="note" >}}
2929

30-
This tutorial uses the `test-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. The `test-api.k6.io` service is also [open source](https://github.com/grafana/test-api.k6.io) if youd like to deploy a private instance.
30+
This tutorial uses the `quickpizza.grafana.com` 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. The `quickpizza.grafana.com` service is also [open source](https://github.com/grafana/quickpizza) if you'd like to deploy a private instance.
3131

3232
{{< /admonition >}}
3333

@@ -37,44 +37,54 @@ To create a test recording:
3737

3838
1. Open the k6 Studio desktop application.
3939
1. Click **Record Flow**.
40-
1. Type "test-api.k6.io" under **Starting URL**.
40+
1. Type "quickpizza.grafana.com" under **Starting URL**.
4141
1. Click **Start recording**. You should see a Google Chrome window open after a few seconds, and the **Requests** header in the k6 Studio application.
4242

43-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-1-test-recorder-2.png" >}}
43+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-1-test-recorder-3.png" >}}
4444

4545
1. In k6 Studio, click the edit button next to **Default group**, rename it to "Homepage", and press **Enter**.
46-
1. 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.
47-
1. 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.
48-
1. For username, enter "studio-user". For password, enter "password".
46+
1. Next click **Create group**, name it "Generate pizza", 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.
47+
1. In the browser window, press the **Pizza, Please!** button. You should see a generated recommendation.
48+
1. Under the recommendation, press the **Love it!** button. A message saying, "Please log in first." will appear.
4949

50-
You'll be logged in to the test service, and this will complete the requests for the "Go to Create Crocs" group.
50+
This will complete the requests for the "Generate pizza" group.
5151

52-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-2-test-api-service-2.png" >}}
52+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-2-test-api-service-3.png" >}}
5353

54-
### Create a crocodile
54+
### Log in to the service
5555

56-
In this next step, you'll use the form for the test service to create a crocodile.
56+
In this next step, you'll log in to the service to be able to rate a pizza:
5757

58-
To create a crocodile:
58+
1. In the k6 Studio application, click **Create group**, name it "Log in", and press **Enter**.
59+
1. In the browser, in the top-right of the screen press the **Login/Profile** link. You should see an authentication dialog box.
60+
1. For username, enter "studio-user". For password, enter "k6studiorocks".
5961

60-
1. In k6 Studio, click **Create group**, name it "Create Croc", and press **Enter**.
61-
1. 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.
62+
You'll be logged in to the test service, and this will complete the requests for the "Log in" group.
6263

63-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-3-create-crocodile-2.png" >}}
64+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-3-test-api-service-login.png" >}}
6465

65-
1. Click **POST** to submit the API request.
66+
### Generate and rate a named pizza
67+
68+
In this next step, you'll generate and rate a pizza again, this time with a custom name.
69+
70+
1. In k6 Studio, click **Create group**, name it "Generate and rate custom pizza", and press **Enter**.
71+
1. In the browser, press the **Back to main page** link. You should see the homepage again.
72+
1. In the top-right of the page, toggle the `Advanced` input, a form will appear on the page to customize the generated pizza.
73+
1. In the form, insert "testedpizza" as a name in the **Custom Pizza Name** input, and press the **Pizza, Please!** button. You should see a generated recommendation with the same name.
74+
75+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-4-test-api-service-generate-pizza.png" >}}
76+
77+
1. Under the recommendation, press the **Love it!** button, a message with "Rated!" will appear.
6678
1. In k6 Studio, click **Stop recording**.
6779

6880
After you click **Stop recording**, k6 Studio saves the recording as a HAR file.
6981

70-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-4-save-test-recording-2.png" >}}
71-
7282
### Rename the recording
7383

7484
In the sidebar, you can change the name of the recording after it's created. To do that:
7585

7686
1. In k6 Studio, right click on the recording you just created in the left sidebar.
77-
1. Rename the recording to `create crocs`.
87+
1. Rename the recording to `generate pizza`.
7888
1. Press **Enter** to confirm the changes.
7989

8090
### Inspect response and request data
@@ -83,7 +93,7 @@ After you finish a recording, you can visualize the request and response data fo
8393

8494
To view the request and response data for a request, click on the request you would like to inspect from the **Requests** list.
8595

86-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-5-test-recording-inspector-2.png" >}}
96+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-5-test-recording-inspector-3.png" >}}
8797

8898
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.
8999

@@ -94,47 +104,84 @@ To generate a script from a test recording:
94104
- If you still have the test recording open from the last step, click **Create test generator** on the top-right.
95105
- You can also click **+** next to Generator on the left side, and then select your recording on the top-right.
96106

97-
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**.
107+
A dialog box shows up that lets you select the hosts to use from the recording for generating the script. Select `quickpizza.grafana.com` and press **Continue**.
98108

99-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-6-test-generator-allowed-hosts-2.png" >}}
109+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-6-test-generator-allowed-hosts-3.png" >}}
100110

101111
The Generator lets you generate and modify a k6 test script via the user interface, without having to write JavaScript code.
102112

103113
On the top, you can inspect the recording from this view, similar to the Recorder. On the bottom, you can see the list of **Test rules**, with a **Verification rule** already added.
104114

105-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-7-test-generator-2.png" >}}
115+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-7-test-generator-3.png" >}}
106116

107-
**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](https://grafana.com/docs/k6/latest/using-k6/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.
117+
**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](https://grafana.com/docs/k6/latest/using-k6/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 can be a good starting point to verify your application is working as expected.
108118

109119
You can also inspect the script that would be generated by selecting the **Script** tab in the top panel.
110120

121+
## Validate a test script
122+
123+
In this next step, you can validate the test script that was created from the recording to check if your script is working correctly. To validate a test script:
124+
125+
1. In k6 Studio, click on the **Script** tab next to **Requests**.
126+
1. Click **Validate** on the top-right.
127+
128+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-8-test-generator-validate.png" >}}
129+
130+
The 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.
131+
132+
For this tutorial, running the script that was created by the previous steps will result in a couple of requests that should be returning a 200 status code, returning a 401 status code instead.
133+
134+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-9-test-validator-failed-request.png" >}}
135+
136+
That happens because of two reasons:
137+
138+
1. The log in endpoints use a CSRF token to validate the request.
139+
2. The pizza rating endpoint uses a `pizza.id` property to rate the pizza that was randomly generated.
140+
141+
For both cases, these are common scenarios where APIs or services require information to be retrieved from an endpoint, and then used in subsequent requests.
142+
143+
In the next steps, you'll learn how to use rules to customize your script and make it work correctly.
144+
111145
## Correlate dynamic data
112146

113-
If you inspect the data of the **POST** request, you can see that it makes use of a `csrf` token.
147+
If you inspect the data of the **POST** request `/api/csrf-token`, you can see that it generates a `csrf` token.
114148

115-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-8-form-csrf-token-3.png" >}}
149+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-10-form-csrf-token.png" >}}
116150

117-
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 predict 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.
151+
That's an example of a dynamic value that you can't predict when generating the script because that value is generated by the server when you reach the log in form, and the server expects it back when you submit the form.
118152

119-
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**.
153+
You'll need a way to customize 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**.
120154

121155
To add a correlation rule:
122156

123157
1. In k6 Studio, click **+ Add rule** and select **Correlation** from the entries.
124-
1. Under **Extractor**, in the **Begin** field, type `csrfmiddlewaretoken" value="`.
125-
1. In the **End** field, type `"`.
158+
1. Under **Extractor**, set the **Target** field to **Headers**.
159+
1. In the **Begin** field, type `csrf_token=`.
160+
1. In the **End** field, type `;`.
126161

127-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-9-correlation-rule-2.png" >}}
162+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-11-correlation-rule.png" >}}
128163

129-
On the request list, you'll see that a `match` label appears next to a couple of requests. These are the requests that this particular rule is either extracting or replacing a value from.
164+
On the request list, you'll see that a `Value extracted` and `match` label appears next to a couple of requests. These are the requests that this particular rule is either extracting or replacing a value from.
130165
At the bottom of the rule editor, you can see the value that got extracted.
131166

132-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-10-rule-preview-2.png" >}}
133-
134167
k6 Studio updates the labels 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. When replacing the value, 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.
135168

136169
With this rule in place, you have added dynamic data correlation to your script without having to touch any code.
137170

171+
If you run the validation step again, you can see that one of the failing requests returns a 201 status code.
172+
173+
There's still one failing request related to the generated ID for the pizza not being extracted and used in the next request. To fix that:
174+
175+
1. In k6 Studio, click **+ Add rule** and select **Correlation** from the entries.
176+
1. Under **Extractor**, set the **Target** field to **Body**, set the **Type** field to **JSON**.
177+
1. In the **JSON path** field, type `pizza.id`.
178+
179+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-12-correlation-rule-pizza-id.png" >}}
180+
181+
This correlates the generated pizza ID in both the `Generate pizza` and `Generate and rate custom pizza` groups.
182+
183+
You can validate the test script again, and all the requests should be returning the correct status code, and the k6 checks should be passing.
184+
138185
## Parameterize a value
139186

140187
You can make use of the [parameterization rule](https://grafana.com/docs/k6-studio/components/test-generator/#parameterization-rule) to modify a value from some text, and replace it with a variable or even data from a CSV or JSON file.
@@ -143,10 +190,10 @@ To add a parameterization rule:
143190

144191
1. Under **Test rules**, click **+ Add rule** and select **Parameterization**.
145192
1. Change the **Type** field to `Text`.
146-
1. In the **Text** input, insert the name you used earlier when creating a crocodile.
147-
1. On the right-side, insert the new value you want to replace with under **Value**. For example `Grot`.
193+
1. In the **Text** input, insert the name you used earlier when generating the custom pizza.
194+
1. On the right-side, insert the new value you want to replace with under **Value**. For example `Grotpizza`.
148195

149-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-11-parameterization-rule.png" >}}
196+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-13-parameterization-rule.png" >}}
150197

151198
You'll see that the **POST** request from the recording now has the `match` label next to it. If you inspect the **Payload** you can see that it's now using the newly defined name.
152199

@@ -157,27 +204,10 @@ You can make use of the **Custom Code** rule to insert a custom JavaScript snipp
157204
1. In k6 Studio, click **+ Add rule** and select **Custom Code** from the entries.
158205
1. In the **Snippet** input, type `console.log('hello k6')`.
159206

160-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-12-custom-code-rule.png" >}}
207+
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-14-custom-code-rule.png" >}}
161208

162209
If you open the **Script** tab, you can see the script getting updated in real-time with the snippet being inserted before every request.
163210

164-
## Validate a test script
165-
166-
To validate a test script:
167-
168-
1. In k6 Studio, click on the `Script` tab next to `Requests`.
169-
1. Click **Validate** on the top-right.
170-
171-
The 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.
172-
173-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-13-test-validator.png" >}}
174-
175-
The checks generated by the default **Verification rule** that can give you a quick glimpse if the script is working as expected.
176-
177-
{{< figure src="/media/docs/k6-studio/tutorial/screenshot-k6-studio-tutorial-14-test-validator-checks.png" >}}
178-
179-
In the Validator, you can also inspect requests details similar to the Recorder and Generator, giving you a powerful tool to debug your script.
180-
181211
## Export a test script
182212

183213
To export a test script:
@@ -193,7 +223,7 @@ After you save the script, it'll show up under **Scripts** on the left side. You
193223

194224
To summarize:
195225

196-
- You created a test recording using the https://test-api.k6.io/ service.
226+
- You created a test recording using the https://quickpizza.grafana.com/ service.
197227
- You created groups to organize a test recording into logical steps.
198228
- You learned how to view request and response data from a test recording.
199229
- You created a Generator from a test recording, and learned how to use the correlation, parameterization, and custom code rules.

0 commit comments

Comments
 (0)