Jenkinsfile
4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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'
// )