作者:灬猎丶豹灬_511 | 来源:互联网 | 2023-09-07 14:11
Ant+JMeter+Jenkins接口自动化平台搭建 参考资料::https:www.cnblogs.comdwdwp10096136.html 一、ANT下载安装(鉴于已经安装
Ant+JMeter+Jenkins接口自动化平台搭建
参考资料::
https://www.cnblogs.com/dwdw/p/10096136.html
一、ANT下载安装(鉴于已经安装了JDK1.8,就不讲解如何安装jdk了)
1.下载
下载地址:https://archive.apache.org/dist/ant/binaries/
官网下载地址:https://ant.apache.org/bindownload.cgi
注意版本信息,下载是要根据jdk版本来下载对应ant
1.10.2 .zip
archive 对应jdk8
1.9.10
.zip
archive 对应jdk7
我这里使用的是jdk1.8,故可以使用1.10.x版本,这里下载的是apache-ant-1.10.9
![](https://img.php1.cn/3cd4a/1eebe/cd5/bcafc120671304eb.webp)
2.安装(解压即可,不需安装具体执行文件)
下载完成后解压,然后配置ant环境变量
![](https://img.php1.cn/3cd4a/1eebe/cd5/fb32005f2115b419.webp)
3. 配置系统变量
1.新增ANT_HOME变量:D:\apache-ant-1.10.9-bin\apache-ant-1.10.9
2.path变量添加:%ANT_HOME%\bin
![](https://img.php1.cn/3cd4a/1eebe/cd5/e3aa5425383ba10d.png)
path后添加或新增:%ANT_HOME%\bin,我这里是win10,在path变量里加上变量即可,添加好之后点击确定,确定,确定
![](https://img.php1.cn/3cd4a/1eebe/cd5/4fae50aeee651818.webp)
验证是否安装成功:
打开cmd窗口,输入:ant -v,展示出ant版本号则表示ant安装成功
![](https://img.php1.cn/3cd4a/1eebe/cd5/70be2ca197098d98.webp)
二、配置JMeter+Ant
1.将JMeter所在目录下extras子目录里的ant-JMeter-1.1.1.jar复制到Ant所在目录lib子目录下
![](https://img.php1.cn/3cd4a/1eebe/cd5/3e641eb5f5c8cd90.webp)
2.创建ant执行目录
创建如下文件目录:
jmeter_test(主目录文件)
result_log(测试报告及日志)
html(测试报告生成目录)
jtl(存放jtl文件的目录)
script(存放jmeter的jmx文件)
build.xml(核心配置文件)
创建完成后,目录结构如下图:
![](https://img.php1.cn/3cd4a/1eebe/cd5/4283cd4bbba41b87.png)
![](https://img.php1.cn/3cd4a/1eebe/cd5/b428d8f746fb8d47.webp)
3.在jmeter中完成项目的接口脚本,调试成功,保存到上面的scipt目录。
![](https://img.php1.cn/3cd4a/1e618/c5a/d5d40da532c3a782.png)
将调试好的脚本放入ant执行目录的script目录下,如下图:
![](https://img.php1.cn/3cd4a/1eebe/cd5/5b97d3b808d031e2.webp)
4.创建ant的执行文件build.xml
build.xml文件内容如下:
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
|
"1.0" encoding= "UTF-8" ?>
"jmeter_test" default = "all" basedir= "." >
"time" pattern= "yyyyMMddhhmm" />
"jmeter.home" value= "D:\apache-jmeter-5.2.1" />
"jmeter.result.jtl.dir" value= "D:\jmeter_test\result_log\jtl" />
"jmeter.result.html.dir" value= "D:\jmeter_test\result_log\html" />
"ReportName" value= "接口测试测试报告" />
"jmeter.result.jtlName" value= "${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" />
"jmeter.result.htmlName" value= "${jmeter.result.html.dir}/${ReportName}${time}.html" />
"xslt.classpath" >
"${jmeter.home}/lib" includes= "xalan-2.7.2.jar" />
"${jmeter.home}/lib" includes= "serializer-2.7.2.jar" />
"all" >
"test" />
"report" />
"test" >
"jmeter" classname= "org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
"${jmeter.home}" resultlog= "${jmeter.result.jtlName}" >
"D:\jmeter_test\script" includes= "*.jmx" />
"report" >
"report.datestamp" pattern= "yyyy/MM/dd HH:mm" />
in = "${jmeter.result.jtlName}" out = "${jmeter.result.htmlName}" csharp string">"${jmeter.home}\extras\jmeter-results-detail-report_21.xsl" >
"dateReport" expression= "${report.datestamp}" />
"${jmeter.result.html.dir}" >
"${jmeter.home}/extras" >
"${collapse.png}" />
"${expand.png}" />
|
5.修改JMeter参数
打开JMeter->bin下的jmeter.properties文件,修改配置jmeter.save.saveservice.output_format的值为xml,记得去掉前面的#,然后保存
![](https://img.php1.cn/3cd4a/1eebe/cd5/1113165c4904ecc5.webp)
三、执行测试
打开cmd,定位到jmeter_test目录,执行ant命令,提示BUILD SUCCESSFUL则表示执行成功
![](https://img.php1.cn/3cd4a/1eebe/cd5/ed19db63ee478b98.png)
四、查看执行结果
打开jmeter_test->result_log->html目录,用浏览器打开html文件
![](https://img.php1.cn/3cd4a/1eebe/cd5/99b88427bc9ce0dc.webp)
执行结果如下图:![](https://img.php1.cn/3cd4a/1eebe/cd5/60405fda58cd0acd.webp)
五、 安装Jenkins
1.下载jenkins
下载好jenkins安装包
jenkins下载地:https://jenkins.io/
下载完成后如下图,然后双击安装即可,安装过程中有让输入端口号,需要记录好,后续在浏览器中访问jenkins时需要用到
![](https://img.php1.cn/3cd4a/1eebe/cd5/a1be7872e8d4934f.webp)
jenkins安装包下载地址:
链接:https://pan.baidu.com/s/1unk1wiHLw6AfqQ93zCc_kw
提取码:tlpi
2.安装Jenkins
双击第一步的msi文件,傻瓜式安装即可(安装时需要输入端口,因8080端口容易被占用,故此处改成8081)
3.校验是否安装成功
安装完成后在浏览器输入:localhost:8081,显示如下图,则表示安装成功
![](https://img.php1.cn/3cd4a/1eebe/cd5/617c1173853af4b6.webp)
六、集成JMeter和Ant到Jenkins
1.配置JDK和ANT
![](https://img.php1.cn/3cd4a/1eebe/cd5/780a3060eeed6a4e.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/bff2716168d1ed7b.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/617c1173853af4b6.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/b386c433a16f5497.webp)
点击保存后,再进入界面点击应用
2.配置邮件通知
![](https://img.php1.cn/3cd4a/1eebe/cd5/8be1ccb5166feb93.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/4fae50aeee651818.webp)
3.新建Item
![](https://img.php1.cn/3cd4a/1eebe/cd5/ea91d84a82557da5.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/7494af3c1cda418d.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/7d7ef3f69d479716.webp)
![](https://img.php1.cn/3cd4a/1eebe/cd5/1e3db12dd78db092.webp)
构建触发器
![](https://img.php1.cn/3cd4a/1eebe/cd5/780a3060eeed6a4e.webp)
Build after other projects are built:在其他项目构建完成后才执行构建:指定的项目完成构建后,触发此项目的构建。
Poll SCM :这是CI 系统中常见的选项。当您选择此选项,您可以指定一个定时作业表达式来定义Jenkins每隔多久检查一下您源代码仓库的变化。如果发现变化,就执行一次构建。例如,表达式中填写0,15,30,45 * * * *将使Jenkins每隔15分钟就检查一次您源码仓库的变化。
Build periodically :此选项仅仅通知Jenkins按指定的频率对项目进行构建,而不管SCM是否有变化。如果想在这个Job中运行一些测试用例的话,它就很有帮助。
4.增加构建步骤,ant执行命令
![](https://img.php1.cn/3cd4a/1eebe/cd5/433ea70d6ea577b1.jpeg)
![](https://img.php1.cn/3cd4a/1eebe/cd5/3e641eb5f5c8cd90.webp)
5.配置构建后发送邮件
![](https://img.php1.cn/3cd4a/1eebe/cd5/67cc2e96eddffff8.png)
6.运行Item
回到Jenkins面板,点击项目后的运行按钮,运行![](https://img.php1.cn/3cd4a/1eebe/cd5/1113165c4904ecc5.webp)
遇到的问题:
问题1:提示前言中不允许有内容
BUILD FAILED
D:\jmeter_test\build.xml:26: The following error occurred while executing this line:
D:\jmeter_test\build.xml:42: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 前言中不允许有内容。
![](https://img.php1.cn/3cd4a/1eebe/cd5/99b88427bc9ce0dc.webp)
report:
[xslt] Processing D:\jmeter_test\result_log\jtl\接口测试测试报告202101201144.jtl to D:\jmeter_test\result_log\html\接口测试测试报告202101201144.html
[xslt] Loading stylesheet D:\apache-jmeter-5.2.1\apache-jmeter-5.2.1\extras\jmeter-results-detail-report_21.xsl
[xslt] : Error! 前言中不允许有内容。
[xslt] : Error! com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 前言中不允许有内容。
[xslt] Failed to process D:\jmeter_test\result_log\jtl\接口测试测试报告202101201144.jtl
BUILD FAILED
D:\jmeter_test\build.xml:26: The following error occurred while executing this line:
D:\jmeter_test\build.xml:42: javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 前言中不允许有内容。
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:740)
org.xml.sax.SAXParseException; systemId: file:/D:/jmeter_test/result_log/jtl/%E6%8E%A5%E5%8F%A3%E6%B5%8B%E8%AF%95%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A202101201144.jtl; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
解决办法:修改jmeter.properties文件中jmeter.save.saveservice.output_format的值为xml,并去掉前面的#,保存后重新执行ant命令
![](https://img.php1.cn/3cd4a/1eebe/cd5/8ad8f3bf8da691df.webp)
问题2:在Jenkins中执行项目时提示找不到build.xml
![](https://img.php1.cn/3cd4a/189d8/b64/5b34b53b79a39fdd.jpeg)
解决办法:修改项目->配置->构建->invoke ant,将Tagets修改为all,点开高级,Build File输入build.xml的路径,然后保存再执行
![](https://img.php1.cn/3cd4a/189d8/b64/5b34b53b79a39fdd.jpeg)
![](https://img.php1.cn/3cd4a/1eebe/cd5/dc7ef30f57b727c7.jpeg)
问题3:每天定时执行,如果需要设置成每天早上7点执行一次,则触发器的日程表设置成:0 7 * * *,然后应用保存
![](https://img.php1.cn/3cd4a/1eebe/cd5/72fd2c126203a875.webp)