Playwright - Test Generator

BIPLAB NAYAKBIPLAB NAYAK
3 min read

🚀 Playwright Code Generator in Visual Studio Code

Writing end-to-end tests manually can be time-consuming, but Playwright’s Code Generator makes it easier by recording your browser interactions and converting them into test scripts automatically. If you're using Visual Studio Code (VS Code), you can leverage Playwright's built-in tools to generate test scripts seamlessly.

In this blog, we'll explore how to use Playwright’s Code Generator inside VS Code and optimize the generated tests for better maintainability.

---

🎯 Why Use Playwright Code Generator in VS Code?

✅ Faster Test Creation – Automatically generates test scripts while you interact with the browser.

✅ Eliminates Manual Scripting – No need to write repetitive steps manually.

✅ Supports Multiple Languages – Generate test scripts in TypeScript, JavaScript, Python, or C#.

✅ Works Directly in VS Code – No need to switch between tools.

---

🛠️ Setting Up Playwright in VS Code

Step 1: Install Playwright

First, ensure you have Playwright installed in your project. If not, open VS Code Terminal and run:

npm init playwright@latest

This will: Install Playwright.

Set up a basic test structure.

Install browsers (Chromium, Firefox, and WebKit).---

🏗️ Using Playwright Code Generator in VS Code

Option 1: Using the Terminal Command

Once Playwright is installed, open VS Code Terminal and run:

npx playwright codegen

This will:

✅ Open a browser window.

✅ Record your interactions.

✅ Generate test scripts in real-time inside VS Code.

To record a specific URL, use:

npx playwright codegen https://yourwebsite.com

💡 Tip: You can specify the output format using the --target flag:

npx playwright codegen --target=typescript

npx playwright codegen --target=python

---

Option 2: Using Playwright Extension in VS Code

Playwright has a VS Code extension that enhances your automation workflow.

📌 Install the Playwright Extension

1️⃣ Open VS Code.

2️⃣ Go to Extensions (Ctrl+Shift+X).

3️⃣ Search for Playwright Test for VS Code.

4️⃣ Click Install.

📌 Open the Code Generator

1️⃣ Open the Command Palette (Ctrl+Shift+P).

2️⃣ Search for Playwright: Open Code Generator.

3️⃣ Select the option, and Playwright will launch a browser to start recording.

---

✍️ Editing the Generated Test

Once the recording is complete, Playwright will generate a script similar to this:

import { test, expect } from '@playwright/test';

test('Login Test', async ({ page }) => {

await page.goto('https://example.com');

await page.click('text=Sign In');

await page.fill('input[name="username"]', 'testuser');

await page.fill('input[name="password"]', 'password123');

await page.click('text=Submit');

await expect(page).toHaveURL('https://example.com/dashboard');

});

You can edit and optimize the test by:

✅ Using better locators – Prefer data-testid or role selectors.

✅ Adding assertions – Ensure critical checks are present.

✅ Using Page Object Model (POM) – For better maintainability.

---

⚡ Advanced Usage

🎯 1️⃣ Record Tests in Headless Mode

By default, Playwright opens a browser UI. To record in headless mode, run:

npx playwright codegen --headless

🎯 2️⃣ Record Tests in a Specific Language

npx playwright codegen --target=python

Supported options: typescript, javascript, python, csharp.

🎯 3️⃣ Export the Script to a File

To save the generated script to a file:

npx playwright codegen https://example.com --output=login.spec.ts

---

🔥 Best Practices for Using Playwright Code Generator

✅ Modify Locators – Replace generic selectors with stable locators (data-testid, aria-label).

✅ Refactor the Code – Convert reusable code into functions or a Page Object Model (POM).

✅ Use Assertions – Add expect() statements to validate UI behavior.

✅ Parameterize Tests – Use variables instead of hardcoding values.

---

🎯 Conclusion

Playwright’s Code Generator is a powerful tool for automating UI testing effortlessly. Whether you're a beginner or an experienced tester, integrating it into VS Code enhances your workflow by allowing quick test creation and debugging.

By leveraging the VS Code extension and terminal commands, you can streamline test automation and boost productivity.

💡 Have you tried Playwright Code Generator in VS Code? Let me know your thoughts in the comments below! 🚀

0
Subscribe to my newsletter

Read articles from BIPLAB NAYAK directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

BIPLAB NAYAK
BIPLAB NAYAK