在Codefresh等CI/CD工具中运行Playwright测试超时问题求助
问题描述
我在Codefresh中运行Playwright测试,使用的配置如下:
test: title: 'Run E2E Tests' type: 'freestyle' image: 'mcr.microsoft.com/playwright:v1.40.0-jammy' working_directory: '${{clone}}' stage: test fail_fast: false commands: - npx playwright install --with-deps - xvfb-run npx playwright test
测试中启动浏览器的代码为:
const browser = await chromium.launchPersistentContext('', { args: ['--disable-web-security', '--allow-insecure-localhost', '--no-sandbox', '--disable-setuid-sandbox'], bypassCSP: true, headless: false, });
运行时出现超时错误:
Executing command: xvfb-run npx playwright test Running 2 tests using 1 worker ° Timed out waiting 1000s for the test suite to run Timed out waiting 1000s for the teardown for test suite to run 2 skipped Timed out waiting 1000s for the entire test run Reading environment variable exporting file contents. Reading environment variable exporting file contents.
求解决思路。
解决思路
- 关闭可视化运行模式:CI环境下不要用
headless: false,这会强制浏览器启动图形界面,即便用xvfb模拟,也容易出现性能瓶颈或兼容性问题。把代码里的headless改成true,甚至可以直接去掉这个参数(Playwright默认就是headless模式)。 - 精简Codefresh命令:官方提供的Playwright镜像
mcr.microsoft.com/playwright:v1.40.0-jammy已经预装了所有浏览器依赖,不需要再执行npx playwright install --with-deps,直接删掉这一步。同时Playwright的headless模式不需要xvfb支持,把命令改成npx playwright test即可。 - 排查超时配置:
- 检查测试用例中的超时设置,比如
waitForSelector、waitForResponse等操作是否设置了合理的超时时间,避免无限等待。 - 在Codefresh的stage配置中,检查是否有全局超时限制,必要时调高超时阈值(当前是1000秒,若测试确实需要更长时间可调整)。
- 检查测试用例中的超时设置,比如
- 简化浏览器启动参数:去掉不必要的浏览器启动参数,比如
--disable-web-security、--allow-insecure-localhost、bypassCSP,先保留--no-sandbox和--disable-setuid-sandbox这两个CI环境必需的参数,排查是否是参数冲突导致浏览器启动卡住。 - 增加日志排查:在测试命令中添加
--verbose参数,或者设置环境变量DEBUG=playwright*,比如修改命令为DEBUG=playwright* npx playwright test --verbose,通过详细日志定位到底是浏览器启动失败,还是测试用例执行过程中卡在了某个步骤。
内容的提问来源于stack exchange,提问作者user22873938
相关产品推荐
相关产品推荐

