Playwright Spec action¶
This action contains one or several Playwright tests, it can only be used from inside a Playwright virtual user.
Script¶
When you create a new Playwright Spec action it comes with a default script that visits the home page, the Fish category and a product of OctoPet, our public demo shop, along with the aggregators that name its resources:
import { test, expect } from '@playwright/test';
const APP = (process.env.HOST ?? 'https://pet.octoperf.com') + '/' + (process.env.MODE ?? 'fixed') + '/app/';
test('OctoPet', async ({ page }) => {
await page.goto(APP);
await expect(page.getByRole('heading', { name: 'Meet your new best friend.' })).toBeVisible();
await page.goto(APP + 'catalog/FISH');
await expect(page.getByRole('heading', { level: 1, name: 'Fish' })).toBeVisible();
await page.goto(APP + 'products/FI-SW-01');
await expect(page.getByRole('heading', { level: 1, name: 'Angelfish' })).toBeVisible();
});
Info
More details about the creation process of this script can be found on our blog.
For a more complete overview of all the scripting possibilities, check Playwright official documentation.
OctoPerf provides code auto-completion when using CTRL+Space from inside the script:
Variables¶
Read¶
Variables can be used inside a PlayWright action using the following syntax: process.env.variableName where variableName is the name of your variable. Here is a real life example:
await page.locator('input[name="username"]').click();
await page.locator('input[name="username"]').fill(process.env.login);
await page.locator('input[name="username"]').press('Tab');
await page.locator('input[name="password"]').fill(process.env.password);
In this case, both values will be replaced with the variables upon runtime:
Info
In order to use variables inside text for instance in the title of your test, you can use the following:
test(`test with ${process.env.login}`, async ({page}) => {
Write¶
While inside a PlayWright action, you can also write variables to be saved for later using setVariableValue("name", "value") simply replace name and value with the variable. The same is true for properties using: setPropertyValue("name", "value")
A typical use case would be to use the following code to extract the value of an input inside the page and then write it to a variable for later use:
const sourcepageLocator = page.locator('input[name="_sourcePage"]').nth(0);
const sourcepage = await sourcepageLocator.getAttribute('value');
setVariableValue('_sourcePage',sourcepage);
Info
You can change the nth(0) to another value to get a different occurrence of this input, in case there are several. For more details, check PlayWright documentation.
Files¶
It is possible to use files that were placed in the project files menu, using the path ../resources/.
Here's an example of how you would use them:
await page.locator('input[type="file"]').nth(0).setInputFiles('../resources/myfile.txt');
Core Web Vitals¶
During a load test, OctoPerf measures the Core Web Vitals of every page your script loads. There is nothing to add to the script: the measurement is always active.
| Metric | Measures | Unit |
|---|---|---|
LCP (Largest Contentful Paint) |
when the main content of the page is displayed | milliseconds |
FCP (First Contentful Paint) |
when the first content of the page is displayed | milliseconds |
TTFB (Time To First Byte) |
when the first byte of the page is received | milliseconds |
INP (Interaction to Next Paint) |
how fast the page reacted to the clicks and key presses made on it | milliseconds |
CLS (Cumulative Layout Shift) |
how much the content moves while the page loads | score, no unit |
Each measure belongs to the action that loaded the page: a page.goto(), or a click on a link that opens another page. An assertion or a hook never carries Web Vitals.
The Web Vitals show in the report as custom metrics, filtered by action like the response times.
An SLA profile judges them on Google's bands: a Web Vital that needs improvement raises a warning, a poor one a critical alarm. The Playwright sample Virtual User comes with a Core Web Vitals SLA profile attached.
Info
LCPandINPare measured by Chromium-based browsers; other browsers may report only part of the metrics.INPonly exists when the page receives interactions: a page that is only displayed has noINP.- The pages of a browser context created by the script itself, with
browser.newContext(), are not measured. - A validation of the Virtual User does not report Web Vitals: run a test to get them.
Debug¶
In order to debug a Playwright virtual user we are first going to create two script actions as described in the tabs below:
OctoPet order
import {test, expect} from '@playwright/test';
test('order', async ({page}) => {
await page.goto('https://pet.octoperf.com/fixed/app/');
await page.getByRole('navigation', {name: 'Main'}).getByRole('link', {name: 'Fish', exact: true}).click();
await page.getByRole('link', {name: 'Angelfish'}).click();
await page.getByRole('radio', {name: /Small.*Item EST-2\b/}).check();
await page.getByRole('button', {name: 'Add to Bag'}).click();
await page.getByRole('link', {name: 'Check Out'}).first().click();
await page.getByLabel('Username').fill('user1');
await page.getByLabel('Password').fill('pass');
await page.getByRole('button', {name: 'Sign in'}).click();
await page.getByRole('button', {name: 'Place Order'}).click();
await expect(page.getByRole('heading', {name: 'Thank you. Your order is placed.'})).toBeVisible();
});
import {test, expect} from '@playwright/test';
test('order fail', async ({page}) => {
await page.goto('https://pet.octoperf.com/fixed/app/');
await page.getByRole('navigation', {name: 'Main'}).getByRole('link', {name: 'Fish', exact: true}).click();
await page.getByRole('link', {name: 'Angelfish'}).click();
await page.getByRole('button', {name: 'Add to Cart'}).click();
});
The second script fails: the button reads Add to Bag, and Playwright waits for an Add to Cart button that never shows.
Now when you run a validation, the Debug section of each action lists its tests. Click one to see its duration, its status, its attachments and the network resources it loaded. Here, the validation of the OctoPet SPA sample:
Warning
If you are getting certificates errors like ERR_CERT_AUTHORITY_INVALID make sure to add the proper configuration in order to ignore https errors in your script.
Trace¶
A nice way to see the details of a run is to use the Playwright Trace viewer. In OctoPerf you use the trace button or when you click on the icon next to your playwright action name in the debug menu. This opens the trace viewer in a new tab:
Note
Each trace is also available along with the logs and files of the validation.
Aggregators¶
With no aggregation, results like response times will be reported for every hostname of your playwright script. That means any URL with a dynamic parameter won't be reported individually.
Aggregators allow you to bypass that and group requests based on a regex expression. That way you can have the averaged values for a group of similar requests.
To experiment with aggregators, it is best to use the debug tab after a validation, the difference is visible using the Raw/Aggregated buttons in the bottom section:
For example, the OctoPet SPA sample names its resources with these aggregators:
| Regular Expression | Template |
|---|---|
/api/carts/[^/]+/(claim|lines) |
API cart $1$ |
/api/carts/[^/?]+$ |
API cart |
/api/orders/[0-9]+ |
API order |
/api/products/[^/?]+ |
API product |
/api/categories/[^/]+/products |
API category products |
/api/(account|auth/login|carts|categories|orders) |
API $1$ |
/app/(chunk|main|polyfills|styles)- |
SPA $1$ |
/app/$ |
SPA index |
/assets/(categories|products)/ |
Pictures of $1$ |
/assets/ |
Static assets |
Aggregators are executed in sequence, and a resource takes the first one it matches. So put the most precise ones first: here /api/carts/[^/]+/(claim|lines) comes before /api/carts/[^/?]+$, and /assets/ comes last, to name the assets that no other aggregator matched.
Warning
There is a hard limit on the number of network resources that a Playwright action can generate. Use aggregators to get detailed metrics for your website or application only (not for external resources like CDN calls, analytics tags, etc.).





