Using Playwright code gen

Arthur Pires
2 min readOct 28, 2020

--

https://playwright.dev/

What is the playwright?

Playwright is a Node.js library to automate Chromium, Firefox, and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable, and fast.

Requirements

First, you need to be sure that you have the node and the npm already installed on your machine. If you do not have it, you could take a look at the node page, it should install both.

How do I install it?

It’s simple or should be, you should only run on your terminal the following command:

$ npm install -D -g playwright-cli

It should install it globally and with all its dependencies.

Try to run it!

$ npx playwright-cli codegen wikipedia.org

It fails 😕

The installation could be a success, but sometimes it fails like that:

(node:28837) UnhandledPromiseRejectionWarning: browserType.launch: Failed to launch chromium because executable doesn’t exist at /Users/arthur.pires/Library/Caches/ms-playwright/chromium-815036/chrome-mac/Chromium.app/Contents/MacOS/Chromium

Try re-installing playwright with “npm install playwright”

At the moment I write this article, the script seems to be broken. If you check, it fails to download the Chromium to the right path.

The solution? Put it by force

To solve it You could download the Chromium and put it in the right path, in this case, the path was :

/Users/arthur.pires/Library/Caches/ms-playwright/chromium-815036/chrome-mac/Chromium.app

but will change from machine to machine.

Now it goes!

I have ran

$ npx playwright-cli codegen wikipedia.org

And made some navigations on Wikipedia, and looking at the terminal I could see a test case to programmatic reproduce my steps. I copied it and put in a file called test.js and ran

$ node test.js

👏🏾👏🏾👏🏾 It beautifully opens the browser and reproduced the steps.

Is it good?

It’s a promising tool, is easy to interact with, has good potential and Microsoft probably will maintain it for some years, since it is too new. I will consider to use it in my next web project.

My only concern is to be used to flood projects with integrations tests and make it difficult to maintain later. But this is a conversation for another topic.

Thanks to read this article! Hope this has helped you in some way.

References

--

--

No responses yet