Not able to run test through ANT

2019-05-28 22:47发布

I have a test class, all the tests of this class runs fine if I run in eclipse. (right click on the file , run as jUnit) but when I am trying to run using Ant Script it fails.

Actually this test is to be run against 2 browsers. This test runs fine against IE Chrome. It runs fine against IE. But test is not able to run against Chrome. I am not sure what's happening. All the driver related information I have placed under allresources/drivers/ of the project path. And browser profiles i have kept under browserProfiles folder of the project.

I am not sure why tests are not able to be picked up for Chrome.

I have attached test code and the code where I am trying to create webdriver and seldriver in the source code.

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..
}
}

And source code

    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();
 }
 }

The stack trace which I am getting while running the ANT build is :

[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>

Here is the build script :

<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条回答
疯言疯语
2楼-- · 2019-05-28 23:28

Probably your classpath contains not only .jar files.

The ant task "junit" only likes .jar files (or let's better say Path like structure, to use an Ant term) in the classpath element.

You could try to use

<include name="*.jar" />

instead of

<include name="*.*" />

in your "project.classpath"-fileset.

查看更多
登录 后发表回答