Eclipse+TestNG+ant+selenium++jenkins+svn自动化测试框架搭建

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
 
  1. <suite name="Suite1" parallel="tests" thread-count="5">  
  2.   <test name="test1">  
  3.   <parameter name="myage" value="25"/>  
  4.   <parameter name="myname" value="abel,"/>  
  5.   <groups>  
  6.      <define name="allfunctest">  
  7.         <include name="windows.*"/>  
  8.         <include name="linux.*"/>  
  9.      </define>  
  10.      <define name="all">  
  11.         <include name="allfunctest"/>  
  12.         <include name="functest"/>  
  13.         <exlcude name="checkintest"/>  
  14.      </define>  
  15.      <run>  
  16.         <include name="all"/>  
  17.         <include name="aa"/>  
  18.         <include name="bb"/>  
  19.         <include name="cc"/>  
  20.      </run>  
  21.      <dependencies>  
  22.         <group name="aa" depends-on="bb cc"/>  
  23.      </dependencies>  
  24.   </groups>  
  25.     <classes>  
  26.        <class name="test.TestNG"/>  
  27.     </classes>  
  28.   </test>  
  29. </suite>  


6、接下来用ant去调用testNG的xml文件,因为最后执行是在jenkins的机器上执行,所以需要ant来进行编译,附上ant的build.xml文件。

在机器上装上ant,eclipse上装testng插件,ant插件,我安装了。

[html]  view plain  copy
 
  1. <project name="TestNGTest" default="test" basedir=".">  
  2. <!-- Define <testng> task -->  
  3.   <taskdef name="testng" classname="org.testng.TestNGAntTask">  
  4.     <classpath>  
  5.       <pathelement location="lib/testng-6.8.jar"/>  
  6.     </classpath>  
  7.   </taskdef>  
  8.    <property name="testdir" location="build" />  
  9.    <property name="srcdir" location="src" />  
  10.    <property name="libdir" location="lib" />  
  11.    <property name="full-compile" value="true" />  
  12.    <path id="classpath.base"/>  
  13.    <path id="classpath.test">  
  14.        <fileset dir="${libdir}">  
  15.          <include name="**/testng*.jar" />  
  16.       </fileset>  
  17.       <pathelement location="${testdir}" />  
  18.       <pathelement location="${srcdir}" />  
  19.       <path refid="classpath.base" />  
  20.    </path>  
  21.    <target name="clean" >  
  22.       <delete verbose="${full-compile}">  
  23.          <fileset dir="${testdir}" includes="**/*.class" />  
  24.       </delete>  
  25.    </target>  
  26.    <target name="compile" depends="clean">  
  27.       <javac srcdir="${srcdir}" destdir="${testdir}"   
  28.          verbose="${full-compile}">  
  29.          <classpath refid="classpath.test"/>  
  30.       </javac>  
  31.    </target>  
  32.    
  33.          <target name="test"  depends="compile">  
  34.             <testng outputdir="${testdir}" classpathref="classpath.test">   
  35.                       <xmlfileset dir="${srcdir}" includes="TestNG_study.xml"/>   
  36.            </testng>  
  37.    </target>  
  38.       
  39. </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
 
  1. <path id="test.classpath">  
  2. <fileset dir="${libdir}" includes="*.jar"/>  
  3. </path>  
  4.   
  5.     <target name="testcase">  
  6.   
  7.     <property name="dir" value=""/>  
  8.   
  9.     <mkdir dir="${testdir}/output"/>  
  10.   
  11.           
  12.     <xslt processor="SaxonLiaison" classpathref="test.classpath" out="${testdir}/output/index.html" style="src/testng-results.xsl" in="${testdir}/testng-results.xml">  
  13.   
  14.     <param name="testNgXslt.outputDir" expression="${testdir}/output/"/>  
  15.   
  16.     <param name="testNgXslt.sortTestCaseLinks" expression="true"/>  
  17.   
  18.     <param name="testNgXslt.testDetailsFilter" expression="FAIL,SKIP,PASS"/>  
  19.   
  20.     </xslt>  
  21.   
  22.     </target>  

猜你喜欢

转载自blog.csdn.net/guoguo527/article/details/53174899