1
votes

I am trying to run very simple Java automation test case using PhantomJS. I am using following version:

  1. selenium-server-standalone3.8.1.jar
  2. PhantomJS 2.1.1 version for Windows
  3. PhantomJSdriver.jar 1.1. version

My code desired capabilities and driver launch is as follow:

        DesiredCapabilities caps = new DesiredCapabilities();
        ((DesiredCapabilities) caps).setJavascriptEnabled(true);
        ((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
        ((DesiredCapabilities) caps).setCapability(
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                "C:/Program Files/phantomjs-2.1.1-windows/bin/phantomjs.exe"   //java web start / jnpl file...
                // "/Controller/phantomjs.exe"
        );

        //SET enabled javascript for php script on WEB page to transform it to picture:
        caps.setJavascriptEnabled(true);
        String [] phantomJsArgs = {"--web-security=no", "--ignore-ssl-errors=yes"};
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);

        //CREATING WEB driver
        WebDriver driver = new PhantomJSDriver(caps);

However, I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/browserlaunchers/Proxies at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:178) at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:99) at HD_PhantomJSTest1.main(HD_PhantomJSTest1.java:33) Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 3 more

1
How are you doing the build? It looks like some libraries are not found on the relative path. Make you sure you import all your dependencies or use Maven to take care of that. - Eugene S

1 Answers

0
votes

java.lang.NoClassDefFoundError

java.lang.NoClassDefFoundError is observed when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time.

As an example, if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw java.lang.NoClassDefFoundError.

From your Test Bed information, I am not sure about the inclusion of PhantomJSdriver.jar 1.1. version. Current implementation of PhantomJS doesn't need any additional jar as such. Relevant Selenium JARs resolves the required dependencies. So you can remove the PhantomJSdriver.jar 1.1. version.

Other than that, I don't see any such error in your code. However after a bit of code formatting I executed your code with my configuration and was SUCCESS as follows:

@Test
public void verifyFacebookTitle()
{
    DesiredCapabilities caps = new DesiredCapabilities();
    ((DesiredCapabilities) caps).setJavascriptEnabled(true);
    ((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
    ((DesiredCapabilities) caps).setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
    caps.setJavascriptEnabled(true);
    String [] phantomJsArgs = {"--web-security=no", "--ignore-ssl-errors=yes"};
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);
    WebDriver driver = new PhantomJSDriver(caps);
    driver.get("https://www.facebook.com/");
    System.out.println(driver.getTitle());
    driver.quit();
}

Output on my console :

INFO: Detected dialect: OSS
Facebook – log in or sign up
[INFO  - 2017-12-06T08:24:22.972Z] ShutdownReqHand - _handle - About to shutdown
PASSED: verifyFacebookTitle

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================