需要通过ANT构建一个java文件

时间:2015-11-27 11:56:40

标签: java selenium ant junit build.xml

这是我的java文件。

package test;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class MyTest{
    @Test
    public void startWebDriver(){
            WebDriver driver;
            driver = new FirefoxDriver();
            driver.navigate().to("http://google.com");
            Assert.assertTrue("title should start with google",driver.getTitle().startsWith("Google"));
            driver.close();
            driver.quit();
        }
}

我已将所有selenium和Junit依赖项存储在名为Xjars的文件中。

这是我的build.xml

<?xml version="1.0"?>
<project name="AntSelenium" default="main" basedir=".">
  <!-- Sets variables which can later be used. -->
  <!-- The value of a property is accessed via ${} -->
  <property name="src.dir" location="src" />

  <property name="build.dir" location="classes" />

  <!-- Variables used for JUnit testin -->
  <property name="test.dir" location="src" />
  <property name="test.report.dir" location="testreport" />
  <property name="jar.dir" location="Xjars"/>
  <path id="classpath">     
      <pathelement path="${basedir}"/>    
      <fileset dir="${jar.dir}" >
          <include name="**/*.jar"/> 
          </fileset>

  </path>

  <!-- Define the classpath which includes the junit.jar and the classes after compiling-->
  <path id="junit.class.path">

    <pathelement location="Xjars/junit-4.8.2.jar" />
    <pathelement location="${build.dir}" />
  </path>


  <!-- Deletes the existing build, docs and dist directory-->
  <target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${test.report.dir}" />
  </target>

  <!-- Creates the  build, docs and dist directory-->
  <target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${test.report.dir}" />
  </target>

  <!-- Compiles the java code (including the usage of library for JUnit -->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" includeantruntime="false" />

      <!-- <pathelement path="Xjars/cglib-nodep-2.1_3.jar"/>
        <pathelement path="Xjars/commons-codec-1.10.jar"/>
        <pathelement path="Xjars/commons-collections-3.2.1.jar"/>
        <pathelement path="Xjars/commons-exec-1.3.jar"/>
        <pathelement path="Xjars/commons-io-2.4.jar"/>
        <pathelement path="Xjars/commons-lang3-3.4.jar"/>
        <pathelement path="Xjars/commons-logging-1.2.jar"/>
        <pathelement path="Xjars/cssparser-0.9.16.jar"/>
        <pathelement path="Xjars/gson-2.3.1.jar"/>
        <pathelement path="Xjars/guava-18.0.jar"/>
        <pathelement path="Xjars/htmlunit-2.18.jar"/>
        <pathelement path="Xjars/htmlunit-core-js-2.17.jar"/>
        <pathelement path="Xjars/httpclient-4.5.1.jar"/>
        <pathelement path="Xjars/httpcore-4.4.3.jar"/>
        <pathelement path="Xjars/httpmime-4.5.jar"/>
        <pathelement path="Xjars/jetty-io-9.2.12.v20150709.jar"/>
        <pathelement path="Xjars/jetty-util-9.2.12.v20150709.jar"/>
        <pathelement path="Xjars/jna-4.1.0.jar"/>
        <pathelement path="Xjars/jna-platform-4.1.0.jar"/>
        <pathelement path="Xjars/junit-4.8.2.jar"/>
        <pathelement path="Xjars/junit-4.8.2-sources.jar"/>
        <pathelement path="Xjars/nekohtml-1.9.22.jar"/>
        <pathelement path="Xjars/netty-3.5.2.Final.jar"/>
        <pathelement path="Xjars/sac-1.3.jar"/>
        <pathelement path="Xjars/selenium-api-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-chrome-driver-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-edge-driver-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-firefox-driver-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-htmlunit-driver-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-java-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-leg-rc-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-remote-driver-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-safari-driver-2.48.2.jar"/>
        <pathelement path="Xjars/selenium-support-2.48.2.jar"/>
        <pathelement path="Xjars/serializer-2.7.2.jar"/>
        <pathelement path="Xjars/webbit-0.4.14.jar"/>
        <pathelement path="Xjars/websocket-api-9.2.12.v20150709.jar"/>
        <pathelement path="Xjars/websocket-client-9.2.12.v20150709.jar"/>
        <pathelement path="Xjars/websocket-common-9.2.12.v20150709.jar"/>
        <pathelement path="Xjars/xalan-2.7.2.jar"/>
        <pathelement path="Xjars/xercesImpl-2.11.0.jar"/>
        <pathelement path="Xjars/xml-apis-1.4.01.jar"/> -->


  </target>

  <!-- Run the JUnit Tests -->
  <!-- Output is XML, could also be plain-->
  <target name="junit" depends="compile">
    <junit printsummary="on"  haltonfailure="yes">
      <classpath refid="junit.class.path" />
      <formatter type="plain" usefile="false" />
      <batchtest todir="${test.report.dir}">
        <fileset dir="${src.dir}">
          <include name="**/*Test*.java" />
        </fileset>
      </batchtest>
    </junit>
  </target>

  <target name="main" depends="compile, junit">
    <description>Main target</description>
  </target>

</project> 

但最后我得到了这个错误Buildfile:

 C:\Users\manish\workspace\AntSelenium\build.xml clean:    [delete]
 Deleting directory C:\Users\manish\workspace\AntSelenium\classes   
 [delete] Deleting directory
 C:\Users\manish\workspace\AntSelenium\testreport makedir:
     [mkdir] Created dir: C:\Users\manish\workspace\AntSelenium\classes
     [mkdir] Created dir: C:\Users\manish\workspace\AntSelenium\testreport compile:
     [javac] Compiling 2 source files to C:\Users\manish\workspace\AntSelenium\classes junit:
     [junit] Running test.MyTest
     [junit] Testsuite: test.MyTest
     [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
     [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
     [junit]    Caused an ERROR
     [junit] org/openqa/selenium/WebDriver
     [junit] java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
     [junit]    at java.lang.Class.forName0(Native Method)
     [junit]    at java.lang.Class.forName(Unknown Source)
     [junit]    at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:36)
     [junit]    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:452)
     [junit]    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:139)
     [junit] Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
     [junit]    at java.lang.ClassLoader.loadClass(Unknown Source)

 BUILD FAILED C:\Users\manish\workspace\AntSelenium\build.xml:94: Test
 test.MyTest failed

 Total time: 4 seconds Ant is not able to find the org.openqa.selenium.WebDriver class, 

虽然我所包含的jar是Xjars文件夹的一部分。

1 个答案:

答案 0 :(得分:0)

为了清楚起见,但更多的是评论而不是答案

在你的ant构建文件中添加它来调试使用的类路径

{{1}}

它将打印出类路径并帮助您调试是否正确拾取了罐子