2011-12-28 118 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