Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To conduct playwright tests by utilizing variables from the .env file, you can follow these steps:

  1. Install dotenv package by running the following command in your terminal:
npm install dotenv
  1. Create a .env file in the root folder of your project and define your variables in this format:
VARIABLE_NAME=value

For example:

USERNAME=myusername
PASSWORD=mypassword
  1. In your test file, require the dotenv package at the top and load the variables from the .env file by calling the config method:
require('dotenv').config();
  1. Use the variables in your playwright test by referencing process.env followed by the name of the variable enclosed in brackets:
const username = process.env.USERNAME;
const password = process.env.PASSWORD;

await page.type('#username', username);
await page.type('#password', password);

By utilizing variables from the .env file, you can keep sensitive information like usernames and passwords separate from your code and easily manage them in a central location.