3
votes

This is my first selenium script using TestNG and Maven. Created a simple "Hello World" code and a selenium test code which just checks the title of google page.

Selenium Code Below with TestNG:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HelloTest {
    @Test
    public void testOne() {
        //WebDriver d=new FirefoxDriver();
        System.setProperty("webdriver.gecko.driver","D:\\Firefox Driver\\geckodriver-v0.17.0-win64\\geckodriver.exe");
        WebDriver d=new FirefoxDriver();
        d.get("https://www.google.com");
        System.out.println("This is first TestNG");

    }
}

This is working absolutely fine when run through eclipse - Run As - Test NG test.

But when ran through Maven - mvn clean install from cmd prompt, i am getting below error

T E S T S
-------------------------------------------------------

Running HelloTest
Configuring TestNG with: TestNG652Configurator
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.94 sec <<< FAILURE! - in HelloTest
testOne(HelloTest)  Time elapsed: 0.032 sec  <<< FAILURE!
java.lang.NoClassDefFoundError: org/openqa/selenium/firefox/FirefoxDriver
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at HelloTest.testOne(HelloTest.java:11)

It is showing error at WebDriver d=new FirefoxDriver();. Not sure where the issue is. Added all jar files , checked the build path and all the jar were there. Below is my POM file.

<?xml version="1.0"?>
<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>com.demo.micky</groupId>
    <artifactId>MavenDemoTwo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.12.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
            <type>jar</type>
            <version>15.0</version>
        </dependency>
    </dependencies>
</project>

Any help is deeply appreciated.

2
selenium-firefox-driver is included in selenium-java. See the graphic on this page for additional info: docs.seleniumhq.org/download/maven.jspSiKing
you shouldn't need to set your gecko driver path. take it out.Simon N
I tried taking gecko driver path but its not allowing as its a synta mistake.Ramdas Metla

2 Answers

1
votes

What is NoClassDefFoundError

NoClassDefFoundError in Java occurs when JVM is not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a Class or accessing any static member of a Class and that Class is not available during runtime then JVM will throw NoClassDefFoundError.

The error you are seeing is :

java.lang.NoClassDefFoundError: org/openqa/selenium/firefox/FirefoxDriver

This clearly indicates that Selenium is trying to resolve the particular FirefoxDriver Class at runtime from org/openqa/selenium/firefox/FirefoxDriver which is not available.

What went wrong :

This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK/Maven/Gradle.

From the pom.xml it is pretty clear that you have added multiple dependencies for FirefoxDriver Class as follows:

  • <artifactId>selenium-java</artifactId>:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.12.0</version>
        <scope>test</scope>
    </dependency>
    
  • <artifactId>selenium-firefox-driver</artifactId>:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.12.0</version>
    </dependency>
    
  • <artifactId>selenium-server</artifactId>:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.12.0</version>
    </dependency>
    
  • Additionally you have also added all jar files.

From all the above mentioned points it's clear that the related Class or Methods were resolved from one source at Compile Time which was not available during Run Time.

Solution :

Here are a few steps to solve NoClassDefFoundError :

  • While using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download all the dependencies mentioned in the configuration file (e.g. pom.xml) to resolve the Classes and Methods.
  • If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused and duplicate External JARs.
  • If you are using FirefoxDriver use either of the <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
  • Remove the unwanted and duplicated from pom.xml
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • While you execute a Maven Project always perform the folling in sequence:

    • maven clean
    • maven install
    • maven test

You can find related discussions in:

0
votes

I was using eclipse 09-2019 which is the latest one with latest JDK installed 13, and latest selenium jar files 3.141.59, I installed other JDKs to work around to solve this issue after trying all answers on similar question. Then after 4 days trying, installed eclipse neon version(https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/neon/3/eclipse-java-neon-3-win32-x86_64.zip&mirror_id=105) and used Selenium-Java 3.5.2 jar files (https://jar-download.com/artifacts/org.seleniumhq.selenium/selenium-java/3.5.2/source-code) and now it is working perfectly Alhamdullilah. Also I don't know what was the errors root cause exactly or at all, but now it is solved. Wish if that help You