Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Playwright, how to pass a baseUrl via command line so my spec files dont have a hardcoded url for the app I am testing?

Tags:

playwright

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.

like image 864
Kumar Avatar asked Nov 29 '25 06:11

Kumar


1 Answers

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
like image 94
Yevhen Laichenkov Avatar answered Dec 02 '25 05:12

Yevhen Laichenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!