Search code examples
playwrightplaywright-test

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?


Solution

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