eclipse+selenium+testNG+Ant集成

本文详细介绍了在Eclipse中通过安装TestNG插件进行自动化测试配置,包括创建TestNG类和XML文件、整合ANT构建系统、构建build.xml文件及运行测试的过程。同时,解决了一些常见的编码和类路径相关问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在eclipse中安装TestNGinx插件,http://beust.com/eclipse
2. eclipse已经集成了Ant,所以无需安装Ant
3. 执行testNG之后,会自动生成test-output目录
创建TestNG class文件,GoogleTest.java

  1. package com.twioo.test.google;
  2. importcom.thoughtworks.selenium.*;
  3. importstatic org.testng.AssertJUnit.assertTrue;
  4. importorg.testng.annotations.Test;
  5. publicclass GoogleTest {
  6. Stringurl = "http://www.google.com";
  7. privateSelenium selenium = new DefaultSelenium("localhost", 4444,
  8. "*iexplore", url);
  9. @Test
  10. publicvoid testSearch() {
  11. selenium.open("/");
  12. selenium.type("q", "selenium rc");
  13. selenium.click("btnG");
  14. selenium.waitForPageToLoad("30000");
  15. assertTrue(selenium.isTextPresent("Results * for selenium rc"));
  16. }
  17. }

创建TestNG文件时,可以指定XML suite file,如testng.xml,会自动生成XML文件,内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <suitename="Suite" parallel="false">
  3. <testname="testSearch">
  4. <classes>
  5. <classname="com.twioo.test.google.GoogleTest"/>
  6. </classes>
  7. </test>
  8. </suite>

4. 在testng.xml右键,点击run as -> TestNG Suite,即可运行
5. 整合ANT,build.xml文件内容如下

  1. <project name="myproject" basedir="." default="start_server_and_run_tests">
  2. <propertyname="src.dir" value="src" />
  3. <propertyname="lib.dir" value="lib" />
  4. <propertyname="test.dir" value="test" />
  5. <propertyname="dist.dir" value="dist" />
  6. <pathid="test.classpath">
  7. <pathelementlocation="${src.dir}" />
  8. <pathelementlocation="${dist.dir}" />
  9. <pathelementlocation="." />
  10. <!-- adding the saxon jar to your classpath -->
  11. <filesetdir="${lib.dir}" includes="*.jar" />
  12. </path>
  13. <taskdefname="testng" classname="com.beust.testng.TestNGAntTask" classpath="${lib.dir}/testng-6.1.1.jar" />
  14. <targetname="compile">
  15. <!--clean the old classes-->
  16. <deletedir="${dist.dir}" failonerror="false" />
  17. <!--create new dist dir-->
  18. <mkdirdir="${dist.dir}" />
  19. <!--compile-->
  20. <javacclasspathref="test.classpath" srcdir="${src.dir}" destdir="${dist.dir}" />
  21. </target>
  22. <targetname="start_server_and_run_tests" depends="compile" description="start selenium server and run tests">
  23. <parallel>
  24. <antcalltarget="start_selenium_server" />
  25. <sequential>
  26. <echotaskname="waitfor" message="wait for selenium server launch" />
  27. <waitformaxwait="2" maxwaitunit="minute" checkevery="10">
  28. <httpurl="http://localhost:4444/selenium-server/driver/?cmd=testComplete" />
  29. </waitfor>
  30. <antcalltarget="run_tests">
  31. </antcall>
  32. </sequential>
  33. </parallel>
  34. </target>
  35. <targetname="run_tests">
  36. <testngclasspathref="test.classpath" outputDir="test-output">
  37. <xmlfilesetdir="src" includes="testng.xml" />
  38. </testng>
  39. <xsltin="${basedir}/test-output/testng-results.xml" style="${basedir}/test-output/testng-results.xsl" out="${basedir}/test-output/index1.html">
  40. <!-- you need to specify the directory here again -->
  41. <paramname="testNgXslt.outputDir" expression="${basedir}/test-output/" />
  42. <classpathrefid="test.classpath" />
  43. </xslt>
  44. <antcalltarget="stop_selenium_server" />
  45. <failmessage="ERROR: test failed!!!!!" if="test.failed" />
  46. </target>
  47. <targetname="start_selenium_server">
  48. <echomessage="starting selenium server" />
  49. <javajar="${lib.dir}/selenium-server-standalone-2.1.0.jar" fork="true" spawn="false" output="selenium.log" />
  50. </target>
  51. <targetname="stop_selenium_server">
  52. <echomessage="going to stop the selenium server" />
  53. <gettaskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer" dest="stop.out" ignoreerrors="true" />
  54. </target>
  55. </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-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值