1. 在eclipse中安装TestNGinx插件,http://beust.com/eclipse
2. eclipse已经集成了Ant,所以无需安装Ant
3. 执行testNG之后,会自动生成test-output目录
创建TestNG class文件,GoogleTest.java
- package com.twioo.test.google;
- importcom.thoughtworks.selenium.*;
- importstatic org.testng.AssertJUnit.assertTrue;
- importorg.testng.annotations.Test;
- publicclass GoogleTest {
- Stringurl = "http://www.google.com";
- privateSelenium selenium = new DefaultSelenium("localhost", 4444,
- "*iexplore", url);
- @Test
- publicvoid testSearch() {
- selenium.open("/");
- selenium.type("q", "selenium rc");
- selenium.click("btnG");
- selenium.waitForPageToLoad("30000");
- assertTrue(selenium.isTextPresent("Results * for selenium rc"));
- }
- }
创建TestNG文件时,可以指定XML suite file,如testng.xml,会自动生成XML文件,内容如下
- <?xml version="1.0" encoding="UTF-8"?>
- <suitename="Suite" parallel="false">
- <testname="testSearch">
- <classes>
- <classname="com.twioo.test.google.GoogleTest"/>
- </classes>
- </test>
- </suite>
4. 在testng.xml右键,点击run as -> TestNG Suite,即可运行
5. 整合ANT,build.xml文件内容如下
- <project name="myproject" basedir="." default="start_server_and_run_tests">
- <propertyname="src.dir" value="src" />
- <propertyname="lib.dir" value="lib" />
- <propertyname="test.dir" value="test" />
- <propertyname="dist.dir" value="dist" />
- <pathid="test.classpath">
- <pathelementlocation="${src.dir}" />
- <pathelementlocation="${dist.dir}" />
- <pathelementlocation="." />
- <!-- adding the saxon jar to your classpath -->
- <filesetdir="${lib.dir}" includes="*.jar" />
- </path>
- <taskdefname="testng" classname="com.beust.testng.TestNGAntTask" classpath="${lib.dir}/testng-6.1.1.jar" />
- <targetname="compile">
- <!--clean the old classes-->
- <deletedir="${dist.dir}" failonerror="false" />
- <!--create new dist dir-->
- <mkdirdir="${dist.dir}" />
- <!--compile-->
- <javacclasspathref="test.classpath" srcdir="${src.dir}" destdir="${dist.dir}" />
- </target>
- <targetname="start_server_and_run_tests" depends="compile" description="start selenium server and run tests">
- <parallel>
- <antcalltarget="start_selenium_server" />
- <sequential>
- <echotaskname="waitfor" message="wait for selenium server launch" />
- <waitformaxwait="2" maxwaitunit="minute" checkevery="10">
- <httpurl="http://localhost:4444/selenium-server/driver/?cmd=testComplete" />
- </waitfor>
- <antcalltarget="run_tests">
- </antcall>
- </sequential>
- </parallel>
- </target>
- <targetname="run_tests">
- <testngclasspathref="test.classpath" outputDir="test-output">
- <xmlfilesetdir="src" includes="testng.xml" />
- </testng>
- <xsltin="${basedir}/test-output/testng-results.xml" style="${basedir}/test-output/testng-results.xsl" out="${basedir}/test-output/index1.html">
- <!-- you need to specify the directory here again -->
- <paramname="testNgXslt.outputDir" expression="${basedir}/test-output/" />
- <classpathrefid="test.classpath" />
- </xslt>
- <antcalltarget="stop_selenium_server" />
- <failmessage="ERROR: test failed!!!!!" if="test.failed" />
- </target>
- <targetname="start_selenium_server">
- <echomessage="starting selenium server" />
- <javajar="${lib.dir}/selenium-server-standalone-2.1.0.jar" fork="true" spawn="false" output="selenium.log" />
- </target>
- <targetname="stop_selenium_server">
- <echomessage="going to stop the selenium server" />
- <gettaskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer" dest="stop.out" ignoreerrors="true" />
- </target>
- </project>
6. 执行build.xml之后,会在test-output文件夹下生成index1.htm文件,打开即可看到结果
遇到的问题:
1. 警告:编码 UTF8 的不可映射字符
原因:eclipse工程中默认的编码是GBK,xml中指定的是utf-8,两者要保持一致
2. 执行build.xml时报Cannot find class in classpath
原因:没有编译,编译之后就可以了
参考的文档
http://www.ibm.com/developerworks/cn/java/j-testng/
http://www.seleniumcn.cn/simple/index.php?t164.html-