会不会搭建测试平台是一般测试工程师和高级测试工程师分水岭 ----tobecrazy
我们项目有现成的测试平台,使用的是selenium grid+testng+ant+jenkins+VM
但是我平时用的是selenium+testng并未体验过使用ant 和jenkins,今天心血来潮尝试使用一下jenkins.
Jenkins简介:
- Jenkins是一个开源框架
- Jenkins可执行重复的jobs,主要使用在两方面
1.可持续build或test project
2.监控外部执行的jobs
- 自动build发邮件。。。。。
了解更多看https://wiki.jenkins-ci.org/display/JENKINS/Meet+Jenkins
首先安装Jenkins,下载windows平台的版本
由于安装比较简单,这里就不再介绍
安装ant,下载ant zip文件,解压到C:Program Filesapache-ant-1.9.4
在系统变量添加ANT_HOME
之后再CMD运行ant -version
若显式:Apache Ant(TM) version 1.9.4 compiled on April 29 2014
说明ant 配置成功
- 使用ant 编译工程,必须有一个xml文件,默认build.xml
- 每个build文件配置一个default target,执行ant命令会执行这个target
首先了解一下简单使用ant
使用note pad 写一个class,保存为Test java
public class Test { /** * @author tobecrazy * */ public static void main(String[] args) { System.out.println("This ANT HOME "); System.out.println(System.getenv("ANT_HOME")); } }
在cmd尝试编译:javac Test.java
如官方没有报错,恭喜你,可以进行下一步
使用ant所做的操作和命令行输入是一样的,只不过所有的命令转换为xml标签:
这次配置如下:
每个target是一个操作,clean是删除build目录,compile是编译,run是执行java class
<?xml version="1.0"?> <project name="AntTest" default="run" basedir="." > <!-- 默认target是run,运行ant命令直接执行run --> <echo message="The base dir is: ${basedir}" /> <target name="clean"> <delete dir="build"/> </target> <target name="compile" depends="clean"> <!-- 编译java文件依赖clean --> <mkdir dir="build/classes"/> <javac srcdir="src" destdir="build/classes"/> </target> <target name="run" depends="compile"> <!-- 运行class文件,依赖compile --> <echo message="Compile dir is: ${basedir}uildclasses" /> <java classname="Test"> <classpath> <pathelement path="build/classes"/> </classpath> </java> </target> </project>
之后在命令行输入ant(cmd切换到该目录)
结果如下:
D:AntTest>ant
Buildfile: D:AntTestuild.xml
[echo] The base dir is: D:AntTest
clean:
[delete] Deleting directory D:AntTestuild
compile:
[mkdir] Created dir: D:AntTestuildclasses
[javac] D:AntTestuild.xml:11: warning: 'includeantruntime' was not set, d
efaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to D:AntTestuildclasses
run:
[echo] Compile dir is: D:AntTestuildclasses
[java] This ANT HOME
[java] C:Program Filesapache-ant-1.9.4
BUILD SUCCESSFUL
Total time: 1 second
为了配合使用selenium webdriver打开eclipse创建名为Demo的项目
接下来就是使用Jenkins,如果你的Jenkins安装成功,打开http://localhost:8080/会显示Jenkins主页
点击新建->输入项目名->选择“构建一个自由风格的软件项目”->构建->增加构建步骤->选择invoke ANT
接下来配置如下:
此时准备ant build.xml放到工程的根目录
build.xml内容如下,testng 和selenium的jar包需要加载,所以设置
<path id="run.classpath">
由于项目使用的UTF-8编码,在编译时报错所以添加
<javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8">
<?xml version="1.0"?> <project name="Demo" default="run" basedir="."> <echo message="import libs" /> <path id="run.classpath"> <fileset dir="${basedir}"> <include name="lib/testng.jar" /> <include name="lib/sikuli-script.jar" /> </fileset> <fileset dir="${basedir}/lib/selenium"> <include name="selenium-java-2.45.0.jar" /> <include name="libs/*.jar" /> </fileset> </path> <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" /> <target name="clean"> <delete dir="build"/> </target> <target name="compile" depends="clean"> <echo message="mkdir"/> <mkdir dir="build/classes"/> <javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8"> <classpath refid="run.classpath"/> </javac> </target> <path id="runpath"> <path refid="run.classpath"/> <pathelement location="build/classes"/> </path> <target name="run" depends="compile"> <testng classpathref="runpath" outputDir="test-output"> <xmlfileset dir="${basedir}" includes="testng.xml"/> <jvmarg value="-ea" /> </testng> </target> </project>
更多ant和testng classname="org.testng.TestNGAntTask"是固定格式
参考:http://testng.org/doc/index.html
接下来在项目添加几个case,项目目录如下
准备testng的配置文件如下,放在工程的根目录下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" parallel="none"> <test name="Test"> <groups> <run> <include name="Case1" /> <include name="MapTest" /> <include name="JDaddress" /> <include name="useCookies" /> </run> </groups> <classes> <class name="com.dbyl.tests.Case1"/> <class name="com.dbyl.tests.JDaddress"/> <class name="com.dbyl.tests.UseCookieLogin"/> <class name="com.dbyl.tests.MapTest"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
接下来就是见证奇迹的时候:
在Jenkins里选择构建
Building in workspace C:Program Files (x86)JenkinsworkspaceDemo [workspace] $ cmd.exe /C '""C:Program Filesapache-ant-1.9.4inant.bat" -file Demo run && exit %%ERRORLEVEL%%"' Buildfile: C:UsersYoungworkspaceDemouild.xml [echo] import libs clean: [delete] Deleting directory C:UsersYoungworkspaceDemouild compile: [echo] mkdir [mkdir] Created dir: C:UsersYoungworkspaceDemouildclasses [javac] C:UsersYoungworkspaceDemouild.xml:21: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 6 source files to C:UsersYoungworkspaceDemouildclasses run: [testng] [TestNG] Running: [testng] C:UsersYoungworkspaceDemo estng.xml [testng] [testng] Fast test [testng] Slow test [testng] Starting ChromeDriver (v2.9.248315) on port 43260 [testng] Starting ChromeDriver (v2.9.248315) on port 20660 [testng] Starting ChromeDriver (v2.9.248315) on port 13803 [testng] cookie write to file
。。。。