標籤:des style http io ar 使用 java for strong
我的需求:
1、webdriver代碼放在svn上;
2、hudson(jenkins)去執行構建,它從svn上下載代碼,並利用testng.xml來執行我設定好的測試套件;
3、接上,將結果放在指定位置;
我的裝置:
開發工具:intellij idea
自動化工具:webdriver(selenium2.0)
持續整合工具:hudson(它的外掛程式需要安裝seleniumhq,testng,ant)這些都可以下包來安裝也可以自動安裝。ANT我用的是1.8.3版本
PS:我的svn及hudson都是用我本地機器作為伺服器;
另外:在配置這個環境之前,請把你的代碼在自己的開發ide上跑一下,以保證代碼不會報錯
一、hudson外掛程式安裝及設定
http://hi.baidu.com/janice51
5/item/4c468bf7e79e780985d27821 在這兒,可以參考一下;
ps: ant的安裝我選擇是自動安裝,可以下個包,解壓下就可;
安裝完selenium後,在jenkins的工作目錄下會有一個selenium的服務包;
注意:在hudson在啟動時,你可以觀察一下log,是否可以看到會自動啟動selenium的伺服器包;所以你在構建selenium的時候就可以不用再設定伺服器,否則的話,會報錯,說連接埠已被使用;因為已經啟動過了。
二、建立hudson job,設定job
建議job的主要事情就是讓想hudson去幹啥,這裡需要設定ant,svn
svn存放代碼地址
用ANT執行testng.xml
下面這個其實可以不用設定,testng執行完會產生test-output的檔案,我們要做的就是去配置這個檔案的存放位置。
當你建立完job後,可以試著去構建一下;此時jenkins的工作目錄下會有一個workspace;
workspace放著是svn上所下載的內容。
三、編寫build.xml--重點
當所有需要外掛程式的東西設定好,點擊構建的時候,首先jenkins先去svn下載代碼,並放在workspace;
接著ant就去找workspace的build.xml。如果沒有,會告訴你不存在;有一點非常重要,就是在構建過程中你可以去jenkins的控制台去查看log,到底是哪個地方錯了;這是一個非常關鍵的地方,千萬不要忽視它。
build.xml主要做3件事,
一件是編譯java類,
另一件事執行testng.xml,
最後一件事就是將結果輸出到指定位置
編譯java類:
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" classpathref="compile.path" encoding="UTF-8"/>
</target>
srcdir="${src}"java類所在的地方;
destdir="${dest}",編譯後classes所臨時放的地方,為什麼說臨時呢,因為在build.xml我把它刪除了,你也可以不刪除它;
classpathref="compile.path",編譯所要用到一些lib;
encoding="UTF-8",這個要注意設定,如果沒有設定會報錯,如字元與**不符合等;當然首先你的java類的編碼格式需要是UTF-8;
執行testng.xml
<target name="run_tests" depends="compile">
<testng classpathref="compile.path"? failureproperty="test.failed">
<!--xml test suite file -->
<xmlfileset dir="${basedir}">
<include name="testng.xml" />
</xmlfileset>
</testng>
<antcall target="sendReport" />
<fail message="ERROR: test failed!!!!!" if="test.failed" />
</target>
<taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.4.jar" />
這裡遇到的問題:
1、Cause:
The name is undefined. Action:
Check the spelling. Action:
Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
建立任務無法成功。
解決辦法:
1)http://ant-contrib.sourceforge.net/ 下載ant-contrib-1.0b3.zip
2)下載完後,解壓,將ant-contrib-1.0b3.jar,放在你安裝的ANT下的lib下就可;
結果輸出到指定位置
<target name="transform">
<xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/test-output/testng-results.xsl" out="${basedir}/test-output/index1.html" classpathref="compile.path">
<!-- you need to specify the directory here again -->
<param name="testNgXslt.outputDir" expression="${basedir}/test-output/" />
<param name="testNgXslt.showRuntimeTotals" expression="true"/>
<!--<classpath refid="compile.path" />-->
</xslt>
</target>
這裡遇到的問題:
1、testng-results.xsl找不到這個檔案
解決辦法:
我去網上下載了一下zip,將解壓後的此檔案testng-results.xsl,放在test-output(這個檔案testng會自動產生,只要你的testng.xml被成功執行後)
PS:ANT1.7版本之後, processor = "Saxon Liaison"屬性用了之後會報錯,直接刪除就好了。
下面為整個build.xml的內容;有了上方文,可能看起來會好理解些;
<project name="kuaidi100" default="start_server_and_run_tests" basedir=".">
<property name="src" value="src/com" />
<property name="dest" value="classes" />
<property name = "log4j" value = "config"/>
<property name="lib.dir" value="lib" />
<path id="compile.path">
<fileset dir="${lib.dir}/">
<include name="*.jar" />
<include name="*.zip"/>
</fileset>
<fileset dir="${log4j}/">
<include name="*.properties" />
<include name="*.xml"/>
</fileset>
<pathelement location="${src}"/>
<pathelement location="${dest}" />
<pathelement location="${log4j}"/>
</path>
<target name="init" description = "create classes file">
<!--mkdir means : create file:classes-->
<mkdir dir="${dest}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" classpathref="compile.path" encoding="UTF-8"/>
</target>
<!--run testng ant task-->
<taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.4.jar" />???????
<target name="start_server_and_run_tests" depends="compile" description="start selenium server and run tests">
<parallel>
<antcall target="run_tests">
</antcall>
</parallel>
</target>
<target name="run_tests" depends="compile">
<testng classpathref="compile.path"? failureproperty="test.failed">
<!--xml test suite file -->
<xmlfileset dir="${basedir}">
<include name="testng.xml" />
</xmlfileset>
</testng>
<antcall target="sendReport" />
<fail message="ERROR: test failed!!!!!" if="test.failed" />
</target>
<target name="sendReport">
<delete dir="${dest}"/>
<antcall target="transform" />
</target>
<target name="transform">
<xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/test-output/testng-results.xsl" out="${basedir}/test-output/index1.html" classpathref="compile.path">
<!-- you need to specify the directory here again -->
<param name="testNgXslt.outputDir" expression="${basedir}/test-output/" />
<param name="testNgXslt.showRuntimeTotals" expression="true"/>
<!--<classpath refid="compile.path" />-->
</xslt>
</target>
</project>
這樣我的整個環境就搭成了,寫代碼及調試用了將近一周,而配置實際上只用了一個上午;
希望你也能成功;從此不用開啟idea直接用jenkins就可以搞定了。
本文轉自:http://www.spasvo.com/ceshi/open/kydycsgj/TestNG/20131218103100.html
jenkins+testng+ant+webdriver持續整合測試