selenium+testng+reportng+ant+jenkins集成日记

1、新建一个项目

2、编写测试脚本

3、配置ant的build.xml脚本

4、集成到jenkins,并运行

1.新建项目  

注意jdk的版本要一致

eclipse  Window --Preference --java --Compiler

ant   Window --Preference -- Ant --Runtime --Global Entries

新建一个TestNG Class

导入项目需要的lib

配置reportng监听器

 

2、测试代码如下:

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报告了

3、配置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中可查看报告文件:

4、配置到jenkins

 

手工点击构建  查看工作空间:

如果遇到不启动浏览器的情况 请参照我转载的另一篇博文 :http://www.cnblogs.com/tester-hehehe/p/5504086.html

 总结一下 遇到主要的问题     ant的jdk环境与平台不一致

                                 ant编译build.xml文件生成的html中文乱码   已在代码中添加

                             <sysproperty key="file.encoding" value="UTF-8" />    解决
原文地址:https://www.cnblogs.com/tester-hehehe/p/5668007.html