puppeteer-to-playwright
puppeteer-to-playwright
automatically converts Javascript Puppeteer scripts to Playwright, aiming to reduce the amount of manual work involved in such a migration (ideally reducing it to zero). It is heavily based on jscodeshift.
puppeteer-to-playwright requires Node.js 14 or newer.
Features
puppeteer-to-playwright
will convert your existing Puppeteer script to Playwright, including:
- Converting import statements
- Converting basic methods to match new API (e.g.
setViewport
tosetViewportSize
and similar) - Creating browser context explicitly
- Eliminating explicit waiting (unless Strict mode is enabled)
- Converting cookies-related commands
Currently unsupported
The following features are not yet supported, meaning that the corresponding instructions won't be converted to Playwright if they are used in your Puppeteer script:
- File upload
- File download
- Request/response interception
- Multiple contexts / tabs
Still, these might very well be implemented in the near future. If you would like to help, see our how to contribute section.
Getting Started
You can use puppeteer-to-playwright
on a script file or multiple scripts at a time.
🚨 It will overwrite the script file when run, so we recommend doing a dry-run first.
Dry run
A dry run will run the conversion without actually writing to the file(s) you point it at.
$ npm run convert -- -d my-puppeteer-script.js
You can add -p
to print out the resulting script:
$ npm run convert -- -d -p my-puppeteer-script.js
You can pass additional jscodeshift parameters as described in the project's repository.
Convert script
You can convert scripts one by one...
$ npm run convert my-puppeteer-script.js
...or you can convert entire folders recursively.
$ npm run convert my-puppeteer-folder
puppeteer-to-playwright will ignore files that have any extension other than
.js
, as well as those that do not import/require Puppeteer.
Strict mode
When converting files, puppeteer-to-playwright will, by default, get rid of likely unnecessary waits that Playwright should handle automatically. If you know that the waits in your Puppeteer script will remain necessary even with Playwright, you can set puppeteer-to-playwright to Strict mode by running export STRICT=true
. Alternatively, you can set the flag directly when converting a file or folder, e.g.:
STRICT=true npm run convert my-puppeteer-folder
How to contribute
If you would like to improve this codemod, you are very welcome to send a PR. Make sure it contains a test for the specific feature you are trying to add. Testing is currently set up as follows:
- In
test/base
you have the original Puppeteer script - In
test/output
you have the original Puppeteer script, which will be automatically converted when tests are run then immediately restored to the original - In
test/expected
you have the expected Playwright script that your result will need to be equal to
You can run npm run test
locally, and tests will run automatically for each new PR.
Links
- jscodeshift - without which this project wouldn't be possible.
- ASTExplorer - useful debugging tool in case you would like to modify the transform file of this project to tweak its output.
License
Code, test, and deploy synthetic monitoring at scale
From Checkly with