How to use environment variables from a .env file in Playwright tests?
Hello Kusha,
your inquiry on utilizing the dotenv package to load variables in your project, here is a concise guide to assist you:
Using dotenv package to load variables:
Installation: First, install the dotenv package using npm or yarn:
npm install dotenv --save-dev
yarn add dotenv --dev
Usage: In your test file, import and configure dotenv to load the variables from the .env file:
require(‘dotenv’).config();
const myVariable = process.env.MY_VARIABLE;
Hi Kusha,
I trust this message finds you well. In response to your query about leveraging the dotenv-playwright package to seamlessly incorporate variables into Playwright tests, please find below a concise guide for installation and usage:
Using dotenv-playwright package to load variables directly in Playwright tests:
Installation: Install the dotenv-playwright package as a dev dependency: npm install dotenv-playwright --save-dev yarn add dotenv-playwright --dev Usage: In your test file, import and configure dotenv-playwright to load the variables from the .env file: require(‘dotenv-playwright’).config();
const myVariable = process.env.MY_VARIABLE;
This package is specifically designed for use with Playwright tests and automatically loads the .env file when running tests.