Skip to content

Commit ed30777

Browse files
[synthetics] Document the request parameter (#2071)
* initial approach * move from params/secrets to write a test * remove detailed examples * add info on how synthetics request is different than playwright request * final clean up
1 parent b5c3a1a commit ed30777

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

docs/en/observability/synthetics-create-test.asciidoc

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The callback provides access to fresh Playwright `page`, `params`, `browser`, an
7878

7979
[source,js]
8080
----
81-
journey('Journey name', ({ page, browser, context, params }) => {
81+
journey('Journey name', ({ page, browser, context, params, request }) => {
8282
// Add steps here
8383
});
8484
----
@@ -94,6 +94,9 @@ journey('Journey name', ({ page, browser, context, params }) => {
9494
For example, if you want to use a different homepage depending on the `env`
9595
(`localhost` for `dev` and a URL for `prod`). See <<synthetics-params-secrets>>
9696
for more information.
97+
`request`:: A request object that can be used to make API requests independently of the browser
98+
interactions. For example, to get authentication credentials or tokens in service of a
99+
browser-based test. See <<synthetics-request-param>> for more information.
97100

98101

99102
[discrete]
@@ -107,7 +110,7 @@ A basic two-step journey would look like this:
107110

108111
[source,js]
109112
----
110-
journey('Journey name', ({ page, browser, client, params }) => {
113+
journey('Journey name', ({ page, browser, client, params, request }) => {
111114
step('Step 1 name', () => {
112115
// Do something here
113116
});
@@ -176,6 +179,37 @@ For more details on getting started with the Synthetics Recorder, see <<syntheti
176179

177180
image::images/synthetics-create-test-script-recorder.png[Elastic Synthetics Recorder after recording a journey and clicking Export]
178181

182+
[discrete]
183+
[[synthetics-request-param]]
184+
== Make API requests
185+
186+
You can use the `request` parameter to make API requests independently of browser interactions.
187+
For example, you could retrieve a token from an HTTP endpoint and use it in a subsequent webpage request.
188+
189+
[source,js]
190+
----
191+
step('make an API request', async () => {
192+
const response = await request.get(params.url);
193+
// Do something with the response
194+
})
195+
----
196+
197+
The Elastic Synthetics `request` parameter is similar to https://playwright.dev/docs/api/class-apirequestcontext[other request objects that are exposed by Playwright]
198+
with a few key differences:
199+
200+
* The Elastic Synthetics `request` parameter comes built into the library so it doesn't
201+
have to be imported separately, which reduces the lines of code needed and allows you to
202+
make API requests in inline journeys.
203+
* The top level `request` object exposed by Elastic Synthetics has its own isolated cookie storage
204+
unlike Playwright's `context.request` and `page.request`, which share cookie storage
205+
with the corresponding https://playwright.dev/docs/api/class-browsercontext[`BrowserContext`].
206+
* If you want to control the creation of the `request` object, you can do so by passing options
207+
via <<elastic-synthetics-command, `--playwright-options`>> or in the
208+
<<synthetics-configuration, `synthetics.config.ts` file>>.
209+
210+
NOTE: The `request` parameter is not intended to be used for writing pure API tests. Instead, it is a way to support
211+
writing plain HTTP requests in service of a browser-based test.
212+
179213
[discrete]
180214
[[before-after]]
181215
== Set up and remove a global state

docs/en/observability/synthetics-params-secrets.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ These options are discussed in detail in the sections below.
5353
[discrete]
5454
[[synthetics-dynamic-configs]]
5555
// lint ignore params
56-
== Using a config file to set params
56+
== Use a config file to set params
5757

5858
Use a `synthetics.config.js` or `synthetics.config.ts` file to define variables your tests always need to be defined.
5959
This file should be placed in the root of your synthetics project.
@@ -79,7 +79,7 @@ Note that we use the `env` variable in the above example, which corresponds to t
7979
[discrete]
8080
[[synthetics-cli-params]]
8181
// lint ignore params
82-
== Using CLI arguments to set params
82+
== Use CLI arguments to set params
8383

8484
To set parameters when running `elastic-synthetics` on the command line, use the `--params` or `-p` flag to the `elastic-synthetics` program. The provided map is merged over any existing variables defined in the `synthetics.config.{js,ts}` file.
8585

@@ -88,7 +88,7 @@ To override the `url` parameter, you would run: `elastic-synthetics . --params '
8888
[discrete]
8989
[[synthetics-hb-params]]
9090
// lint ignore params
91-
== Using {heartbeat} options to set params
91+
== Use {heartbeat} options to set params
9292

9393
When running via {heartbeat} use the `params` option to set additional parameters, passed through the `--params` flag
9494
mentioned above and have their values merged over any default values. In the example below we run the `todos` app, overriding the `url`

0 commit comments

Comments
 (0)