UI Test Generation with playwright-cli

Playwright is a UI automation tool for Node.js with support for all major browsers, capable of running in Chromium, Firefox and WebKit-based clients. It also supports several test runners like Jest, Jasmine and Mocha.

The Playwright library gives the user powerful capabilities with little effort. Test creation can be further facilitated with playwright-cli, which gives the ability for capture-and-replay with code generation.

Let’s begin with the setup of our environment. We first need to initialize our Node.js package in the terminal/console. To simplify, we will just skip the questionnaire.

// In your project folder run
npm init -y // If you are using npm
yarn init -y // or yarn

With our package.json created, we can install the playwright and playwright-cli libraries, besides adding the packages, it will also download Chromium, Firefox and WebKit binaries automatically. We can add the dependencies using the command:

npm i -D playwright playwright-cli
yarn add -D playwright playwright-cli

Automation is easy, we only need to run the following command, and the library will record and print in the console our code.

npx playwright-cli codegen YOUR_WEBSITE

After recording, we can simply copy-and-paste the result code in a .js file. To run the file just use the following command in the terminal.

node index.js // Your filename

The first test we will record is a simple login and like flow on Instagram.

p.s. Please consider the testing ethics, we are only using Instagram to run a simple UI test, using existing websites to run more complex and demanding tests is something that should not be done.

npx playwright-cli codegen instagram.com
Our Instagram automation running

Our result code will be:

Now we will test a game, Antimatter Dimensions is my favorite idle game and the web version is open source. We will set up the game theme, and start by buying the first dimension and getting our first achievement.

As you can see is incredibly easy to setup, start recording and using the code. Creating test cases using test runners like Jest is also a breeze to setup and can even simplify your test cases.

It also has a good and very readable documentation and API reference. The codebase is well maintained and regularly updated, and has a good community with several plugins and extensions.

Not to say that some problems exists. One I encountered is that sometimes the recording stops, to get around this is best if you don’t do the steps too fast, and move your mouse around other elements if clicking the same object.

CS Student at Universidade Federal de Pernambuco