`

eclipse+selenium+testNG+Ant集成

    博客分类:
  • ANT
阅读更多
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,两者要保持一致

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics