14

In Protractor, I am currently passing a flag in the command line that indicates what my URL is for the app I am testing. And now we are switching to Playwright, I want to do something similar. Since the same tests will be used to test the app in different environments (dev, test, CI) I need a way to pass different URLs and ideally it will be nice if I can control that via command line.

Kumar
  • 295
  • 1
  • 5
  • 10

2 Answers2

14

UPDATE:

The baseURL option has been added in Playwright v1.13.0.

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

const config: PlaywrightTestConfig = {
  use: {
    baseURL: 'http://localhost:3000',
  },
};
export default config;

Currently, in Playwright Test v1.12.0 there is no baseUrl property as we have in Protractor. But you can accomplish it with a system variable.

For instance:

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

const BASE_URL = process.env.URL;

test('verify title of the page', async ({ page }) => {
  await page.goto(BASE_URL);
});

and then run it on dev env:

URL=https://playwright.dev npx playwright test

or prod:

URL=https://playwright.prod npx playwright test
Yevhen Laichenkov
  • 7,746
  • 2
  • 27
  • 33
1

I added an environment variable to the playwright.config.js and it seems to work here.

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

const config = {
  use: {
      baseUrl: process.env.MY_CUSTOM_BASE_URL
    }
  }

then run something like the answer above.

MY_CUSTOM_BASE_URL=https://example.com npx playwright test

or

export MY_CUSTOM_BASE_URL=https://example.com
npx playwright test