Jenkinsfile 4.89 KB
// Jenkinsfile - UI自动化测试流水线配置
// 使用方法:在 Jenkins 中创建 Pipeline 任务,选择 "Pipeline script from SCM" 或直接粘贴此文件内容

pipeline {
    agent any
    
    tools {
        nodejs 'NodeJS-18'    // Jenkins 中配置的 Node.js 工具名称
        allure 'Allure'        // Jenkins 中配置的 Allure 工具名称
    }
    
    environment {
        // 环境变量配置
        NODE_ENV = 'test'
        // 测试环境 URL(可在 Jenkins 中覆盖)
        BASE_URL = credentials('test-base-url') ?: 'https://erp-pad.test.gszdtop.com'
    }
    
    parameters {
        choice(
            name: 'TEST_SUITE',
            choices: ['all', 'customer', 'product', 'sale', 'consignment'],
            description: '选择要执行的测试套件'
        )
        choice(
            name: 'BROWSER',
            choices: ['chromium', 'firefox', 'webkit'],
            description: '选择浏览器'
        )
        booleanParam(
            name: 'HEADLESS',
            defaultValue: true,
            description: '是否使用无头模式'
        )
        string(
            name: 'GREP',
            defaultValue: '',
            description: '测试用例过滤(留空执行全部)'
        )
    }
    
    stages {
        stage('Checkout') {
            steps {
                echo '📥 拉取代码...'
                git branch: 'main', url: 'https://your-git-repo-url/xfbhAutoTest.git'
            }
        }
        
        stage('Install Dependencies') {
            steps {
                echo '📦 安装依赖...'
                sh 'npm install'
            }
        }
        
        stage('Install Playwright Browsers') {
            steps {
                echo '🌐 安装浏览器...'
                sh "npx playwright install ${params.BROWSER}"
                sh "npx playwright install-deps ${params.BROWSER}"
            }
        }
        
        stage('Run Tests') {
            steps {
                echo '🧪 执行测试...'
                script {
                    def testCommand = "npx playwright test"
                    
                    // 选择测试套件
                    if (params.TEST_SUITE != 'all') {
                        testCommand += " tests/${params.TEST_SUITE}.spec.ts"
                    }
                    
                    // 选择浏览器
                    testCommand += " --project=${params.BROWSER}"
                    
                    // 无头模式
                    if (!params.HEADLESS) {
                        testCommand += " --headed"
                    }
                    
                    // 过滤测试用例
                    if (params.GREP) {
                        testCommand += " -g \"${params.GREP}\""
                    }
                    
                    sh testCommand
                }
            }
        }
    }
    
    post {
        always {
            echo '📊 生成测试报告...'
            
            // 发布 Allure 报告
            allure includeProperties: false, 
                   jdk: '', 
                   results: [[path: 'allure-results']]
            
            // 归档 Playwright HTML 报告
            publishHTML(target: [
                allowMissing: false,
                alwaysLinkToLastBuild: false,
                keepAll: true,
                reportDir: 'playwright-report',
                reportFiles: 'index.html',
                reportName: 'Playwright Report'
            ])
            
            // 归档测试结果
            archiveArtifacts artifacts: 'playwright-report/**, test-results/**', 
                            allowEmptyArchive: true
        }
        
        success {
            echo '✅ 测试执行成功!'
        }
        
        failure {
            echo '❌ 测试执行失败,请检查测试报告!'
        }
        
        unstable {
            echo '⚠️ 测试执行完成,但存在失败的用例!'
        }
    }
}

// ============ 定时执行配置 ============
// 如需定时执行,在 pipeline 块内添加以下 triggers 配置:
//
// triggers {
//     // 每天 6:00 执行(北京时间)
//     cron('0 22 * * *')  // UTC 22:00 = 北京时间 06:00
//     
//     // 或者使用轮询 SCM(代码变更时触发)
//     pollSCM('H/5 * * * *')  // 每5分钟检查一次代码变更
// }

// ============ 邮件通知配置 ============
// 如需邮件通知,在 post 块中添加:
//
// emailext(
//     subject: "UI自动化测试报告 - ${env.JOB_NAME} #${env.BUILD_NUMBER}",
//     body: """
//         <h2>测试执行完成</h2>
//         <p>构建状态: ${currentBuild.currentResult}</p>
//         <p>构建链接: ${env.BUILD_URL}</p>
//         <p>Allure报告: ${env.BUILD_URL}allure/</p>
//         <p>HTML报告: ${env.BUILD_URL}Playwright_Report/</p>
//     """,
//     to: 'team@example.com',
//     from: 'jenkins@example.com'
// )