Using Playwright for E2E testing

What is Playwright?

Playwright is a popular cross-browser automation and E2E testing framework, developed by Microsoft. It provides support for 3 different browser engines, including Chromium, Firefox, and WebKit. It also supports multiple programming languages, including JavaScript, TypeScript, Python, .NET and Java.

Playwright is noted for its fast and stable approach to interacting with web browsers, making it an attractive option for E2E testing. The framework has key features such as one-time login, web-first approach, codegen, and auto-wait that make it suitable for E2E testing tasks. Additionally, it has an integrated test runner called Playwright Test, which makes it easier for developers to conduct E2E tests.

Here is an example of how to set up a sample project with Playwright:

// Open a new terminal and run
$ git clone https://github.com/kyleaday/react-app-playwright
$ cd react-app-playwright
$ npm install

We can extend the Playwright config using a playwright.config.js file such as below:

// e2e/playwright.config.js
import { chromium, firefox, webkit, devices } from 'playwright';

const iPhone = devices['iPhone 6'];

module.exports = {
    browserType: webkit,
    launchConfig: {
        headless: false,
        slowMo: 300
    },
    contextConfig: {
        viewport: iPhone.viewport,
        userAgent: iPhone.userAgent
    }
};

For a full list of configuration options, you can visit this link

Why Playwright and not Cypress for E2E?

Both Playwright and Cypress are popular end-to-end (E2E) testing frameworks for web applications.

Playwright is a Node.js library for automating modern browsers, including Chrome, Firefox, Safari, and Edge. It provides a high-level API for performing E2E tests and supports features like automatic waiting, headless testing, and parallel test execution.

Cypress is a JavaScript-based E2E testing framework that runs directly in the browser, providing fast feedback and making it easy to test and debug web applications. It has a tight integration with the browser and can access the DOM directly, making it easier to test complex interactions and animations.

In terms of performance, Cypress tends to be faster than Playwright due to its direct access to the browser. On the other hand, Playwright supports a wider range of browsers and is better suited for testing complex, multi-page applications that involve multiple contexts and frames.

Ultimately, the choice between Playwright and Cypress comes down to your specific testing requirements and the type of application you are testing. Both frameworks have their own strengths and weaknesses, and the best choice for you will depend on the specifics of your project.

Conclusion

In conclusion, Playwright is a powerful and versatile end-to-end (E2E) testing framework that offers a high-level API and a range of features to automate the testing of modern browsers. Its support for multiple browsers, automatic waiting, and parallel test execution make it well-suited for testing complex, multi-page applications. Its ease of use, flexibility, and ability to handle dynamic and changing web environments make it a popular choice for many organizations and developers looking for a reliable and efficient way to perform E2E testing.