Playwright Test Runner

Rate this content
Bookmark

FAQ

Playwright Test is a cross-browser web testing framework written in Node.js, allowing you to author tests in JavaScript or TypeScript. It is free, open source, and sponsored by Microsoft.

Playwright Test was created to address the unique challenges of end-to-end testing, such as cross-browser support, parallel test execution, and test isolation, which are not adequately handled by existing unit test runners.

Playwright Test offers unique APIs for test isolation, cross-browser testing, parallel test execution, and extensive configuration options. It also includes features inspired by PyTest fixtures.

To get started with Playwright Test, create a new folder, initialize a new npm project, and run 'npm init playwright'. This sets up the project to use Playwright, installs dependencies, and downloads necessary browsers.

You can write tests in Playwright Test using JavaScript or TypeScript. TypeScript tests will be transpiled to JavaScript and run seamlessly.

The Playwright Test configuration file allows you to set up different projects or environments to run tests in various browsers and devices, such as Chrome, Firefox, Safari, and mobile emulations for Android and iPhone.

Playwright Test runs tests in parallel using multiple workers, which speeds up test execution and saves time.

Playwright Test supports various reporters, including line reporters for interactive terminal prompts, HTML reporters for visual results, and third-party reporters like Allure.

Playwright Codegen is a tool that records user interactions in the browser to automatically generate test scripts. You can run 'npx playwright codegen' to open a browser and start recording actions.

You can debug tests in Playwright Test using the Playwright Inspector. By running tests with the '--debug' flag, you can step through the code, inspect the DOM, and view logs of test actions.

Playwright Tracing captures detailed information about test runs, including actions, events, screencasts, network logs, and DOM snapshots. This information is stored in trace files and can be viewed using the TraceViewer for post-mortem debugging.

Andrey Lushnikov
Andrey Lushnikov
25 min
18 Nov, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Playwright Test Runner is a cross-browser web testing framework that allows you to write tests using just a few lines of code. It supports features like parallel test execution, device emulation, and different reporters for customized output. Code-Gen is a new feature that generates code to interact with web pages. Playwright Tracing provides a powerful tool for debugging and analyzing test actions, with the ability to explore trace files using TraceViewer. Overall, Playwright Test offers installation, test authoring, debugging, and post-mortem debugging capabilities.
Available in Español: Playwright Test Runner

1. Introduction to Playwright Test Runner

Short description:

Hi everybody, my name is Andrej, and today I would like to introduce you to the new Playwright Test Runner. Playwright Test is a cross-browser web testing framework written in Node.js. It is free and open source, sponsored by Microsoft, and extensively used in the industry. Playwright Test was built specifically to address all end-to-end testing needs. Let's jump straight into the first chapter, which is getting started. I will create a new folder, initialize a new npm project using Playwright, and answer some questions to set up the project.

Hi everybody, my name is Andrej, and today I would like to introduce you to the new Playwright Test Runner. So let's start.

So first thing first, what is Playwright Test? Well, it is a cross-browser web testing framework written in Node.js, which basically means that you can author your tests in JavaScript or TypeScript. Now it is free and open source and sponsored by Microsoft and is already extensively used in the industry. Now this is all good, but it doesn't answer the big looming question. So why did we do yet another Test Runner? So honestly, we did not intend to. There is a lot of different test runners in JavaScript, turns out they are all unit test runners. Whereas in end-to-end testing, there's lots of very hard challenges. So for example, you want your test runner to support cross-browser tests out of the box. You want them to be parallelized out of the box as well. And you want them to be isolated, and Playwright, for example, has a set of unique APIs to make sure that test isolation is fast and reliable. And of course, we want these to be used. And last but not least, history shows that end-to-end tests require a crazy level of flexibility and configuration, because there are many different configurations we want to use to run the same test. And Vivio is inspired by PyTest fixtures, so fixtures are implemented in PlayWriteTest as well. So PlayWriteTest is our answer to end-to-end tests. And this is the test runner that was built specifically to address all of the end-to-end testing needs.

So we have quite a lot to talk about in this talk, and we have just 20 minutes. So let's jump straight into the first chapter, which is getting started. Okay, so the best way to show something is actually to show something. So let me hop to my terminal and I will create a new folder. And I will start from scratch and initialize a new npm project. Now let's do npm init PlayWrite. And what this does is actually sets up this project to use PlayWrite for me. While doing so, it will ask me all kinds of different questions. For example, what language do I want to use to author tests? And it is TypeScript because I actually don't need to set up anything additional, I can just author my tests in TypeScript and they will be transpiled to JavaScript and run seamlessly, no need to do Babel and Webpack or anything. It will ask me where to put my tests, of course I want to have a GitHub Actions workflow, and yeah, examples are good. So now once I answered all the questions, it installs all the dependencies for the NPM, downloads all the browsers, I have them downloaded already so these have been reused. And now I am here with a bunch of sources pre-created for me. Let's look into these sources. First we have this example end-to-end test.

2. Playwright Test Runner: Features and Configuration

Short description:

The Playwright Test Runner allows you to write tests using just a few lines of code. It provides a well-isolated test page for each test, ensuring no clashes between pages or state. You can navigate the test page to a specific website, locate elements, and interact with them. The configuration file allows you to set up different environments, such as running tests in different browsers. The Playwright Test Runner supports cross-browser testing, device emulation, and parallel test execution out of the box.

It is just seven lines of code and let's pretend that we don't know what Playwright is, let's just read it. So first line we import something, test and expect from the package we just installed. Now test turned out to be a function and we use it to create a test. We give it a basic name and we pass in a callback which is actually a test body. Interestingly as an argument in the test body, we have this page that comes from the Playwright test framework. This is a well isolated test page that is different for every test and no tests have pages or state clashing. So this is the isolation we talked about. This is a browser page so we can navigate it to a certain website and then we can locate an element using a text on the page and after that we can click what you located. And everything is async so we wait for it to complete. It looks like it does some kind of navigation so after this we can expect the page to have a certain title like getting started regex. Pretty transparent.

OK what else do we have? We have this configuration file. This time I will actually use Wim to go into it. Now this is a generated file and it has lots of comments so you can easily go through it and understand what's going on. We don't need any of this stuff. I want to focus on this projects array. Now projects are kind of environments. We have only one test and we want to run this test in different browsers like Firefox or in Chrome and this is what projects are for. So here we have five projects pre-setup for us. We have Desktop Chrome, Desktop Firefox, Desktop Safari. All good. And we even have mobile emulation for Pixel 5 which is Android and iPhone 12 which is a Mobile Safari. So we have one test and five projects which means we'll run five times for this test. Let's see how this actually happens. So to run tests I can do mbxplaywrite test and as I run this command it runs five tests using five workers. So we will write that it actually runs five tests which is nice. But the other important thing is that it actually uses five workers. So all of these test runs are happening in parallel and actually only take eight seconds which is pretty fast. OK so out of the box we didn't do anything. We already got cross browser test runs, we got device emulation and we got parallels.