Selenium-TestNG-Maven - Getting “java.lang.NoClassDefFoundError: org/openqa/selenium/firefox/FirefoxDriver”

后端 未结 2 1721
逝去的感伤
逝去的感伤 2020-12-02 01:00

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.

相关标签:
2条回答
  • 2020-12-02 01:43

    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

    0 讨论(0)
  • 2020-12-02 01:44

    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:

    • Exception in thread “main” java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
    • How to solve java.lang.NoClassDefFoundError? Selenium
    • java.lang.NoClassDefFoundError: com/google/common/base/Function
    0 讨论(0)
提交回复
热议问题