Is there a way to capture a value out of a test.step() in Playwright?
When using the Playwright Test Runner, you have access to test steps, which can be used when reporting steps of certain tests. Here you can define a title to describe the step, and the body which is a function that runs whatever code you want to run during this step.
One nice feature with this is from the test step, you are able to return a value from the function.
import { test, expect } from '@playwright/test';
import { faker } from "@faker-js/faker";
test('test', async ({ page }) => {
const userName = await test.step('Sign up', async () => {
const randomUsername =`Automation_${faker.name.lastName()}`;
await signUpPage.signUpRandomUser(randomUsername);
return randomUsername;
});
console.log(userName) //This would be the randomUsername that was set inside the test step.
});
In the above example I am able to randomly generate some data within a test step, and persist it to use later in my assertions, keepin my code DRY. A link to the playwright test.step() function is provided below.
Enjoy and Happy Testing!