跳至主要内容

Web服务器

简介

Playwright 在配置文件中提供了一个 webserver 选项,使您能够在运行测试前启动本地开发服务器。这对于开发期间编写测试以及没有暂存或生产环境URL进行测试时非常理想。

配置Web服务器

在您的Playwright配置中使用webserver属性,以便在测试期间启动开发Web服务器。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Run your local dev server before starting the tests
webServer: {
command: 'npm run start',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
stdout: 'ignore',
stderr: 'pipe',
},
});
PropertyDescription
testConfig.webServerLaunch a development web server (or multiple) during the tests.
commandShell command to start the local dev server of your app.
urlURL of your http server that is expected to return a 2xx, 3xx, 400, 401, 402, or 403 status code when the server is ready to accept connections.
reuseExistingServerIf true, it will re-use an existing server on the url when available. If no server is running on that url, it will run the command to start a new server. If false, it will throw if an existing process is listening on the url. To see the stdout, you can set the DEBUG=pw:webserver environment variable.
ignoreHTTPSErrorsWhether to ignore HTTPS errors when fetching the url. Defaults to false.
cwdCurrent working directory of the spawned process, defaults to the directory of the configuration file.
stdoutIf "pipe", it will pipe the stdout of the command to the process stdout. If "ignore", it will ignore the stdout of the command. Default to "ignore".
stderrWhether to pipe the stderr of the command to the process stderr or ignore it. Defaults to "pipe".
timeoutHow long to wait for the process to start up and be available in milliseconds. Defaults to 60000.
gracefulShutdownHow to shut down the process. If unspecified, the process group is forcefully SIGKILLed. If set to { signal: 'SIGTERM', timeout: 500 }, the process group is sent a SIGTERM signal, followed by SIGKILL if it doesn't exit within 500ms. You can also use SIGINT as the signal instead. A 0 timeout means no SIGKILL will be sent. Windows doesn't support SIGTERM and SIGINT signals, so this option is ignored on Windows. Note that shutting down a Docker container requires SIGTERM.

添加服务器超时设置

Web服务器有时可能需要更长的时间来启动。在这种情况下,您可以增加超时时间以等待服务器启动。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Rest of your config...

// Run your local dev server before starting the tests
webServer: {
command: 'npm run start',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
timeout: 120 * 1000,
},
});

添加基础URL

还建议在配置文件的use: {}部分指定baseURL,这样测试就可以使用相对URL,而不必反复指定完整的URL。

当使用page.goto()page.route()page.waitForURL()page.waitForRequest()page.waitForResponse()时,它会通过URL()构造函数考虑基础URL来构建相应的URL。例如,通过将baseURL设置为http://localhost:3000并在测试中导航到/login,Playwright将使用http://localhost:3000/login运行测试。

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
// Rest of your config...

// Run your local dev server before starting the tests
webServer: {
command: 'npm run start',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
use: {
baseURL: 'http://localhost:3000',
},
});

现在您可以在导航页面时使用相对路径:

test.spec.ts
import { test } from '@playwright/test';

test('test', async ({ page }) => {
// This will navigate to http://localhost:3000/login
await page.goto('./login');
});

多个Web服务器

通过提供一个webServer配置数组,可以同时启动多个Web服务器(或后台进程)。更多信息请参阅testConfig.webServer

playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
webServer: [
{
command: 'npm run start',
url: 'http://localhost:3000',
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
},
{
command: 'npm run backend',
url: 'http://localhost:3333',
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
}
],
use: {
baseURL: 'http://localhost:3000',
},
});