OrdinaryKing
OrdinaryKing

Reputation: 461

Playwright - Test against different environments and different variables

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?

Upvotes: 10

Views: 24217

Answers (2)

Andrew
Andrew

Reputation: 5284

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.)

Upvotes: 0

demouser123
demouser123

Reputation: 4264

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.

Upvotes: 15

Related Questions