I'm new to Selenium and I have little knowledge of the java compilation and run process, because I always compiled and executed with the help of the IDE.
My problem
I'm trying to run a simple test with selenium (for learning purposes) to request "www.google.com" and assert that the title is the correct one.
My goal is to learn enough to use it on my work and reduce time doing manual testing on multiple browsers and OS.
My problem is that when I want to run the test from the command line it fails with a java.lang.IllegalArgumentException: Could not find class
I saw a lot of similar problems in SO but none of them helped me....
What I have done
I did the following steps:
- Follow this https://code.google.com/p/selenium/wiki/Grid2 and start hub+node
Download the chromedriver and write this simple test class
import junit.framework.TestCase; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import java.io.File; import java.io.IOException; @RunWith(JUnit4.class) public class ChromeTest extends TestCase { private static ChromeDriverService service; private WebDriver driver; @BeforeClass public static void createAndStartService() { service = new ChromeDriverService.Builder() .usingDriverExecutable(new File("tools/chromedriver")) .usingAnyFreePort() .build(); try { service.start(); } catch (IOException e) { e.printStackTrace(); } } @AfterClass public static void createAndStopService() { service.stop(); } @Before public void createDriver() { driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome()); } @After public void quitDriver() { driver.quit(); } @Test public void testTitleDevel() { driver.get("http://www.google.com"); TestCase.assertEquals("Google", driver.getTitle()); } }Use maven and create a pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Test</groupId> <artifactId>Test</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.46.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>12.0</version> </dependency> </dependencies> <build> <sourceDirectory>/Users/user1/QAScripts/AmateurTest/src/</sourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> </plugin> </plugins> </build> </project>
I have now the following directory structure:
Test
|--pom.xml
|--src
|------ChromeTest.java
|--tools
|------chromedriver
|------selenium-server-standalone-2.46.0.jar
- Run
mvn dependency:build-classpath - Use that classpath to compile ChromeTest.java
javac -cp "..HERE GOES CLASSPATH.." src/ChromeTest.java
user1@tororrosso ~/T/src> ls -la
total 16
drwxrwxrwx 4 user1 staff 136 Jun 12 16:43 ./
drwxrwxrwx 8 user1 staff 272 Jun 12 16:41 ../
-rw-r--r-- 1 user1 staff 2316 Jun 12 16:44 ChromeTest.class
-rwxrwxrwx 1 user1 staff 1372 Jun 12 16:43 ChromeTest.java*
- Use the same classpath to run ChromeTest.class
java -cp "..HERE GOES CLASSPATH.." org.junit.runner.JUnitCore src/ChromeTest
But FAILS!!
JUnit version 4.12
.E
Time: 0,002
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [src/ChromeTest]
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
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: src/ChromeTest
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at org.junit.internal.Classes.getClass(Classes.java:16)
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
... 4 more
FAILURES!!!
Tests run: 1, Failures: 1
Tried with
java -cp "..HERE GOES CLASSPATH.." org.junit.runner.JUnitCore src/ChromeTest.class
But it fails too
JUnit version 4.12
.E
Time: 0,002
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [src/ChromeTest.class]
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
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: src/ChromeTest.class
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at org.junit.internal.Classes.getClass(Classes.java:16)
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
... 4 more
FAILURES!!!
Tests run: 1, Failures: 1
Edit:
Tried too:
user1@tororrosso ~/T/> cd src
user1@tororrosso ~/T/src> javac -cp "....." ChromeTest.java
user1@tororrosso ~/T/src> java -cp "....." "ChromeTest"
JUnit version 4.12
.E
Time: 0,001
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [ChromeTest]
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
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: ChromeTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at org.junit.internal.Classes.getClass(Classes.java:16)
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
... 4 more
FAILURES!!!
Tests run: 1, Failures: 1
user1@tororrosso ~/T/src> java -cp "....." 'ChromeTest'
JUnit version 4.12
.E
Time: 0,001
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [ChromeTest]
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
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: ChromeTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at org.junit.internal.Classes.getClass(Classes.java:16)
at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
... 4 more
FAILURES!!!
Tests run: 1, Failures: 1
Edit2: My classpath
Maybe there is a problem with my classpath. But is what maven uses....
/Users/user1/.m2/repository/cglib/cglib-nodep/2.1_3/cglib-nodep-2.1_3.jar:/Users/user1/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/user1/.m2/repository/com/google/code/gson/gson/2.3.1/gson-2.3.1.jar:/Users/user1/.m2/repository/com/google/guava/guava/12.0/guava-12.0.jar:/Users/user1/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar:/Users/user1/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar:/Users/user1/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/user1/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Users/user1/.m2/repository/io/netty/netty/3.5.2.Final/netty-3.5.2.Final.jar:/Users/user1/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/user1/.m2/repository/net/java/dev/jna/jna/4.1.0/jna-4.1.0.jar:/Users/user1/.m2/repository/net/java/dev/jna/jna-platform/4.1.0/jna-platform-4.1.0.jar:/Users/user1/.m2/repository/net/sourceforge/cssparser/cssparser/0.9.16/cssparser-0.9.16.jar:/Users/user1/.m2/repository/net/sourceforge/htmlunit/htmlunit/2.17/htmlunit-2.17.jar:/Users/user1/.m2/repository/net/sourceforge/htmlunit/htmlunit-core-js/2.17/htmlunit-core-js-2.17.jar:/Users/user1/.m2/repository/net/sourceforge/nekohtml/nekohtml/1.9.22/nekohtml-1.9.22.jar:/Users/user1/.m2/repository/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar:/Users/user1/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar:/Users/user1/.m2/repository/org/apache/httpcomponents/httpclient/4.4.1/httpclient-4.4.1.jar:/Users/user1/.m2/repository/org/apache/httpcomponents/httpcore/4.4.1/httpcore-4.4.1.jar:/Users/user1/.m2/repository/org/apache/httpcomponents/httpmime/4.4.1/httpmime-4.4.1.jar:/Users/user1/.m2/repository/org/eclipse/jetty/jetty-io/9.2.11.v20150529/jetty-io-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/jetty-util/9.2.11.v20150529/jetty-util-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/websocket/websocket-api/9.2.11.v20150529/websocket-api-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/websocket/websocket-client/9.2.11.v20150529/websocket-client-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/websocket/websocket-common/9.2.11.v20150529/websocket-common-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-api/2.46.0/selenium-api-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-chrome-driver/2.46.0/selenium-chrome-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-firefox-driver/2.46.0/selenium-firefox-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-htmlunit-driver/2.46.0/selenium-htmlunit-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-ie-driver/2.46.0/selenium-ie-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-java/2.46.0/selenium-java-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-leg-rc/2.46.0/selenium-leg-rc-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-remote-driver/2.46.0/selenium-remote-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-safari-driver/2.46.0/selenium-safari-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-support/2.46.0/selenium-support-2.46.0.jar:/Users/user1/.m2/repository/org/w3c/css/sac/1.3/sac-1.3.jar:/Users/user1/.m2/repository/org/webbitserver/webbit/0.4.14/webbit-0.4.14.jar:/Users/user1/.m2/repository/xalan/serializer/2.7.2/serializer-2.7.2.jar:/Users/user1/.m2/repository/xalan/xalan/2.7.2/xalan-2.7.2.jar:/Users/user1/.m2/repository/xerces/xercesImpl/2.11.0/xercesImpl-2.11.0.jar:/Users/user1/.m2/repository/xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar