Search code examples
playwrightplaywright-test

how to setup playwright webserver configuration?


I am working with playwright. I'm new to use playwright. In playwright documentation, it is written that 'Playwright comes with a webserver option in the config file which gives you the ability to launch a local dev server before running your tests. This is ideal for when writing your tests during development and when you don't have a staging or production URL to test against.'. I'm confused about that. Does playwright provides an internally built server or do we need to specify a server file in the package.json file and then it will start that server?

this is my playwright.config.js.

const { defineConfig, devices } = require('@playwright/test');
module.exports = defineConfig({
  testDir: './tests',
  fullyParallel: true,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 2 : 0,
  workers: process.env.CI ? 1 : undefined,
  reporter: 'html',
  use: {

    trace: 'on-first-retry',
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },

    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },

    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },
  ],
  webServer: {
    command: 'npm run start',
    url: 'http://127.0.0.1:3000',
    reuseExistingServer: true,
  },
});

My question is when I run 'npx playwright test', will it start a server that is internally built-in playwright, or do we need to mention a start script in the package.json file to start a server?


Solution

  • Playwright can't know what your application does or needs to be started, and it does not have a built-in webserver.

    That's why you have to configure a command in your Playwright config to run and test your project.