Playwright 测试报 “Timeout of 30000ms exceeded“ 如何定位并调整超时配置?
Playwright 测试报 Timeout of 30000ms exceeded 如何定位并调整超时配置【免费下载链接】playwrightPlaywright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.项目地址: https://gitcode.com/GitHub_Trending/pl/playwright在 Playwright TestJavaScript 测试库中跑测试时如果某个用例失败并输出如下报错说明该测试超出了默认的 30 秒测试超时example.spec.ts:3:1 › basic test Timeout of 30000ms exceeded.这篇文章基于仓库内的 Timeouts 文档、playwright 测试配置文档 和 Debug 文档说明这个报错对应哪一层超时、如何确认失败点以及在配置文件、单个测试、单个断言三个层级分别调整超时的具体写法。适用于使用playwright.config.ts组织测试的 Playwright Test 项目。先判断报错来自哪一层超时Playwright Test 有多层互相独立的超时Timeout of 30000ms exceeded 对应的是测试超时Test timeout默认 30 000 ms。各层超时的默认值和配置入口如下来自 Timeouts 文档超时类型默认值说明配置位置Test timeout30_000 ms每个测试的超时包含测试函数、fixture 设置和beforeEach钩子的耗时配置{ timeout: 60_000 }测试内test.setTimeout(120_000)Expect timeout5_000 ms每个自动重试断言如toHaveText的超时与测试超时相互独立配置{ expect: { timeout: 10_000 } }断言参数{ timeout: 10_000 }Action timeout无超时每个动作如locator.click的超时配置{ use: { actionTimeout: 10_000 } }测试内locator.click({ timeout: 10_000 })Navigation timeout无超时每个导航动作的超时配置{ use: { navigationTimeout: 30_000 } }测试内page.goto(/, { timeout: 30_000 })Global timeout无超时整个测试运行的超时防止全部出错时过度占用资源配置{ globalTimeout: 3_600_000 }区分要点报错文案是Timeout of 30000ms exceeded.时是测试超时被触发而不是断言或动作超时。断言超时的报错长这样文档示例其中Call log会标出是哪个断言、等待了哪个 locatorError: expect(received).toHaveText(expected) Expected string: my text Received string: Call log: - expect.toHaveText with timeout 5000ms - waiting for locator(button)两种报错对应不同的处理入口前者调测试/配置层超时后者调 expect 层超时。另外两个容易混淆的边界测试函数结束后fixture teardown 和afterEach钩子共享一个独立的、与测试超时等值的超时。beforeAll/afterAll钩子有独立超时默认与测试超时相等30_000 ms不与任何测试共享时间。定位卡住的步骤用 --debug 跑一次在调整数值之前先用 Debug 模式确认测试到底慢在哪一步。Debug 文档 说明--debug会打开 Playwright Inspector逐用例运行并显示 actionability 日志同时会配置两个调试默认值浏览器以 headed 模式启动Default timeout is set to 0 ( no timeout)即调试时测试不再受 30 秒超时限制。这意味着--debug运行本身不会复现超时适合用来观察测试实际执行到哪里、哪一步耗时异常。命令# 调试单个测试文件的全部测试 npx playwright test --debug # 调试指定文件指定行的单个测试 npx playwright test example.spec.ts:10 --debug # 只在配置的某个 project 上调试 npx playwright test example.spec.ts:10 --projectchromium --debugexample.spec.ts:10中的文件与行号替换为你要调试的测试文件路径和测试所在行号。调整配置根据定位结果在以下四个层级中选择最小改动范围来调整。1. 全局在 playwright.config.ts 中提高测试超时适用于整个项目普遍偏慢、或希望统一放宽的情况import { defineConfig } from playwright/test; export default defineConfig({ timeout: 120_000, });对应 API 为TestConfig.timeout见 TestConfig 参考单位为毫秒。测试配置文档 中默认配置示例即为timeout: 30000。2. 单个测试test.setTimeout / test.slow只给个别慢用例放宽时间import { test, expect } from playwright/test; test(slow test, async ({ page }) { test.slow(); // Easy way to triple the default timeout // ... }); test(very slow test, async ({ page }) { test.setTimeout(120_000); // ... });test.slow()把该测试的默认超时变为 3 倍API 参考 说明它不能在beforeAll/afterAll钩子中使用。test.setTimeout(ms)直接指定毫秒数0表示不限制Test.setTimeout 的说明。也可以作用于整个test.describe组test.describe(group, () { // Applies to all tests in this group. test.describe.configure({ timeout: 60000 }); test(test one, async () { /* ... */ }); });3. 慢在 beforeEach从钩子里动态加时如果测试本身不慢、是beforeEach里的公共准备耗时过长可以在钩子里基于当前超时追加beforeEach的耗时本来就计入测试超时import { test, expect } from playwright/test; test.beforeEach(async ({ page }, testInfo) { // Extend timeout for all tests running this hook by 30 seconds. testInfo.setTimeout(testInfo.timeout 30_000); });当前测试的超时值可通过testInfo.timeout读取TestInfo.setTimeout 用于在钩子中修改它。beforeAll/afterAll钩子则在自己的钩子体内调用test.setTimeout(60000)单独调整钩子超时这不影响测试超时。4. 慢在断言调 expect 超时如果Call log显示是某个自动重试断言等不到条件调 expect 层而不是测试超时import { defineConfig } from playwright/test; export default defineConfig({ expect: { timeout: 10_000, }, });也可以只给单条断言指定import { test, expect } from playwright/test; test(example, async ({ page }) { await expect(locator).toHaveText(hello, { timeout: 10_000 }); });其中locator替换为你断言目标的 Locator。可选分支以下两类超时默认不限制且 Timeouts 文档 明确标注它们属于Advanced: low level timeouts——测试运行器已预先配置you should not need to change these如果你的测试不稳定文档建议先在其他方向找原因而不是调低层超时动作与导航超时在use下设置import { defineConfig } from playwright/test; export default defineConfig({ use: { actionTimeout: 10 * 1000, navigationTimeout: 30 * 1000, }, });或在单个动作上覆盖import { test, expect } from playwright/test; test(basic test, async ({ page }) { await page.goto(https://playwright.dev, { timeout: 30000 }); await page.getByText(Get Started).click({ timeout: 10000 }); });全局运行超时没有默认值可在配置中设置一个合理的上限如 1 小时防止everything went wrong时测试运行无限占用资源import { defineConfig } from playwright/test; export default defineConfig({ globalTimeout: 3_600_000, });触发时输出如下文档示例Running 1000 tests using 10 workers 514 skipped 486 passed Timed out waiting 3600s for the entire test run慢在 worker 级 fixturefixture 默认与测试共享超时但可以给它单独超时从而保持整体测试超时的同时给慢 fixture 更多时间import { test as base, expect } from playwright/test; const test base.extend{ slowFixture: string }({ slowFixture: [async ({}, use) { // ... perform a slow operation ... await use(hello); }, { timeout: 60_000 }] }); test(example test, async ({ slowFixture }) { // ... });验证调整是否生效改完配置或代码后重新运行对应测试文件按以下文档给出的现象判断npx playwright test example.spec.ts之前的Timeout of 30000ms exceeded.不再出现且用例不再因测试超时失败——测试超时调整生效。如果仍超时报错中的毫秒数会变成新的配置值例如配置了timeout: 120_000后报错显示 120000ms据此确认改的是同一层超时。如果是断言问题Call log中的expect.toHaveText with timeout 5000ms一行会显示 expect 的新值说明 expect 超时已生效此时应继续根据waiting for指向的 locator 排查页面条件为何不满足而不是继续加大超时。调整超时应保持在能覆盖实际耗时的最小范围优先给单个测试或单个断言调test.setTimeout、断言参数其次给 describe 组或配置项调避免无差别放大全局timeout。相关文档Timeouts、playwright 测试配置、Debugging、Fixtures。【免费下载链接】playwrightPlaywright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.项目地址: https://gitcode.com/GitHub_Trending/pl/playwright创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考