2

NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException: I'm executing a piece of code which is giving this error. This is written in Junit on eclipse using Selenium. It works fine through eclipse but I need to share it with team on a shared repository. It has to work through command line but its failing with this error. Z:\TripPlanner\lib>java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar;Z:\TripPlanner\lib* org.junit.runner.JUnitCore "com.example.tests.TripPlannerJUnit" JUnit version 4.12 Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:398) at org.junit.internal.Classes.getClass(Classes.java:16) at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50 at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72) at org.junit.runner.JUnitCore.main(JUnitCore.java:36) Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.NoAlertPresentException

My code is as follows:

package com.example.tests;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.ChromeDriverManager;
public class TripPlannerJUnit {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
  ChromeDriverManager.getInstance().setup();
  driver = new ChromeDriver();
  baseUrl = "https://www.google.com.au/.com/";
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void testTripPlannerJUnit() throws Exception {
driver.get("https://transportnsw.info/trip#/");
driver.findElement(By.id("search-input-From")).click();
driver.findElement(By.id("search-input-From")).clear();
driver.findElement(By.id("search-input-From")).sendKeys("North Sydney Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='North Sydney Station'])[1]/following::li[1]")).click();
driver.findElement(By.id("search-input-To")).click();
driver.findElement(By.id("search-input-To")).clear();
driver.findElement(By.id("search-input-To")).sendKeys("Town Hall Station");
driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Town Hall Station'])[1]/following::ul[1]")).click();
driver.findElement(By.id("search-button")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
  fail(verificationErrorString);
    }
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}


private boolean isElementPresent(By by) {
try {
  driver.findElement(by);
  return true;
    } catch (NoSuchElementException e) {
  return false;
    }
}

public boolean isAlertPresent() {
try {
  driver.switchTo().alert();
  return true;
    } catch (NoAlertPresentException e) {
  return false;
    }
}

private String closeAlertAndGetItsText() {
try {
  Alert alert = driver.switchTo().alert();
  String alertText = alert.getText();
  if (acceptNextAlert) {
    alert.accept();
      } else {
    alert.dismiss();
      }
  return alertText;
      } finally {
  acceptNextAlert = true;
     }
   }
 }
SSharma
  • 47
  • 1
  • 9

1 Answers1

0

If it compiles and runs in Eclipse, you will have additional jar files added to your project. It will look similar to this:

enter image description here

You will need all those dependencies added to your classpath. It is possible to use wildcards like it is described in Including all the jars in a directory within the Java classpath. But you will need all the libraries of your Eclipse Java Build Path in your classpath if you are running your code standalone.

Jens Dibbern
  • 1,434
  • 2
  • 13
  • 20
  • Hello, I have shared the path to all jars using --- Z:\TripPlanner\lib>java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar;**Z:\TripPlanner\lib*** org.junit.runner.JUnitCore "com.example.tests.TripPlannerJUnit" JUnit. – SSharma Feb 10 '19 at 02:24
  • I think it should be Z:\TripPlanner\lib\* – Jens Dibbern Feb 10 '19 at 09:07
  • Hi. Yes i just mentioned like **Z:\TripPlanner\lib*** to bold that string. Anyway the issue is resolved now with making method boolean isAlertPresent as private. However, I'm getting this failure message now for which I've opened another thread. NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()? – SSharma Feb 10 '19 at 09:42