9

Im looking to use Playwright to test against a web page.

The system im working on has 4 different environments that we need to deploy against, for example the test urls may be

www.test1.com www.test2.com www.test3.com www.test4.com

The first question is how do I target the different Environment? In my playwright config I had a baseUrl but I need to override that.

In addition each environment has different login credentials, how can I create and override these as parameters per environment?

Noxin D Victus
  • 386
  • 1
  • 5
  • 17
OrdinaryKing
  • 411
  • 1
  • 7
  • 16

2 Answers2

14

Since Playwright v1.13.0, there is a baseURL option available. You can utilise that in this way probably

In your config.js file, you can have this

import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  use: {
    baseURL: process.env.URL,
  },
};
export default config;

Now in the package.json file, you can have the environment variables set in the test commands for various env in the scripts , like this

...
"scripts": {
  "start": "node app.js",
  "test1": "URL=www.test1.com mocha --reporter spec",
  "test2": "URL=www.test2.com mocha --reporter spec",
  .
  .
},
...

Similarly you can set the environment variables for the login credentials also and then pass them in the script in the same way the URL is passed.

demouser123
  • 4,108
  • 9
  • 50
  • 82
  • 1
    Is there some other way to do this? For example if you hove some more properties that are environment specific. Is there a way to override or extend the configuration file instead of using env vars? – mismas Mar 25 '22 at 21:08
  • 2
    in this case i pass ENVIRONMENT=test or ENVIRONMENT=production etc , then i use playwright GlobalSetup that loads from a json by the environment. – Andrea Bisello Mar 31 '22 at 09:50
0

Another approach to this is to use a Bash script. I use something like the following to run tests across environments, to ensure that my Playwright tests will work in all environments they're run in -

#!/bin/bash

echo "Running tests against env 1";
ENV_URL=https://www.env1.com SOMESERVICE_ENV_URL=http://www.env1.com/scholarship npx playwright test $1;

echo "Running tests against env 2"

ENV_URL=https://env2.com SOMESERVICE_ENV_URL=http://env2.com:4008 npx playwright test $1;

echo "Running tests against env 3";

ENV_URL=http://localhost:3000 SOMESERVICE_ENV_URL=http://localhost:4008 npx playwright test $1;

And then run with ./myScript.sh myTest.test.ts

(In a Bash script, the first argument passed in is available via $1.)

Andrew
  • 3,825
  • 4
  • 30
  • 44