1. 在eclipse中安装TestNGinx插件,http://beust.com/eclipse
2. eclipse已经集成了Ant,所以无需安装Ant
3. 执行testNG之后,会自动生成test-output目录
创建TestNG class文件,GoogleTest.java
package com.twioo.test.google;
import com.thoughtworks.selenium.*;
import static org.testng.AssertJUnit.assertTrue;
import org.testng.annotations.Test;
public class GoogleTest {
String url = "http://www.google.com";
private Selenium selenium = new DefaultSelenium("localhost", 4444,
"*iexplore", url);
@Test
public void 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"?>
<suite name="Suite" parallel="false">
<test name="testSearch">
<classes>
<class name="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">
<property name="src.dir" value="src" />
<property name="lib.dir" value="lib" />
<property name="test.dir" value="test" />
<property name="dist.dir" value="dist" />
<path id="test.classpath">
<pathelement location="${src.dir}" />
<pathelement location="${dist.dir}" />
<pathelement location="." />
<!-- adding the saxon jar to your classpath -->
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<taskdef name="testng" classname="com.beust.testng.TestNGAntTask" classpath="${lib.dir}/testng-6.1.1.jar" />
<target name="compile">
<!--clean the old classes-->
<delete dir="${dist.dir}" failonerror="false" />
<!--create new dist dir-->
<mkdir dir="${dist.dir}" />
<!--compile-->
<javac classpathref="test.classpath" srcdir="${src.dir}" destdir="${dist.dir}" />
</target>
<target name="start_server_and_run_tests" depends="compile" description="start selenium server and run tests">
<parallel>
<antcall target="start_selenium_server" />
<sequential>
<echo taskname="waitfor" message="wait for selenium server launch" />
<waitfor maxwait="2" maxwaitunit="minute" checkevery="10">
<http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete" />
</waitfor>
<antcall target="run_tests">
</antcall>
</sequential>
</parallel>
</target>
<target name="run_tests">
<testng classpathref="test.classpath" outputDir="test-output">
<xmlfileset dir="src" includes="testng.xml" />
</testng>
<xslt in="${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 -->
<param name="testNgXslt.outputDir" expression="${basedir}/test-output/" />
<classpath refid="test.classpath" />
</xslt>
<antcall target="stop_selenium_server" />
<fail message="ERROR: test failed!!!!!" if="test.failed" />
</target>
<target name="start_selenium_server">
<echo message="starting selenium server" />
<java jar="${lib.dir}/selenium-server-standalone-2.1.0.jar" fork="true" spawn="false" output="selenium.log" />
</target>
<target name="stop_selenium_server">
<echo message="going to stop the selenium server" />
<get taskname="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,两者要保持一致
分享到:
相关推荐
【Ant+Selenium+TestNG自动化测试方案】是一个集成的Web自动化测试框架,它结合了三个强大的工具:Apache Ant,Selenium WebDriver,以及TestNG。这个方案主要用于自动化Web应用程序的测试,确保其功能正确且稳定。 ...
在构建自动化测试解决方案时,`Selenium`、`TestNG`、`ReportNG`和`Ant`这四个组件是常用的技术栈。以下是关于这个话题的详细说明: **Selenium** 是一个广泛使用的开源自动化测试框架,它允许开发人员编写脚本来...
- **Eclipse + TestNG + Ant + Selenium 2 测试框架整合**:整合上述工具和技术,构建一个完整的自动化测试环境。 #### 六、持续集成与部署 - **自动化与持续集成**:介绍持续集成的概念及其在自动化测试中的应用。...
文档还提供了如何在Eclipse、Ant和命令行中运行TESTNG的方法,以及如何处理运行结果和重新运行失败测试的策略。 最后,Selenium的出现和使用大大简化了Web应用的功能测试工作,尤其是通过WebDriver的API集成,测试...
它提供了丰富的注解来组织测试代码,增强了测试的灵活性,并支持多种运行测试的方式,例如Eclipse、Ant、命令行等。此外,TESTNG框架还支持通过DataProvider进行数据驱动测试,这使得测试用例的复用性和数据的维护...
【 WebDriver 自动化框架】是一种基于 Selenium WebDriver 的自动化测试解决方案,它结合了 Gradle 构建工具、Jenkins 持续集成服务器以及 TestNG 测试框架,为 Web 应用程序提供高效、可靠的测试自动化服务。...
2. **Eclipse IDE**:用于开发和管理Selenium测试脚本的集成开发环境。 3. **TestNG**:用于编写和执行测试用例的测试框架。 4. **Selenium 2 (WebDriver)**:核心自动化库,提供了与不同浏览器交互的能力。 5. **...
- **Maven + TestNG**:集成构建工具和测试框架,支持更灵活的测试配置和执行。 - **Ant + JUnit**:另一种组合,使用Ant作为构建工具,JUnit作为测试框架。 - **Jenkins**:持续集成/持续部署(CI/CD)工具,可以自动...
- 计划更新的内容包括Firefox 10下的Selenium-IDE,Jmock桩的规范使用,Ant自动构建,Cobertura覆盖率计算,以及TestNG的介绍。 8. **文档来源与贡献**: - 文档主要由云层编写,同时得到了学生和技术社区的支持...