I've got Playwright test written using TypeScript and I would like to use variables from .env in my test file, how can I do that?
You can put require('dotenv').config() inside your playwright.config.ts and then it will work, see here:
https://www.npmjs.com/package/dotenv
In addition to Max Schmitt's answer, here's 3 other ways to do it:
Add dotenv-cli to your dev dependencies then use it: dotenv playwright test (or npx dotenv playwright test if you're running this command outside of a package.json script).
Use the --require Node.js option and NODE_OPTIONS to set it: NODE_OPTIONS=--require=dotenv/config playwright test.
Use the --require Node.js option and npx to set it: npx --node-options=--require=dotenv/config playwright test (if you're using an old version of npx, you might need to replace --node-options by --node-arg)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With