跳到主要内容

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://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
stdout: 'ignore',
stderr: 'pipe',
},
});
属性描述
testConfig.webServer在测试期间启动一个(或多个)开发 Web 服务器。
command用于启动应用程序本地开发服务器的 Shell 命令。
url您的 HTTP 服务器的 URL,当服务器准备好接受连接时,预计返回 2xx、3xx、400、401、402 或 403 状态代码。
reuseExistingServer如果为 true,则在可用时重用 url 上的现有服务器。 如果该 url 上没有服务器正在运行,它将运行命令来启动新服务器。 如果为 false,如果现有进程正在监听该 url,则会抛出错误。 要查看 stdout,您可以设置 `DEBUG=pw:webserver` 环境变量。
ignoreHTTPSErrors是否在获取 `url` 时忽略 HTTPS 错误。 默认为 `false`。
cwd生成进程的当前工作目录,默认为配置文件目录。
stdout如果为 "pipe",它会将命令的 stdout 管道传输到进程 stdout。 如果为 "ignore",它将忽略命令的 stdout。 默认为 "ignore"
stderr是否将命令的 stderr 管道传输到进程 stderr 或忽略它。 默认为 "pipe"
timeout等待进程启动并可用(以毫秒为单位)的时间。 默认为 60000。
gracefulShutdown如何关闭进程。 如果未指定,则强制 SIGKILL 进程组。 如果设置为 { signal: 'SIGTERM', timeout: 500 },则向进程组发送 SIGTERM 信号,如果进程组在 500 毫秒内未退出,则发送 SIGKILL 信号。 您也可以使用 SIGINT 作为信号。 0 超时表示不会发送 SIGKILL。 Windows 不支持 SIGTERMSIGINT 信号,因此此选项在 Windows 上被忽略。 请注意,关闭 Docker 容器需要 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://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
timeout: 120 * 1000,
},
});

添加 baseURL

还建议在配置的 use: {} 部分中指定 baseURL,以便测试可以使用相对 url,而无需一遍又一遍地指定完整 URL。

当使用 page.goto(), page.route(), page.waitForURL(), page.waitForRequest(), 或 page.waitForResponse() 时,它会通过使用 URL() 构造函数构建相应的 URL 来考虑基本 URL。 例如,通过将 baseURL 设置为 http://127.0.0.1:3000 并在测试中导航到 /login,Playwright 将使用 http://127.0.0.1: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://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
},
use: {
baseURL: 'http://127.0.0.1:3000',
},
});

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

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

test('test', async ({ page }) => {
// This will navigate to http://127.0.0.1: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://127.0.0.1:3000',
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
},
{
command: 'npm run backend',
url: 'http://127.0.0.1:3333',
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
}
],
use: {
baseURL: 'http://127.0.0.1:3000',
},
});