前提
1、Ubuntu上运行selenium的pytest脚本,已经成功运行
https://blog.csdn.net/weixin_43431593/article/details/118635952
2、Ubuntu安装Jenkins,已正常使用
https://blog.csdn.net/weixin_43431593/article/details/118642676
3、Jenkins创建第一个Pipeline,已经会初步使用jenkins
https://editor.csdn.net/md/?articleId=117159892
使用jenkins直接运行脚本
1、只编写pipeline脚本来运行
pipeline {
agent any
stages {
stage('run test') {
steps {
sh '''
#!/bin/bash
cd /home/openailab/yeqinfang/seleniumProjects/testCases
python3 test_main_page.py
'''
}}
stage('finish test') {
steps {
sh '''
#!/bin/bash
echo "good job!"
'''
}}
}
}
如上图,运行成功。只不过,存在一些缺点,如:无法看到实际测试结果,如测试报告、图片信息等等
Pipeline脚本优化一:生成测试报告
pipeline {
agent any
stages {
stage('run test') {
steps {
sh '''
#!/bin/bash
cd /home/openailab/yeqinfang/seleniumProjects/testCases
python3 test_main_page.py
'''
}}
stage('finish test') {
steps {
sh '''
#!/bin/bash
echo "good job!"
'''
}}
stage("get report"){
steps {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/home/openailab/yeqinfang/seleniumProjects/result/', reportFiles: 'report.html', reportName: 'HTML Report', reportTitles: 'test_report'])
}
}
}
}
1、实际生成的报告
2、期望的报告:
3、查明原因
(1) 首先导出源文件
即脚本里的“/home/openailab/yeqinfang/seleniumProjects/result/report.html”,使用浏览器打开:
如图,源文件没有问题
(2)然后导出转换后的文件
即log显示的文件“/home/openailab/jenkins/jobs/showdoc_auto_test/htmlreports/HTML_20Report/report.html”
如图,转化后的文件也没有问题,说明jenkins在渲染报告的时候出现了问题。这就有点不知所措了。
(3)查看页面报错
Refused to apply inline style because it violates the following Content Security Policy directive: “style-src ‘self’”. Either the ‘unsafe-inline’ keyword, a hash (‘sha256-SdJfBf2qWdD/UltlFIaboetttH0zknwzvtXu0qH276M=’), or a nonce (‘nonce-…’) is required to enable inline execution.
(4)解决办法
样式不对,在脚本命令行中执行
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
时区不对,在脚本命令行中执行
System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', 'Asia/Shanghai')
接着再次构建运行脚本,可使测试报告恢复正常
Pipeline脚本优化二:输出图片文件
pipeline {
agent any
stages {
stage('run test') {
steps {
sh '''
#!/bin/bash
cd /home/openailab/yeqinfang/seleniumProjects/testCases
python3 test_main_page.py
'''
}}
stage('finish test') {
steps {
sh '''
#!/bin/bash
echo "good job!"
'''
}}
stage("get report"){
steps {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/home/openailab/yeqinfang/seleniumProjects/result/', reportFiles: 'report.html', reportName: 'HTML Report', reportTitles: 'test_report'])
archiveArtifacts artifacts: '**/*.png', followSymlinks: false
}
}
}
}
1、运行结果报错
Archiving artifacts
‘/*.png’ doesn’t match anything: ‘’ exists but not ‘/*.png’
ERROR: No artifacts found that match the file pattern "/*.png". Configuration error?
2、查明原因
(1)查看用法说明如下,可以了解到比对文件是基于工作空间的。
(2)如图,是实际的工作空间
(3)查询工作空间的文件:
ls /home/openailab/jenkins/workspace/showdoc_auto_test
果然为空,因此会报“doesn’t match anything 。。。exists but not”的错误
(4)脚本中增加一行代码
拷贝文件到工作空间,即如下“finish test”部分的“cp …”操作。
pipeline {
agent any
stages {
stage('run test') {
steps {
sh '''
#!/bin/bash
cd /home/openailab/yeqinfang/seleniumProjects/testCases
python3 test_main_page.py
'''
}}
stage('finish test') {
steps {
sh '''
#!/bin/bash
echo "good job!"
cp -r /home/openailab/yeqinfang/seleniumProjects/result/* /home/openailab/jenkins/workspace/showdoc_auto_test
'''
}}
stage("get report"){
steps {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/home/openailab/yeqinfang/seleniumProjects/result/', reportFiles: 'report.html', reportName: 'HTML Report', reportTitles: 'test_report'])
archiveArtifacts artifacts: '**/*.png', followSymlinks: false
}
}
}
}
如图,成功输出了文件
Pipeline脚本优化三:自动拉取代码
拉取代码的方式可以通过git或svn等,这里采用svn。
svn的搭建方式,请查看我的另一篇文章:https://blog.csdn.net/weixin_43431593/article/details/118878943
1、生成SVN流水线语句
svn 'svn://10.12.1.162/repository'
2、使用svn拉取代码
pipeline {
agent any
stages {
stage('get codes') {
steps {
svn 'svn://10.12.1.162/repository'
}}
stage('run test') {
steps {
sh '''
#!/bin/bash
cd /home/openailab/yeqinfang/seleniumProjects/testCases
python3 test_main_page.py
'''
}}
stage('finish test') {
steps {
sh '''
#!/bin/bash
echo "good job!"
cp -r /home/openailab/yeqinfang/seleniumProjects/result/* /home/openailab/jenkins/workspace/showdoc_auto_test
'''
}}
stage("get report"){
steps {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/home/openailab/yeqinfang/seleniumProjects/result/', reportFiles: 'report.html', reportName: 'HTML Report', reportTitles: 'test_report'])
archiveArtifacts artifacts: '**/*.png', followSymlinks: false
}
}
}
}
如图,已经将代码下载到工作空间,只是按理说需要输入账号密码,可能是服务器上搭建了svn,并且登陆过仓库,所以不需要密码。
3、我们更换jenkins插件,重新生成SVN流水线语句
备注:如果SCM没有Subversion可选,就要去安装该插件。
将“svn ‘svn://10.12.1.162/repository’”改为:
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '6a3e7f97-6036-4c50-b0bf-d08e8637ea82', depthOption: 'infinity', ignoreExternalsOption: true, local: '~/yeqinfang/seleniumProjects', remote: 'svn://10.12.1.162/repository']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
如图报错,原因是路径是相对于工作控件的路径,因此需要修改
继续修改后,成功:
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '6a3e7f97-6036-4c50-b0bf-d08e8637ea82', depthOption: 'infinity', ignoreExternalsOption: true, local: './../../../yeqinfang/seleniumProjects', remote: 'svn://10.12.1.162/repository']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
Pipeline脚本优化四:打造一条龙
pipeline {
agent any
stages {
stage('初始化环境') {
steps {
sh '''
#!/bin/bash
cd /home/openailab/yeqinfang/seleniumProjects/result/
RESULT=$1
if [ "`ls -A $RESULT`" = "" ]; then
echo "$RESULT is allready empty"
else
rm -rf /home/openailab/yeqinfang/seleniumProjects/result/*
fi
cd /home/openailab/jenkins/workspace/showdoc_auto_test/
SHOWDOC_AUTO_TEST=$1
if [ "`ls -A $SHOWDOC_AUTO_TEST`" = "" ]; then
echo "$SHOWDOC_AUTO_TEST is allready empty"
else
rm -rf /home/openailab/jenkins/workspace/showdoc_auto_test/*
fi
'''
}}
stage('拉取最新代码') {
steps {
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '6a3e7f97-6036-4c50-b0bf-d08e8637ea82', depthOption: 'infinity', ignoreExternalsOption: true, local: './../../../yeqinfang/seleniumProjects', remote: 'svn://10.12.1.162/repository']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
}}
stage('运行测试并生成结果') {
steps {
sh '''
#!/bin/bash
cd /home/openailab/yeqinfang/seleniumProjects/testCases
python3 test_main_page.py
'''
}}
stage('拷贝结果到工作空间') {
steps {
sh '''
#!/bin/bash
echo "good job!"
cp -r /home/openailab/yeqinfang/seleniumProjects/result/* /home/openailab/jenkins/workspace/showdoc_auto_test
'''
}}
stage("展示工作空间的结果"){
steps {
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: '/home/openailab/yeqinfang/seleniumProjects/result/', reportFiles: 'report.html', reportName: 'HTML Report', reportTitles: 'test_report'])
archiveArtifacts artifacts: '**/*.png', followSymlinks: false
}
}
}
}
1、运行成功
2、查看图片结果
3、查看报告