分享selenium+testng+reprotng+ant配置

来源:互联网 发布:alias软件logo 编辑:程序博客网 时间:2024/05/18 03:48
 一、Configure
1. 安装testNG插件到eclipse.
-) 选择菜单 Help /Software updates / Find and Install.
-) 点击add button然后在location中输入http://beust.com/eclipse/
-) 确定后会自动安装testNG插件。
2. 在使用ant时候先将jdktools.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;
publicclass NewSampleextends SeleneseTestCase{
  private Selenium selenium;
  private String testUrl="http://...";
  @BeforeSuite
  publicvoid beforSuite()
  {
selenium =new DefaultSelenium("localhost",4444,"*chrome",testUrl);
  selenium.setSpeed("600");
  selenium.start();
  }
  @Test
  publicvoid login()
  {
  //loginjs
  }
  @Test
  publicvoid configure()
  {
  //configurejs
  }
  @Test
  publicvoid support()
  {
  // supportjs
  }
  @AfterSuite
  publicvoid afterSuit()
  {
  selenium.stop();
  }
}
6.配置testng.xml文件
<!DOCTYPEsuiteSYSTEM "http://testng.org/testng-1.0.dtd">
<suitename="Suite"parallel="false">
<testname="Test"preserve-order="true">
<classes>
<classname="com.selenium.testng.ant.NewSample">
  <methods>
  <includename="login"/>
  <includename="configure"/>
  <includename="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" ?>
<projectname="testng"default="run_testng"basedir=".">
<propertyname="src"value="src"/>
<propertyname="dest"value="classes"/>
<propertyname="lib.dir"value="${basedir}/lib"/>
<propertyname="output.dir"value="${basedir}/OneSight"/>
<pathid="compile.path">
<filesetdir="${lib.dir}/">
<includename="*.jar"/>
</fileset>
<pathelementlocation="${src}"/>
<pathelementlocation="${dest}"/>
</path>
<targetname="init">
<mkdirdir="${dest}"/>
</target>
<targetname="compile"depends="init">
<javacsrcdir="${src}"destdir="${dest}"classpathref="compile.path"/>
</target>
<taskdefresource="testngtasks"classpath="${lib.dir}/testng.jar"/>
<targetname="run_testng"depends="compile"description="run testng">
<echo>running testng</echo>
<parallel>
<antcalltarget="startServer"/>
<sequential>
<echotaskname="waitfor"message="Wait for proxy server launch"/>
<waitformaxwait="2"maxwaitunit="minute"checkevery="100">
<httpurl="http://localhost:4444/selenium-server/driver/?cmd=testComplete"/>
</waitfor>
<antcalltarget="run_tests"/>
<antcalltarget="stopServer"/>
</sequential>
</parallel>
</target>
<!-- start selenium server -->
<targetname="startServer"description="start selenium server">
<javajar="${lib.dir}/selenium-server.jar"fork="true"spawn="true">
<argline="-timeout 30"/>
<jvmargvalue="-Dhttp.proxyHost=proxy.corporate.com"/>
<jvmargvalue="-Dhttp.proxyPort=3128"/>
</java>
</target>
<!-- stop selenium server -->
<targetname="stopServer"description="stop selenium server">
<getdest="result.txt"src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"ignoreerrors="true"/>
</target>
<!-- run tests -->
<targetname="run_tests"depends="compile">
<echo>running tests</echo>
<testngclasspathref="compile.path"outputdir="${output.dir}"haltonfailure="true"usedefaultlisteners="false"
  listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter"failureproperty="test.failed">
<xmlfilesetdir="${src}/">
<includename="testng.xml"/>
</xmlfileset>
<syspropertykey="org.uncommons.reportng.title"value="My Test Report"/>
</testng>
<failmessage="test failed.."if="test.failed"/>
</target>
</project>
11.选择build.xml然后run as ant build即可执行成功。
12. 至此基础的selenium+testng+reporting+ant框架搭建成功。
原创粉丝点击