Running Selenium ant build - ClassNotFound (simple test)

divix :

Trying to run the first test from the book: Selenium Testing Tools Cookbook, however I am getting a ClassNotFound for the first simple test when I type ant in the CLI inside my project root folder.

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>

Running mvn clean test doesn't throw any exception and successfully passes compilation.

UPDATE 11/09/2019 It turned out I had wrong destdir which was pointing to report instead of ="${bin}". This is now working as expecting.

divix :

It turned out I had wrong destdir in <javac section which was pointing to report instead of ="${bin}". This is now working as expecting.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=139854&siteId=1