Monitoring as Code: Learn more about the Multistep Check Construct.
Multistep Check Construct
multistep-check.ts
Test Script
Multistep checks use Playwright’stest.step()
function to organize sequential operations:
check-group.spec.ts
Test Structure
Let’s look at the code above step-by-step, as it determines what our Multistep check will do. 1. Initial declarations: Import the Playwright test framework. 2. Define our headers: In many cases, you will have to authenticate when requesting data by providing authorization headers. Use environment variables to avoid having any confidential data in your test. 3. Establish environment: Create a new test and leverage the Playwrightrequest
fixture to make API requests in the test steps.
4. Declare our first test.step
: The test step uses the request
to perform a post
request, using the headers we defined earlier.
Always use
await
before test.step
, otherwise the test will fail.expect(response)
method to assert if the response was successful (the response code is in the range of 200 - 299) with toBeOK()
. Should the request return anything outside of the ‘OK’ range, the check will fail and in a production scenario, trigger any configured alerts.
6. Return the response for future usage: Return the request response in JSON format, so we can use it in the next test step.
7. Declare our second test.step
: In order to remove the test group we just created, and avoid cluttering our system with test data, remove it by sending a delete
request using the group ID that was returned in our earlier test step. Use the same toBeOK()
assertion as in the previous test step.
If you want to build on the above example, you can add additional assertions, ensuring that the data returned is correct and contains a specific check, or add a PUT
and GET
test step to verify more of the /check-groups
functionality.