java - 运行 Selenium ant build - ClassNotFound(简单测试)

标签 java maven selenium selenium-webdriver

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

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 不会抛出任何异常并成功通过编译。

UPDATE 11/09/2019 结果我错了 destdir 它指向报告而不是 ="${bin}"。现在一切正常。

最佳答案

原来我在 <javac 中的 destdir 有误指向报告而不是 ="${bin}"的部分。现在一切正常。

关于java - 运行 Selenium ant build - ClassNotFound(简单测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57845756/

相关文章:

java - 如何在连接丢失时重新连接 jconsole

java - MySQL 拒绝以 UTF-8 格式保存

未使用的方法的 java.lang.NoSuchMethodError

scala - Spark SQL配置单元连接错误

python - 输入隐藏的文本框并按回车键(Selenium、Python)

selenium - 如何在 Selenium Chromedriver 中加载 URL 之前发送 key ?

java - Java中default关键字的作用是什么?

java - android.view.WindowManager$BadTokenException : Unable to add window -- token android. os.BinderProxy@739b5ae 无效

java - 如何返回具有 @Produces(MediaType.APPLICATION_JSON) 的响应字符串?

java - 使用 Selenium 抓取网站时出现 StaleElementReferenceException