selenium+testng+reprotng+ant框架搭建配置

一、Configure
1. 安装testNG插件到eclipse.
-) 选择菜单 Help /Software updates / Find and Install.
-) 点击add button然后在location中输入http://beust.com/eclipse/
-) 确定后会自动安装testNG插件。
2. 在使用ant时候先将jdk的tools.jar引入进来。(不引入会报错Unable to find a javac;Perhaps JAVA_HOME does not point to the JDK)
二、Create a example to illustrate.
1. Create a new java project.
2. 将需要用到的包加进来。
                   selenium 包:selenium-java-client-driver.jar, selenium-server.jar
                   testng 包: testng.jar
                 reportng包:reporting.jar,velocity-dep.jar
3. Add a TestNG class.
4. 配置时: XML suit File = testng.xml
5. 在该类中添加代码,例如下
package com.selenium.testng.ant;
import org.testng.annotations.AfterSuite;
importorg.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
importcom.thoughtworks.selenium.DefaultSelenium;
importcom.thoughtworks.selenium.SeleneseTestCase;
importcom.thoughtworks.selenium.Selenium;
public class NewSample extends SeleneseTestCase{
    private Selenium selenium; 
    private String testUrl="http://...";
    @BeforeSuite
    public void beforSuite()
    {
selenium = new DefaultSelenium("localhost",4444,"*chrome",testUrl);
     selenium.setSpeed("600");
     selenium.start();
    }
    @Test
    public void login()
    {
     //login js
    }
    @Test
    public void configure()
    {
     //configure js
    }
    @Test
    public void support()
    {
     // support js
    }
    @AfterSuite
    public void afterSuit()
    {
     selenium.stop();
    }
}
6. 配置testng.xml文件
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="false">
<test name="Test" preserve-order="true">
<classes>
<class name="com.selenium.testng.ant.NewSample">
  <methods>
  <include name="login"/>
  <include name="configure"/>
  <include name="support"/>
  </methods>
</class>
</classes>
</test>
</suite>
7. 选择testng.xml然后run as TestNG Suite。看是否执行成功。(至此testng配置好)
8. 在根目录下新建一个file名为build.xml。(此文件为ant的配置文件)
9. 在project根目录下(与src,bin同级)建立lib目录并将所需要的包拷贝到改目录下。
10. 配置build.xml文件
<?xml version="1.0" ?>
<project name="testng" default="run_testng" basedir=".">
<property name="src" value="src" />
<property name="dest" value="classes" />
<property name="lib.dir" value="${basedir}/lib"/>
<property name="output.dir" value="${basedir}/OneSight"/>
<path id="compile.path">
<fileset dir="${lib.dir}/">
<include name="*.jar" />
</fileset>
<pathelement location="${src}" />
<pathelement location="${dest}"/>
</path> 
<target name="init">
<mkdir dir="${dest}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" classpathref="compile.path"/>
</target>
<taskdef resource="testngtasks" classpath="${lib.dir}/testng.jar"/>
<target name="run_testng" depends="compile" description="run testng">
<echo>running testng</echo>
<parallel>
<antcall target="startServer"/>
<sequential>
<echo taskname="waitfor" message="Wait for proxy server launch" />
<waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
<http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete"/>
</waitfor>
<antcall target="run_tests"/>
<antcall target="stopServer"/>
</sequential>
</parallel>
</target>
<!-- start selenium server -->
<target name="startServer" description="start selenium server">
<java jar="${lib.dir}/selenium-server.jar" fork="true" spawn="true"> 
<arg line="-timeout 30" />
<jvmarg value="-Dhttp.proxyHost=proxy.corporate.com"/>
<jvmarg value="-Dhttp.proxyPort=3128"/>
</java>
</target>
<!-- stop selenium server -->
<target name="stopServer" description="stop selenium server">
<get dest="result.txt" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer" ignoreerrors="true"/>
</target>
<!-- run tests --> 
<target name="run_tests" depends="compile">
<echo>running tests</echo>
<testng classpathref="compile.path" outputdir="${output.dir}" haltonfailure="true" usedefaultlisteners="false" 
    listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter" failureproperty="test.failed">
<xmlfileset dir="${src}/" >
<include name="testng.xml" />
</xmlfileset>
<sysproperty key="org.uncommons.reportng.title" value="My Test Report"/>
</testng>
<fail message="test failed.." if="test.failed"/>
</target>
</project>
11. 选择build.xml然后run as ant build即可执行成功。

12. 至此基础的selenium+testng+reporting+ant框架搭建成功。

转载自:http://blog.163.com/weixia_1985/blog/static/963047972012714335914/

你可能感兴趣的:(selenium+testng+reprotng+ant框架搭建配置)