跳转到主要内容

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: 'https://: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 信号,如果它在 500ms 内没有退出,则会收到 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。
url你的 http 服务器的 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: 'https://: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 设置为 https://:3000 并在测试中导航到 /login,Playwright 将使用 https://: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: 'https://:3000',
reuseExistingServer: !process.env.CI,
},
use: {
baseURL: 'https://:3000',
},
});

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

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

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