一、新建一個項目html
二、編寫測試腳本java
三、配置ant的build.xml腳本web
四、集成到jenkins,並運行chrome
1.新建項目 瀏覽器
注意jdk的版本要一致eclipse
eclipse Window --Preference --java --Compilerjvm
ant Window --Preference -- Ant --Runtime --Global Entries工具
新建一個TestNG Class測試
導入項目須要的libui
配置reportng監聽器
二、測試代碼以下:
package TestKY; import org.testng.annotations.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.*; import org.testng.annotations.*; import org.testng.Assert; public class testkaiyang { WebDriver driver; @Test public void loginyuheng() throws Exception { //若是火狐瀏覽器沒有默認安裝在C盤,須要制定其路徑 也能夠引入其餘瀏覽器 //System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); System.setProperty("webdriver.chrome.driver", "D:\\Python27\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com/"); WebElement txtbox = driver.findElement(By.name("wd")); txtbox.sendKeys("Glen"); WebElement btn = driver.findElement(By.id("su")); btn.click(); String expectedTitle = "百度一下,你就知道"; String actualTitle = driver.getTitle(); System.out.println(actualTitle); Assert.assertEquals(actualTitle,expectedTitle); driver.close(); } public void tearDown(){ driver.quit(); } }
顯示結果經過,同時運行一下testng.xml看看是否是能經過
這時刷新一下項目 就能夠在新生成的test-output中看到reportng生成的html報告了
三、配置ant的build文件
<?xml version="1.0" encoding="UTF-8" ?> <project name="KaiYang" default="run" basedir="."> <echo message="import libs" /> <echo>Java-Home: ${java.home}</echo> <echo>Java-Version: ${java.version}</echo> <property name="src.dir" value="src" /> <property name="dest.dir" value="build" /> <property name="dest.report" value="test-output" /> <path id="run.classpath"> <fileset dir="${basedir}"> <include name="lib/*.jar" /> </fileset> </path> <taskdef name="testng" classname="com.beust.testng.TestNGAntTask"> <classpath> <pathelement location="lib/testng-6.8.8.jar"/> </classpath> </taskdef> <target name="clean"> <delete dir="${dest.dir}" /> </target> <target name="compile" depends="clean"> <echo message="mkdir" /> <mkdir dir="${dest.dir}" /> <javac target="1.7" srcdir="${src.dir}" destdir="${dest.dir}" encoding="UTF-8" debug="on" includeAntRuntime="false" > <classpath refid="run.classpath" /> </javac> </target> <path id="runpath"> <path refid="run.classpath" /> <pathelement location="${dest.dir}" /> </path> <target name="run" depends="compile"> <testng classpathref="runpath" outputDir="${dest.report}" haltOnFailure="true" useDefaultListeners="false" listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,org.testng.reporters.FailedReporter"> <sysproperty key="file.encoding" value="UTF-8" /> <!--<testng classpathref="runpath" outputDir="${dest.report}" haltOnFailure="true" useDefaultListeners="true" >--> <xmlfileset dir="${src.dir}" includes="testng.xml" /> <jvmarg value="-ea" /> </testng> </target> </project>
用ant運行build.xml文件 (ant爲eclipse自帶工具 能夠在 run as Exteral Tools Configuration...裏面添加)
在test-output的html中可查看報告文件:
四、配置到jenkins
手工點擊構建 查看工做空間:
若是遇到不啓動瀏覽器的狀況 請參照我轉載的另外一篇博文 :http://www.cnblogs.com/tester-hehehe/p/5504086.html
總結一下 遇到主要的問題 ant的jdk環境與平臺不一致
ant編譯build.xml文件生成的html中文亂碼 已在代碼中添加
<sysproperty key="file.encoding" value="UTF-8" /> 解決