2011-12-28 115 views
2

我有一個測試類,如果我在eclipse中運行,該類的所有測試運行良好。 (右鍵單擊文件,以jUnit身份運行),但是當我嘗試使用Ant腳本運行時,它失敗。無法通過ANT運行測試

實際上這個測試是針對2個瀏覽器運行的。這項測試對IE瀏覽器運行良好。它對IE運行良好。但是測試無法針對Chrome運行。我不確定發生了什麼。 我已將所有與驅動程序有關的信息放在項目路徑的所有資源/驅動程序下。瀏覽器配置文件保存在項目的browserProfiles文件夾下。

我不確定爲什麼測試無法在Chrome中找到。

我附加了測試代碼和代碼,我嘗試在源代碼中創建webdriver和seldriver。

package com.mytests; 

public class MyParamTest { 
private MyWrapperForBrowser browser; 

@Before 
public void setup(){ 
    b = new MyWrapperForBrowser("chrome"); 
    b.start(); 
} 

@Test 
public void testCURDOnEntity() { 
    MyEntity e = new MyEntity(browser); 
    Assert.assertTrue(e.createMessage("message", "text")); 
    // more business logic.. 
} 
} 

和源代碼

package com.mysrc; 

import java.util.*; 
import org.openqa.selenium.*; 

public class MyWrapperForBrowser { 

    private final WebDriver webDriver; 
    private WebDriverBackedSelenium selDriver; 


public MyWrapperForBrowser(String driver) { 
     if (driver.equalsIgnoreCase("IE") 
     { 
      // create webDriver , selDriver 
     } 
     else{ 

     System.setProperty("webdriver.chrome.driver", 
        "allresources/drivers/chromedriver.exe"); 
     DesiredCapabilities broserCapabilities = DesiredCapabilities.chrome(); 
     broserCapabilities.setCapability("chrome.switches", Arrays.asList("--start-minimized")); 
     String url = this.getClass().getClassLoader().getResource("browserProfiles/ChromeProfile").getPath() 
       .substring(1); 
     broserCapabilities.setCapability("chrome.switches", 
       Arrays.asList("--user-data-dir=" + url)); 
     webDriver = new ChromeDriver(broserCapabilities); 
     selDriver = new WebDriverBackedSelenium(webDriver, ""); 
} 
} 

public void start() { 
    webDriver.get(getURL()); 
    selDriver.windowMaximize(); 
} 
} 

在運行Ant構建這我得到堆棧跟蹤爲:

[junit] java.util.zip.ZipException: error in opening zip file 
[junit]  at java.util.zip.ZipFile.open(Native Method) 
[junit]  at java.util.zip.ZipFile.<init>(ZipFile.java:114) 
[junit]  at java.util.zip.ZipFile.<init>(ZipFile.java:131) 
[junit]  at org.apache.tools.ant.AntClassLoader.getResourceURL(AntClassLoader.java:1028) 
[junit]  at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.findNextResource(AntClassLoader.java:147) 
[junit]  at org.apache.tools.ant.AntClassLoader$ResourceEnumeration.<init> 

這裏是構建腳本:

<project name="MyProject" default="build-proj" basedir="."> 

    <property file="./build.properties" /> 

    <path id="project.classpath"> 
     <fileset dir="resources/drivers"> 
      <include name="*.*" /> 
     </fileset>  
    </path> 

    <taskdef resource="emma_ant.properties"> 
     <classpath> 
      <pathelement path="${lib.dir}/emma_ant-2.1.5320.jar" /> 
      <pathelement path="${lib.dir}/emma-2.1.5320.jar" /> 
     </classpath> 
    </taskdef> 

    <target name="build-proj"> 
     <antcall target="cleanup" /> 
     <antcall target="compile" /> 
     <antcall target="test" /> 
    </target> 

    <target name="cleanup" description="clean up"> 
     <delete dir="${build.dir}" /> 
     <delete dir="${dist.dir}" /> 
     <delete dir="${test.report.dir}" /> 
     <mkdir dir="${build.dir}" /> 
     <mkdir dir="${build.src.dir}" /> 
     <mkdir dir="${build.test.dir}" /> 
    </target> 

    <target name="compile" description="compiling all the code"> 
     <javac srcdir="${src.dir}" destdir="${build.src.dir}" debug="true"> 
      <classpath> 
       <path refid="project.classpath" /> 
      </classpath> 
     </javac> 
     <javac srcdir="${test.dir}" destdir="${build.test.dir}" debug="true"> 
      <classpath> 
       <path refid="project.classpath" /> 
       <path path="${build.src.dir}" /> 
      </classpath> 
     </javac> 
    </target> 

    <target name="test"> 
     <junit haltonfailure="false" printsummary="true" fork="true" forkmode="once" > 
      <classpath> 
       <pathelement path="${src.dir}" /> 
       <pathelement path="${build.src.dir}" /> 
       <pathelement path="${build.test.dir}" /> 
       <pathelement path="${test.data.dir}" /> 
       <path refid="project.classpath" /> 
      </classpath> 

      <formatter type="xml"/> 
      <batchtest todir="${test.report.dir}"> 
       <fileset dir="${build.test.dir}"> 
        <include name="**/*Test.class" /> 
       </fileset> 
      </batchtest> 
     </junit> 
    </target> 
</project> 
+1

我不知道如何當您不顯示ANT構建文件時期待ANT錯誤的幫助!該錯誤顯示嘗試打開一個zip文件時出現問題,但根本沒有足夠的信息繼續。 – SteveD 2011-12-28 11:40:29

+0

@SteveD對不起,我錯過了。我用buildscript更新了Q. – 2011-12-28 11:59:34

回答

5

可能你的classpath不包含o只有.jar文件。

ant任務「junit」只在類路徑元素中喜歡.jar文件(或者讓我們更好地說Path like structure,使用Ant術語)。

你可以嘗試在你的 「project.classpath」 -fileset使用的

<include name="*.jar" /> 

代替

<include name="*.*" /> 

+0

嗯,我也試過這個,我一直保留*。*,因爲我希望所有的驅動程序信息都作爲構建的一部分來運行測試。當我檢查控制檯時,它說:無法從E:\ myProject \ allResources \ drivers \ chromedriver.exe:java.util.zip.ZipException:打開zip文件時出錯。 – 2011-12-29 05:57:46

+1

啊,我看到你已經將你的Ant版本更新到1.8.x了,對吧?有[此問題](https://issues.apache.org/bugzilla/show_bug.cgi?id=47593)已修復,您現在可以看到導致錯誤的文件的名稱。這個.exe文件正是這個問題:這不是一個jar文件,不是一個zip文件,也不是一個目錄,所以它不是一個像結構一樣的Ant路徑。這就是爲什麼你會得到這個錯誤。 – Mayoares 2011-12-30 12:13:03