Maarten van Heek
Maarten van Heek

Reputation: 45

Able to run JUnitCore test runner from IntelliJ run config, but not form commandline

I am writing a program to automate a visitor sign up, using selenide (4.4.x) and junit (4.12). The tests run fine if I run them as a JUnit test in the IDE, but for a more convenient usage I would like to run it from main/commandline.

I managed to get this to work with an IntelliJ run configuration, but not when I attempt the same thing from the commandline. Basically, I have one abstract class that starts and stops the Selenium/ide WebDriver, a concrete test class (RegisterVisitorTest.java) that contains the actual reservation logic, and RunTest.java with a main method. See below for an MWE (cut irrelevant code, so it may not be executable as such).

If I create an IntelliJ run configuration with:

-Dselenide.browser=chrome -Dwebdriver.chrome.driver="C:\downloads\chromedriver.exe" -DlastName="Peeters" -DfirstDay="5-5-2017"

as VM options, it then executes perfectly well and prints both "in RunTest.main", "RegisterVisitorTest", and it fills the form for me. However, if I create a jar using mvn install and run it as

java -Dselenide.browser=chrome -Dwebdriver.chrome.driver="C:\downloads\chromedriver.exe" -DlastName="Peeters" -DfirstDay="5-5-2017" -cp %junit_path%;target\name-of-jar.jar x.selenide.RunTest

where `%junit_path% contains a reference to junit.jar and hamcrest.jar, it does go into main and prints "in RunTest.main", but it does not actually run the tests. The good news is that I don't get errors either.

However, when I run it directly as a JUnitCore runner from the commandline with:

java -Dselenide.browser=chrome -Dwebdriver.chrome.driver="C:\downloads\chromedriver.exe" -DlastName="Peeters" -DfirstDay="5-5-2017" -cp %junit_path%;target\name-of-jar.jar org.junit.runner.JUnitCore nl.ing.selenide.RegisterVisitorTest

I get the following output:

JUnit version 4.12
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/support/events/WebDriverEventListener
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        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.support.events.WebDriverEventListener
        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)
        ... 8 more

What bothers me is that it runs fine when I run it from the IDE, but that it does not fire the test when I run main from commandline, and that it fails when I run the test directly. In my Maven project I do have a number of red lines but that does not seem to matter most of the time...

Anyway this seems to boil down to that WebDriverEventListener mentioned above, but if I attempt to add a WebDriverEventListener it can apparently find the dependency and automatically adds the correct import statement, though this does not change the outcome if I do another mvn install.

Am I missing something?

EDIT: Although 'class not found' is found in other SO questions, it is not a duplicate of the hadoop question mentioned, as I have the correct environment variables set.

I am able to run other JARs, just not this one.

Solved by using the Maven assembly plugin to include all dependencies.

MWE (attempt):

package x.selenide;
//RunTest.java
import org.junit.runner.JUnitCore;

public class RunTest {
    public static void main(String[] args) {
        System.out.println("In RunTest.main");
        JUnitCore junit = new JUnitCore();
        junit.run(RegisterVisitorTest.class);
    }
}

//RegisterVisitorTest.java
public class RegisterVisitorTest extends ClickTest {

    private static String lastName;
    private static LocalDate firstDay;
    private static LocalDate lastDay;

    private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("d-M-yyyy");

    public RegisterVisitorTest() {
        System.out.println("RegisterVisitorTest");
    }

    @BeforeClass
    public static void setUp() {
        // setup properties with System.getProperties();
    }

    @Test
    public void openRegistrationPage(){
        Selenide.$(Selectors.byText("Bezoekers aanmelden")).click();
        String parentWindowHandle = WebDriverRunner.getWebDriver().getWindowHandle();

        // switch tab/window as it opens a new window
        Set<String> handles = WebDriverRunner.getWebDriver().getWindowHandles();
        for (String handle: handles){
            if(!handle.equals(parentWindowHandle)){
                Selenide.switchTo().window(handle);
            }
        }

        // method call to fill the actual registration form
    }
}
// ClickTest.java
public abstract class ClickTest {
    @BeforeClass
    public static void openOrderSite() {
        Configuration.timeout = 10000;
        Configuration.baseUrl = "https://intranet.net";
        Configuration.startMaximized = false;
        Selenide.open("/subdomain");
        waitUntilPageIsLoaded();
    }

    private static void waitUntilPageIsLoaded() {
        waitUntilPageIsLoaded("Bezoekers aanmelden");
    }

    static void waitUntilPageIsLoaded(String expected){
        logger.info(String.format("Waiting for string '%s' to appear...", expected));
        Selenide.$(Selectors.byText(expected)).waitUntil(Condition.appears, 20000);
        logger.info("Page loaded");
    }

    @AfterClass
    public static void logout() {
        WebDriverRunner.closeWebDriver();
    }
}

Upvotes: 2

Views: 472

Answers (1)

GhostCat
GhostCat

Reputation: 140447

This exception is pretty straight forward: the classpath is missing something. java fails to locate that org/openqa/selenium... class.

And all your setup mentions junit, hamcrest, ... but not selenium.

Long story short: probably your IDE adds a selenium jar to the classpath without you noticing. But when you run things on the command line, you need to provide all the elements that are needed. Selenium is missing. And probably your own classes as well.

Upvotes: 1

Related Questions