跳到主要内容

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

添加服务器超时

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,
},
});

添加 baseURL

还建议在配置文件的 use: {} 部分指定 baseURL,以便测试可以使用相对 URL,并且您不必重复指定完整的 URL。

当使用 page.goto()page.route()page.waitForURL()page.waitForRequest()page.waitForResponse() 时,它会使用 URL() 构造函数来构建相应的 URL,从而考虑 baseURL。例如,将 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',
name: 'Frontend',
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
},
{
command: 'npm run backend',
url: 'http://localhost:3333',
name: 'Backend',
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
}
],
use: {
baseURL: 'http://localhost:3000',
},
});