Rayrun
← Back to Discord Forum

Support multiple environments in Playwright Test for VSCode extension

deprecated117posted in #help-playwright
Open in Discord
deprecated117
deprecated117

We are trying to support multiple test environments for our playwright tests. We have a good solution for our pipeline where we use the package.json and define specific scripts.

For example: ... "e2e:ci": "test_env=$TEST_ENV npx playwright test --project=chromium", "e2e-dev": "test_env=dev npx playwright test --project=chromium", "e2e-test": "test_env=test npx playwright test --project=chromium", "e2e-stg": "test_env=stg npx playwright test --project=chromium" ...

The problem is when we develop the test using the VSCode Playwright extension and want to debug a script for a specific environment.

Is there a recommended way to support different environments (i.e. BASE_URLs) in VSCode without breaking our process for CI?

Thanks for your help.

This thread is trying to answer question "Is there a recommended way to support different environments (i.e. BASE_URLs) in VSCode without breaking our process for CI?"

2 replies

It might take some work but "simple" solution is to use projects as environments as you are always using chromium. We use it that way and it nicely works even with VS extension and in pipeline we just update --project to value we want

image.png

Not sure if this would work, but maybe create a setup project for each environment, then in the extension select the setup project for the environment you want to setup. This might need some testing and my have issue if 2 setups are selected.

Answer

Sure, you can manage different environments in VSCode without messing up your CI process. The trick is to use environment variables and configuration files.

Create separate configuration files for each environment, like config.dev.json for development and config.prod.json for production. Each file should contain the base URL for its environment. For instance, your development base URL could be http://localhost:3000 and your production base URL could be https://ray.run.

// config.dev.json
{
  "baseUrl": "http://localhost:3000"
}

// config.prod.json
{
  "baseUrl": "https://ray.run"
}

Set up environment variables in your CI system for each environment. You could have a BASE_URL variable that changes based on the CI job or pipeline.

In your code, read these environment variables and load the right configuration file. This way, your code will use the correct base URL whether you're running locally in VSCode or during a CI execution.

Here's a TypeScript example:

import * as fs from 'fs';

const env = process.env.NODE_ENV || 'development';
const configFile = `config.${env}.json`;

if (!fs.existsSync(configFile)) {
  throw new Error(`Configuration file ${configFile} not found.`);
}

const config = JSON.parse(fs.readFileSync(configFile).toString());

console.log(`Using ${env} configuration with baseUrl: ${config.baseUrl}`);

In this example, if you run locally in VSCode without setting NODE_ENV, it defaults to development and loads config.dev.json. During a CI execution, you can set NODE_ENV to production to load config.prod.json.

This approach lets you switch between environments without breaking your CI process. Each environment can have its own base URL or other environment-specific configurations.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].