1

Not able to configure and run Selenium tests after configuring HtmlUnit driver in my Spring Boot application.

Getting an error when I execute the test.

BaseSeleniumTests.java

public abstract class BaseSeleniumTests {

    protected static String BASE_URL = "";
    private static final String URL_PROPERTIES = "url.properties";
    protected WebDriver driver;

    @Before
    public void setUp() {
        //load test properties
        Properties p = loadAppTestProperties();
        BASE_URL = p.getProperty("base.url");
        System.out.println("BASE_URL = " + BASE_URL);

        //load driver
        this.driver = new HtmlUnitDriver();
    }

    private Properties loadAppTestProperties() {
        ClassLoader classLoader = getClass().getClassLoader();
        String filePath = classLoader.getResource(URL_PROPERTIES).getFile();
        File f = new File(filePath);
        try {
            Properties p = new Properties();
            p.load(new FileReader(f));
            return p;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @After
    public void tearDown() {
        if (driver != null) {
            driver.close();
            driver.quit();
        }
    }

}

HomePageTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class HomePageTest extends BaseSeleniumTests {

    @Test
    public void testHomePage() {
        this.driver.get(BASE_URL);
        String htmlSource = this.driver.getPageSource();
        assertTrue(htmlSource.contains("US Router"));
        assertTrue(htmlSource.contains("Welcome to JPMorgan Chase"));
    }

}

pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-htmlunit-driver</artifactId>
        <version>2.52.0</version>
        <scope>test</scope>
    </dependency>

Error:

java.lang.NoClassDefFoundError: org/openqa/selenium/remote/SessionNotFoundException

    at com.jpmchase.gct.ft.BaseSeleniumTests.loadHtmlUnitDriver(BaseSeleniumTests.java:71)
    at com.jpmchase.gct.ft.BaseSeleniumTests.setUp(BaseSeleniumTests.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.SessionNotFoundException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 31 more
Nital
  • 5,784
  • 26
  • 103
  • 195
  • You try [adding the missing dependency](https://stackoverflow.com/questions/36698363/java-lang-noclassdeffounderror-org-openqa-selenium-remote-sessionnotfoundexcept)? You've not listed a verison number on your selenium-java dependency. `mvn dependency:tree` should let you see what your dependencies are and if any are marked transitive. – Roddy of the Frozen Peas Feb 13 '19 at 22:24
  • `Spring Boot` automatically picks the latest dependency hence no need to mention the dependencies unless you want override it. In my case it pulled in `3.9.1` dependency of `chrome, firefox, ie, edge, safari, remote-driver` etc. It did NOT pull in `htmunit-driver` hence had to put the latest one available from `maven` repo explicitly. – Nital Feb 14 '19 at 17:07
  • No, Spring Boot does not automatically pick the latest dependency. It picks the dependency that's specified in the BOM (bill of materials.) So in order to know the dependency version, you need to know the version of the BOM being used, and if there are any other dependencies which override that. – Roddy of the Frozen Peas Feb 14 '19 at 18:09
  • Is the `BOM` version the `spring-boot-starter-parent` version which in my case is `2.0.6.RELEASE`? – Nital Feb 14 '19 at 18:13

0 Answers0