Skip to content
Vitaliy Potapov
Go back

Introducing Playwright-magic-steps: Simplify your Test Automation Workflow

Intro

Hey Folks👋

Recently I’ve released playwright-magic-steps - a tool to make your Playwright tests cleaner and easier to read. The main idea is to define steps by JavaScript comments and then automatically transform them into test.step() calls. That helps to maintain a straightforward and organized test code while providing detailed step reports.

How It Works

Here’s a quick example to illustrate the transformation:

Original Test Code

test('Check home page', async ({ page }) => {
// step: Open home page
await page.goto('https://playwright.dev');
// step: Click "Get started" link
await page.getByRole('link', { name: 'Get started' }).click();
// step: Check page title
await expect(page).toHaveTitle('Installation | Playwright');
});

Transformed Test Code

test('Check home page', async ({ page }) => {
await test.step('Open home page', async () => {
await page.goto('https://playwright.dev');
});
await test.step('Click "Get started" link', async () => {
await page.getByRole('link', { name: 'Get started' }).click();
});
await test.step('Check page title', async () => {
await expect(page).toHaveTitle('Installation | Playwright');
});
});

The transformation is performed behind the scene, so you deal only with comments.

Do you notice how traditional test.step() functions clutter your code with visual complexity and nesting?

How to Setup

Magic steps setup depends on your project module system.

CommonJS Projects

Add the following code to your Playwright config:

import 'playwright-magic-steps'; // <- enables magic steps
import { defineConfig } from '@playwright/test';
export default defineConfig({
// your config
});

Then run as usual:

npx playwright test

ESM Projects

Run Playwright with the following NODE_OPTIONS containing custom loader:

npx cross-env NODE_OPTIONS="--import playwright-magic-steps/esm" playwright test

Report

After test execution Playwright report contains all defined steps:

Report example

Under the Hood

Playwright-magic-steps operates by intercepting the code of test files and replacing comments with test.step() calls. The interception process differs for CommonJS and ESM projects:

Important Considerations

Conclusion

Following the Golden Rule of testing, we should keep test code as simple as possible, to save brain capacity for production code. I hope magic steps will help to achieve that. The project is new and still experimental, so give it a try and share your feedback ❤️


Share this post:

Previous Post
Solving a problem of duplicate steps in Cucumber BDD testing
Next Post
✨ Generate BDD tests with ChatGPT and run them with Playwright