You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins中运行Playwright并行测试频繁出现卡顿问题

问题:Jenkins并行执行Playwright测试卡顿

场景描述

我尝试通过Jenkins Pipeline运行Playwright测试,期望并行执行login.spec.ts与coder.spec.ts测试,但大多数情况下执行测试命令后会出现卡顿,仅偶尔能正常运行,Freestyle项目也存在同样问题。

我的Pipeline脚本

pipeline {
agent any

// 定义自定义工作区目录
environment {
    CUSTOM_WORKSPACE = 'C:\\Automation\\eclat-qa\\Matrix'  // 根据需要更新路径
}

options {
    // 跳过默认代码检出(如果不需要检出仓库可启用)
    skipDefaultCheckout()
}

stages {
    stage('环境初始化') {
        steps {
            // 在自定义工作区执行初始化
            dir("${env.CUSTOM_WORKSPACE}") {
                script {
                    // 安装npm依赖
                    bat 'npm install'
                    
                    // 安装Playwright浏览器及依赖
                    bat 'npx playwright install --with-deps'
                }
            }
        }
    }
    stage('并行执行测试') {
        parallel {
            stage('运行login.spec.ts') {
                steps {
                    dir("${env.CUSTOM_WORKSPACE}") {
                        script {
                            bat 'npx playwright test src/tests/login.spec.ts'
                        }
                    }
                }
            }
            stage('运行coder.spec.ts') {
                steps {
                    dir("${env.CUSTOM_WORKSPACE}") {
                        script {
                            bat 'npx playwright test src/tests/coder.spec.ts'
                        }
                    }
                }
            }
        }
    }
    stage('生成Allure报告') {
        steps {
            dir("${env.CUSTOM_WORKSPACE}") {
                script {
                    // 基于测试结果生成Allure报告
                    bat 'npx allure generate --clean allure-results'
                }
            }
        }
    }
}

post {
    always {
        script {
            dir("${env.CUSTOM_WORKSPACE}") {
                // 通过Allure Jenkins插件发布报告
                allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
            }
        }
    }
}

控制台输出

C:\Automation\eclat-qa\Matrix>npx playwright test src/tests/login.spec.ts 

C:\Automation\eclat-qa\Matrix>npx playwright test src/tests/coder.spec.ts 

Running 8 tests using 1 worker

Running 4 tests using 1 worker

解决方案建议

1. 隔离Allure结果目录

两个并行测试会同时写入allure-results目录,引发文件读写冲突,导致卡顿。需为每个任务指定独立的结果目录:

  • 修改测试命令:
    npx playwright test src/tests/login.spec.ts --reporter=allure-playwright --output=allure-results-login
    
    npx playwright test src/tests/coder.spec.ts --reporter=allure-playwright --output=allure-results-coder
    
  • 更新报告生成命令:
    npx allure generate --clean allure-results-login allure-results-coder
    
  • 调整post步骤的Allure配置:
    allure includeProperties: false, jdk: '', results: [[path: 'allure-results-login'], [path: 'allure-results-coder']]
    

2. 限制Playwright Worker数量

每个Playwright任务默认会启动多Worker,叠加Jenkins的并行任务易造成资源耗尽。可强制指定单Worker:

npx playwright test src/tests/login.spec.ts --workers=1

3. 检查Jenkins节点资源

确认Jenkins代理节点的CPU、内存是否足够支撑两个并行的Playwright测试(每个测试会启动浏览器实例,资源消耗较大),资源不足会直接导致任务卡顿。

4. 强制使用无头模式

显式指定无头模式运行测试,避免图形界面渲染占用额外资源:

npx playwright test src/tests/login.spec.ts --headed=false

5. 清理缓存文件

偶尔卡顿可能是缓存文件损坏,在环境初始化阶段添加清理步骤:

bat 'npm cache clean --force'
bat 'npx playwright install --force'

内容的提问来源于stack exchange,提问作者Success Shrestha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:32:33