" testng不支持嵌套的" java"元素。" - 执行build.xml时 - JAR目标

时间:2018-01-12 10:11:03

标签: eclipse selenium testng executable-jar build.xml

我们想要创建Jar,从远程机器部署和执行jar文件。

由于testNG中的规定创建了主类文件

从这个类文件运行时它完全执行,但是当执行表单构建XML时会出现以下错误

  

" testng不支持嵌套" java" 。元素"

     
      
  1. TestNG版本 - 6.13.1(尝试使用较低版本)
  2.   
  3. Eclipse - Neon.3发布(4.6.3)
  4.   
  5. 硒 - 3.7.1
  6.   
  7. Ant - 1.10.1
  8.   

经过长时间的搜索和不同的可能性,我无法得到解决方案。

public static void main(String[] args) {

    // Create object of TestNG Class
    TestNG runner = new TestNG();

    // Create a list of String
    List<String> suitefiles = new ArrayList<String>();

    // Add xml file which you have to execute
    suitefiles.add("D:\\Projects\\TestAutomation\\AgainFinal_Jartest\\TestNG.xml");

    // now set xml file for execution
    runner.setTestSuites(suitefiles);

    // finally execute the runner using run method
    runner.run();
}
<!-- compile -->
<target name="compile" depends="init, clean" > 
    <delete includeemptydirs="true" quiet="true">
        <fileset dir="${test.dest}" includes="**/*"/>
    </delete>
    <echo message="making directory..."/>
    <mkdir dir="${test.dest}"/>
    <echo message="classpath------: ${test.classpath}"/>
    <echo message="compiling..."/>
    <javac debug="true" destdir="${test.dest}" srcdir="${test.src}" target="1.8" classpath="${test.classpath}" includeantruntime="false">
    </javac>
</target>

<target name="JarCreation" depends="compile" description="package, output to JAR">
    <!-- Create the distribution directory -->
    <mkdir dir="${jar.dir}" />
    <echo message="Creating Jarfile ..!"/>

    <!-- Put everything in ${build} into the {$ws.project.name}.jar file -->
    <jar jarfile="${jar.dir}/${ws.project.name}.jar" basedir="${basedir}" >
        <manifest>
        <!-- create an executable Jar -->
            <attribute name="Main-Class" value="MyWorld.MainTestNGClass"/>
            <attribute name="Class-Path" value="."/>
        </manifest>
        <fileset dir="build"/>    

    </jar>
    <echo message="Created an executable Jarfile ..!"/> 
</target> 

<!-- build -->
<target name="build" depends="init">
</target>

<target name="run" depends="compile">
    <java classname="MyWorld.MainTestNGClass" fork="true"></java>
    <sysproperty key="env.key" value="CK_AUTO_DEV"></sysproperty>
</target>

<target name="runs" depends="compile,JarCreation">
    <testng classpath="./libs/testng-6.13.1.jar" suitename="Framework for Desktop website">         
        <java jar="${jar.dir}/${ws.project.name}.jar" fork="true"/>
        <sysproperty key="env.key" value="CK_AUTO_DEV"></sysproperty>
    </testng>
</target>

2 个答案:

答案 0 :(得分:1)

我认为问题是由于<java jar="${jar.dir}/${ws.project.name}.jar" fork="true"/>

中包含<target name="runs" depends="compile,JarCreation">

要解决此问题,您需要从<java>元素中删除<testng>元素,然后重试。

有关可用选项的更多信息,请参阅here

答案 1 :(得分:0)

<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef resource="testngtasks" classpath="${dependencies}/testng-6.9.9.jar" />
<target name="testng-execution" depends="jarextraction">
    <testng testjar="${cashkaroJar}" xmlPathInJar="local/${xmlFile}" classpathref="classpath" useDefaultListeners="true">
        <sysproperty key="env.URL_key" value="CK_AUTO_DEV"></sysproperty> 
    </testng>
</target>