1. 从写code开始吧,安装eclipse,官网下载软件,https://www.eclipse.org/downloads/,记得先装Java,建议装JDK1.7以上的。
2.接下来需要写selenium的code,testng的code,从selenium,selenium-server-standalone-2.41.0.jar,testng,testng-6.8.jar官网下载相应jar文件,放入eclipse的目录中。
4.接下来你可以写selenium的代码了,写好代码后,运用testng的框架,testNG如何使用,这里不详细说,参考testNG的官网,在code中加上testNG的注释。
5.testNG的xml文件如下,这时候执行这个xml文件就执行了selenium测试了,最后结果会显示执行成功多少case,失败多少case。
[html] view plain copy
<suite name="Suite1" parallel="tests" thread-count="5"> <test name="test1"> <parameter name="myage" value="25"/> <parameter name="myname" value="abel,"/> <groups> <define name="allfunctest"> <include name="windows.*"/> <include name="linux.*"/> </define> <define name="all"> <include name="allfunctest"/> <include name="functest"/> <exlcude name="checkintest"/> </define> <run> <include name="all"/> <include name="aa"/> <include name="bb"/> <include name="cc"/> </run> <dependencies> <group name="aa" depends-on="bb cc"/> </dependencies> </groups> <classes> <class name="test.TestNG"/> </classes> </test> </suite>
6、接下来用ant去调用testNG的xml文件,因为最后执行是在jenkins的机器上执行,所以需要ant来进行编译,附上ant的build.xml文件。
在机器上装上ant,eclipse上装testng插件,ant插件,我安装了。
[html] view plain copy
<project name="TestNGTest" default="test" basedir="."> <!-- Define <testng> task --> <taskdef name="testng" classname="org.testng.TestNGAntTask"> <classpath> <pathelement location="lib/testng-6.8.jar"/> </classpath> </taskdef> <property name="testdir" location="build" /> <property name="srcdir" location="src" /> <property name="libdir" location="lib" /> <property name="full-compile" value="true" /> <path id="classpath.base"/> <path id="classpath.test"> <fileset dir="${libdir}"> <include name="**/testng*.jar" /> </fileset> <pathelement location="${testdir}" /> <pathelement location="${srcdir}" /> <path refid="classpath.base" /> </path> <target name="clean" > <delete verbose="${full-compile}"> <fileset dir="${testdir}" includes="**/*.class" /> </delete> </target> <target name="compile" depends="clean"> <javac srcdir="${srcdir}" destdir="${testdir}" verbose="${full-compile}"> <classpath refid="classpath.test"/> </javac> </target> <target name="test" depends="compile"> <testng outputdir="${testdir}" classpathref="classpath.test"> <xmlfileset dir="${srcdir}" includes="TestNG_study.xml"/> </testng> </target> </project>
7、如果以上都ok了,这时需要将代码提交到svn,svn服务器搭建就不说了,Linux下自带了,只需要改改东西就行了。可能会出现没有权限的问题,authz文件加上这个
[/] username=rw
8.代码提交成功,关于eclipse代码提交的问题,请参考我前面的blog。lib下的jar文件,代码,及testNG和ant的xml文件均需提交到svn。
6.一些都准备就绪,接下来需要jenkins来调用了。
7.jenkins调用:
前提:在jenkins的机器上安装ant,并修改ant的环境变量。jdk是必需品,不多说了。。
jenkins的步骤:
a.通过svn将代码check out
b.通过jenkins的ant插件invoke ant,如果出现JAVA_HOME error,需要在jenkins的system manger中安装JDK,这时还需要个Oracle帐号(free).
c.invoke ant前,先jenkins的系统管理中配置ant插件,invoke时需指定build.xml文件的位置,否则无法找到,因为build.xml不在默认的workspace目录下。
d.不出意外,这个时候应该是可以执行了。但是执行结果怎么看到呢? 安装jenkins的testNG的插件,执行testng-results.xml文件的位置,就能看到testng的report了
e.个人感觉testNG的report还是不够美观,网上查资料看有testNGxslt使用,但是我研究了后,我这怎么都运行不起来,不是少这就是少那的。。。后续有时间再搞。
8.到此,整个框架搭建完成,所有软件都在相应的官网可下载并有使用说明。
后续,testNGxslt解决了,将saxon-8.7.jar,SaxonLiaison.jar 两个文件传到lib文件夹中,记得验证文件是否错误,双击文件,如果出现错误,则文件错了,我就是因为文件有问题,所以死活整不通,后来双击一下,发现了这个问题。ant的build.xml文件加上以下内容即可:
[html] view plain copy
<path id="test.classpath"> <fileset dir="${libdir}" includes="*.jar"/> </path> <target name="testcase"> <property name="dir" value=""/> <mkdir dir="${testdir}/output"/> <xslt processor="SaxonLiaison" classpathref="test.classpath" out="${testdir}/output/index.html" style="src/testng-results.xsl" in="${testdir}/testng-results.xml"> <param name="testNgXslt.outputDir" expression="${testdir}/output/"/> <param name="testNgXslt.sortTestCaseLinks" expression="true"/> <param name="testNgXslt.testDetailsFilter" expression="FAIL,SKIP,PASS"/> </xslt> </target>