Monday, July 22, 2013

Using Selenium in a Java Dynamic Web Project

The issue with selenium is that you can use the selenium-server-standalone-2.33.0.jar in a simple java project (e.g., console)
as the only jar and everything will work fine,
but when you try to move your code to a java dynamic web project (e.g., JavaEE / Tomcat) it will give you hell.
specifically it will give you the following error messages:

java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver


Now I went through many hours trying to understand if my problem was in importing the lib to the correct place
(WEB-INF/lib) or that the "java build path" libraries tab is correct. Also checked "deployment assembly".

All this didn't help me, so I went down to basics, i opened the "java resources"->libraries->"web app libraries"->"selenium-server-standalone-2.33.0.jar"
and looked for org.openqa.selenium.WebDriver, and didn't find it !

So then i thought, well this probably is not such a standalone jar at all.
The solution - don't use the standalone selenium jar, instead use the small non-standalone selenium jar (selenium-java-2.33.0.jar or selenium-server-2.33.0)
and also use all the jars it depends on.

Assuming you are going for selenium-java-2.33.0.jar (which is for local running VS selenium-java-2.33.0.jar which allow local and remote running)
You download the full zip from http://docs.seleniumhq.org/download/ under "Selenium Client & WebDriver Language Bindings" java-> 'download' link.
At the time of writing this article it was http://selenium.googlecode.com/files/selenium-java-2.33.0.zip

Open the zip and copy selenium-java-2.33.0.jar and all the libs in the libs dir into your java dynamic web project's WebContent->WEB-INF->lib dir.
(no need to change project build path or anything this dir should be automatically in there).

Hope I saved many hours of work from other people in this post !