microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.04k stars 3.68k forks source link

[BUG] using a different .env file with the Playwright for VSCode extension #28734

Closed Antoine-Regembal closed 11 months ago

Antoine-Regembal commented 11 months ago

Context:

Code Snippet

package.json scripts

{
   "test:playwright": "dotenv -e .env.test -- playwright test"
}

Describe the bug

Hello people of Playwright,

In my application I need to use a different .env file for testing my application. I use the dotenv package to define a .env.test (as you can see in the code snippet) file that my application should use to launch for testing. When I launch tests with CLI it works perfectly, but the Playwright for VSCode extension doesn't follow this script obviously and I was not able to find a way to ask Playwright for VSCode extension to launch tests with the .env.test file instead of the .env for the environment variables.

Is there any way to achieve that ? That would be great for me to be able to launch tests with the extension

Cheers 🥖

gofr commented 11 months ago

Does your dotenv have to be specified in the package.json? I load my dotenv at the top of my playwright.config.ts. Would that work for you? See https://stackoverflow.com/questions/70861292/playwright-tests-using-variable-from-env-file.

Antoine-Regembal commented 11 months ago

Hi @gofr

I updated my playwright.config.ts file like so:

import dotenv from "dotenv";
import path from "path";

dotenv.config({ path: path.join(process.cwd(), ".env.test") });

....

It works well, I didn't think about it

Thank you 🥂

Mwalek commented 2 months ago

This was a helpful thread, thanks!

Rendez commented 1 week ago

Hi @gofr

I updated my playwright.config.ts file like so:

import dotenv from "dotenv"; import path from "path";

dotenv.config({ path: path.join(process.cwd(), ".env.test") });

.... It works well, I didn't think about it

Thank you 🥂

You can also now natively achieve the same:

import process from 'node:process';
try {
    process.loadEnvFile(`${import.meta.dirname}/.env`);
} catch {
    console.log('No .env file found');
}