Wednesday, March 5, 2025

Getting Started with Playwright for Beginners

Playwright is a powerful end-to-end testing framework for web applications, developed by Microsoft. It allows you to automate browsers like Chrome, Firefox, and Safari, and is a great tool for testing modern web applications. If you're a beginner, here's a guide to get started with Playwright:

For more details visit: https://playwright.dev/docs/intro

1. Prerequisites

Before you start, ensure you have the following:

      Node.js installed (version 12 or higher). You can download it from Node.js official website.

      A basic understanding of JavaScript or TypeScript.

 

2. Install Playwright

1.               Open your terminal or command prompt.

2.               Run the following command to install Playwright:

npm init playwright@latest

3.               This command will:

      Create a new project folder.

      Install Playwright and its dependencies.

      Set up a basic configuration for you.

4.               Follow the prompts to configure your project. You can choose the language (JavaScript or TypeScript) and the browsers you want to test (Chromium, Firefox, WebKit).

 

3. Project Structure

After installation, your project will have the following structure:

my-playwright-project/ 

├── tests/                # Folder for your test files 

├── playwright.config.js  # Configuration file 

├── package.json          # Project metadata and dependencies 

└── node_modules/         # Installed dependencies

 

4. Writing Your First Test

  •  Create a new test file in the tests folder, e.g., example.spec.js.
  • Add the following code to test a simple webpage:

const { test, expect } = require('@playwright/test');  

test('basic test', async ({ page }) => {    

// Navigate to a website     

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

// Check if the title contains "Example Domain"     

const title = await page.title();     

expect(title).toBe('Example Domain');      

// Take a screenshot     

await page.screenshot({ path: 'screenshot.png' }); 

});

 

5. Running Your Test

  •  Run the following command in your terminal:

                npx playwright test

  •  Playwright will execute your test and display the results in the terminal.

 

6. Playwright Features to Explore

      Auto-waiting: Playwright automatically waits for elements to be ready before interacting with them.

      Cross-browser testing: Test your app on Chromium, Firefox, and WebKit.

      Headless mode: Run tests without opening a browser window (default).

      Trace Viewer: Debug your tests with detailed traces.

 

7. Debugging Tests

      Run tests in headed mode (with a visible browser):

                npx playwright test --headed

      Use the --debug flag to pause and inspect:

                npx playwright test --debug

 

8. Playwright Test Generator

Playwright has a built-in code generator to help you write tests:

  • Run the following command:

            npx playwright codegen

  •  A browser window will open. Interact with the page, and Playwright will generate the code for you.

 

9. Playwright Configuration

The playwright.config.js file allows you to customize settings like:

      Default browser

      Test timeout

      Base URL

      Screenshot and video recording options

Example configuration:

const { defineConfig } = require('@playwright/test');  

module.exports = defineConfig({   

use: {     headless: true, // Run tests in headless mode     

screenshot: 'on', // Take screenshots on failure     

video: 'retain-on-failure', // Record video on failure   

}, });

 

10. Learning Resources

      Playwright Official Documentation

      Playwright GitHub Repository

      Tutorials and guides on YouTube or blogs.

 

Next Steps

Once you're comfortable with the basics, explore advanced features like:

      Parallel test execution

      API testing

      Custom test fixtures

      CI/CD integration

Playwright is a beginner-friendly yet powerful tool for web testing. Happy testing! 🚀


No comments:

Post a Comment