【问题标题】:Running Selenium ant build - ClassNotFound (simple test)运行 Selenium ant build - ClassNotFound(简单测试)
【发布时间】:2020-01-10 17:36:03
【问题描述】:

尝试运行本书中的第一个测试:Selenium Testing Tools Cookbook,但是当我在项目根文件夹内的 CLI 中键入 ant 时,我得到了一个 ClassNotFound 用于第一个简单测试。

pl.divix.selenium.chapter01.GoogleSearchTest

java.lang.ClassNotFoundException: pl.divix.selenium.chapter01.GoogleSearchTest
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:374)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

build.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project name="tests" default="exec" basedir=".">
    <property name="src" value="./src" />
    <property name="lib" value="./lib" />
    <property name="bin" value="./bin" />
    <property name="report" value="./report" />

    <path id="test.classpath">
        <pathelement location="${bin}"/>
        <fileset dir="${lib}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <target name="init">
        <delete dir="${bin}" />
        <mkdir dir="${bin}" />
    </target>

    <target name="compile" depends="init">
        <javac source="1.8" srcdir="${src}" fork="true" destdir="${report}">
            <classpath>
                <pathelement path="${bin}"/>
                <fileset dir="${lib}">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>
        </javac>
    </target>

    <target name="exec" depends="compile">
        <delete dir="${report}"></delete>
        <mkdir dir="${report}"/>
        <mkdir dir="${report}/xml"/>

        <junit printsummary="true" haltonfailure="no">
            <classpath>
                <pathelement path="${bin}"/>
                <fileset dir="${lib}">
                    <include name="**/*.jar"/>
                </fileset>
            </classpath>

            <test name="pl.divix.selenium.chapter01.GoogleSearchTest" haltonfailure="no" todir="${report}/xml" outfile="TEST-result">
                <formatter type="xml" />
            </test>
        </junit>

        <junitreport todir="${report}">
            <fileset dir="${report}/xml">
                <include name="TEST*.xml"/>
            </fileset>
            <report format="frames" todir="${report}/html"/>
        </junitreport>
    </target>

    <!--<manifest>
        <attribute name="GoogleSearchTest" value="pl.divix.selenium.chapter01"/>
    </manifest>!-->


</project>

GoogleSearchTest.java

package pl.divix.selenium.chapter01;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.*;

import static org.junit.Assert.*;

public class GoogleSearchTest {
    private WebDriver driver;

    @Before
    public void setUp() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.google.com");
    }

    @Test
    public void testGoogleSearch() {
        WebElement element = driver.findElement(By.name("q"));
        element.clear();
        element.sendKeys("Selenium testing...");
        element.submit();

        new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("selenium testing...");
            }
        });

        assertEquals("Selenium testing... - Szukaj w Google", driver.getTitle());
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>pl.divix.selenium</groupId>
  <artifactId>SeleniumCookbook</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.14.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
  </dependencies>
  <properties><maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target></properties>
</project>

运行mvn clean test 不会抛出任何异常并成功通过编译。

更新 11/09/2019 原来我错了 destdir 指向报告而不是 ="${bin}"。现在按预期工作。

【问题讨论】:

  • 您的“lib”目录中是否定义了带有 GoogleSearchTest 类的 JAR 文件?根据您的脚本,这就是 Ant 寻找它的地方。
  • @nickolay.laptev 不,我没有,我怎样才能触发它出现在那里?

标签: java maven selenium selenium-webdriver


【解决方案1】:

原来我在&lt;javac 部分中有错误的destdir,它指向report 而不是="${bin}"。现在按预期工作。

【讨论】:

    猜你喜欢
    • 2012-03-23
    • 2017-01-19
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多