【问题标题】:TestNg/Selenium call by ant always return Cannot find class in the classpath蚂蚁调用TestNg/Selenium总是返回在类路径中找不到类
【发布时间】:2014-02-06 19:48:21
【问题描述】:

我对这个设置很陌生。并且在通过 ant 使用 TestNG 调用我的项目时遇到问题。

我可以在 Eclipse 中毫无问题地运行 testng.xml,但我总是收到 Ant 在类路径中找不到类。

构建.xml

<project basedir="." default="runTest" name="Ant file for TestNG">

<property name="src" location="src" />
<property name="bin" location="bin" />
<property name="telus" location="C:\ESP_Testware\ESP_Projects\Selenium\telus-pharma-integration-tests\src\test\resources\suite\local" />
<property name="libs" location="lib" />

<path id="class.path">
     <pathelement location="${libs}/testng-6.4.jar" />
     <pathelement location="${libs}/selenium-java-client-driver.jar" />
     <pathelement location="${libs}/selenium-server-standalone-2.39.0.jar" />
     <pathelement location="${bin}"/>
     <pathelement location="${telus}"/>

     </path>

<taskdef name="testng" classname="org.testng.TestNGAntTask">
<classpath>
<pathelement location="${libs}/testng-6.4.jar"/>
</classpath>
</taskdef>

<target name="runTest">

     <echo message="mkdir"/>

 <mkdir dir="testng_output"/><!-- Create the output directory. -->
     <echo message= "TestNg Start"/>
  <testng outputdir="testng_output" classpathref="class.path"> 
     <xmlfileset dir="${telus}" includes="testng.xml"/> 
    <!--    <xmlfileset dir="." includes="TestNG2.xml"/> -->
   </testng>

</target>
 </project> 

Testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Bolt harness QA" verbose="1">
    <parameter name="test.env" value="qa" />
    <parameter name="selenium.url" value="https://www.google.com" />
    <!-- Valid values for browser: FF, IE, Chrome -->
    <parameter name="selenium.browser" value="Chrome" />

    <listeners>
        <listener class-name="com.gdo.test.integration.listener.SoftAssertTestListener" />
    </listeners>

    <test name="Test_MS_Website" preserve-order="true">
        <classes>

            <class name="com.gdo.telus.SC006">
                <methods>
                    <include name="Web_InvalidPassword" />
                    <exclude name="Web_LockedAccount" />
                </methods>
            </class>

        </classes>
    </test>
</suite>

我的班级在这条路上:

C:\ESP_Testware\ESP_Projects\Selenium\telus-pharma-integration-tests\src\test\java\com\gdo\telus

感谢您的帮助。

【问题讨论】:

    标签: ant testng selenium-rc


    【解决方案1】:

    试试我的 build.xml 文件,我确实将 ReportNG 插件添加到这个 build.xml 文件中,以生成更好看的报告,而不是默认的 TestNG 报告。您可以下载 ReportNG 的 jar 文件并将其放入您的 lib 文件夹中,它应该仍然可以正常工作:

    <project name="Some Bullshit Goes Here" default="clean" basedir=".">
    
        <!-- Initilization properties -->
            <!-- <property name="lib.dir" value="${basedir}/lib"/> -->
    
    <!-- using the ${basedir} allows you to use relative paths. It will use the working directory and add folders that you specify -->
    
        <property name="build.dir" value="${basedir}/build"/>
        <property name="lib.dir" value="hardcoded value can go here"/>
        <property name="src.dir" value="${basedir}/src"/>
        <property name="bin.dir" value="${basedir}/bin"/>
        <property name="output.dir" value="${basedir}/output"/>
    
    
    <!-- I chose to hardcode the location where my jar library files will be, it will be used for compilation. Again you can set relative path if you wish.-->
    <path id="assloadoflibs">
        <fileset dir="/automated/tests/library">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="${basedir}/bin"/>
    </path>
    
        <!-- setting libraries -->
        <target name="setClassPath">
            <path id="classpath_jars">
                <pathelement path="${basedir}/"/>
            <fileset dir="/automated/tests/library" includes="*.jar"/>
            </path>
    
        <!-- Convert jar collection from a given reference into one list, storing the result into a given property, separated by colon -->
    
        <pathconvert pathsep=":" property="test.classpath" refid="classpath_jars"/>
    </target>
    
    
    <target name="loadTestNG" depends="setClassPath"> 
    <!-- Creating task definition for TestNG task -->
        <taskdef resource="testngtasks" classpath="${test.classpath}"/> 
    
        </target>  
    
        <target name="init">
        <!-- Creating build directory structure used by compile -->
            <mkdir dir="${build.dir}"/>
        </target>
    
        <target name="clean">
            <echo message="deleting existing build directory"/>
            <delete dir="${build.dir}"/>
        </target>
    
        <!-- In compile target dependency is given over clean target followed by init,
        this order makes sure that build directory gets created before compile takes place
        This is how a clean complile is achieved.
        -->
    
        <target name="compile" depends="clean,init,setClassPath,loadTestNG">
            <echo message="classpath:${test.classpath}"/>
            <echo message="compiling..."/>
            <javac destdir="${build.dir}" srcdir="${src.dir}" classpath="${test.classpath}"/>
    
    
        </target>
    
        <target name="run" depends="compile">
    
            <!-- testng classpath has been provided reference of jar files and compiled classes
            this will generate report NG report.
            -->
    
        <testng classpath="${test.classpath}:${build.dir}" outputdir="${basedir}/output" haltonfailure="false" useDefaultListeners="true" listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter" classpathref="reportnglibs">
    <xmlfileset dir="${basedir}" includes="testng.xml"/>
    
    <!-- This value here will show the title of the report --> 
    <sysproperty key="org.uncommons.reportng.title" value="Example Test Report"/>
            </testng>
        </target>
    </project>
    

    这是我的 TestNG.xml 文件:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    
    <suite name="Example Test Suite">
    
    <test name ="Example TestCase Name">
        <classes>
            <class name="packageName.JavaFilename"></class>
        </classes>
    </test>
    </suite>
    

    【讨论】:

    • 我不想编译,我只想在类文件上运行我的测试。我已经尝试过您的 build.xml 并且编译步骤返回了很多错误。缺少符号和其他。我的构建 xml 无需编译即可运行我的 testng,但似乎有关于类路径的问题。
    • 黑暗中的另一张照片。我查看了您的 build.xml 文件注意到: classpathref="class.path" 尝试将其转换为变量: classpathref="${class.path}" 显然您需要确保变量设置正确,指向所有你的课是。基本上,问题是,它不知道如何运行您的测试,并且您必须为其提供 .class 文件和 java 库(jar 文件)的位置。当它执行测试时,testng 监听器会接收到任何失败。
    【解决方案2】:

    我在这个网站上找到了我的答案。我需要使用 maven 来调用我的解决方案。

    http://rationaleemotions.wordpress.com/2012/05/14/continuous-integration-with-selenium/

    不过还是谢谢你的帮助

    【讨论】:

      猜你喜欢
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 2011-10-28
      • 2017-09-13
      • 2011-11-27
      • 1970-01-01
      • 2011-02-26
      相关资源
      最近更新 更多