Its quite easy to run Selenium tests using Eclipse and JUnit. Basically all you need is to get Selenium RC:

http://seleniumhq.org/download/

and get Eclipse (Eclipse Classic will do if you don’t need the rest, the Helios 3.6 already includes JUnit)

http://www.eclipse.org/downloads/

to make things easier also get Selenium IDE (Firefox plugin for recoding tests and exporting to the desired language)

http://seleniumhq.org/download/

Install Selenium IDE the rest just unpack to a desired dir and you are ready to roll. If you already have Eclipse its most likely

that you have JUnit as well but just in case you need it it can be found here:

http://www.junit.org/

Record some test case in Selenium IDE by opening up FireFox and going to Tools->Selenium IDE

For example open up blog.wedoqa.com and assert some text (or do whatever test you desire)

While Selenium IDE is running you can easily assert things by right clicking (except on web pages that

have their own right click handling) and looking at the bottom commands on the popup all those come from

Selenium IDE.

After you have the desired test go to Options->Format and pick Java(JUnit) format and you’ll get the test formated for Java and JUnit

and will look something like this:

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class Untitled extends SeleneseTestCase {
	public void setUp() throws Exception {
		setUp("http://66.147.244.161/~wedoqaco/blog_wedoqa/", "*chrome");
	}
	public void testUntitled() throws Exception {
		selenium.open("/");
		assertTrue(selenium.isTextPresent("Because quality matters!"));
	}
}

Now start up Eclipse and create a new Java project and add in the selenium-java-client-driver.jar from selenium-remote-control-1.0.3selenium-java-client-driver-1.0.1

(note versions might be different but you get the idea of its location), you can choose to migrate the jar will make it easier if you give the project to someone etc. At this point

you can add the JUnit jar as well or let Eclipse auto add it when you create a JUnit Test Case.

Now add a JUnit Test Case to the project (in package explorer right click the desired location and pick new-> JUnit Test Case:

Copy in the code that the Selenium IDE generated. You’ll notice some errors fix them (the name of the class should be the same as the file that was just create

also fix the package or just remove it)

Now its time to run the test. Start up the Selenium server from console using java -jar selenium-server.jar or create a neat bat file that will do this for you every time

something like:

cd E:Seleniumselenium-remote-control-1.0.3selenium-server-1.0.3
E:
java -jar selenium-server.jar

After the Server is started it should look something like this:
Now run your test from Eclipse and watch the thing come to life 🙂

Happy testing! 🙂

If you prefer a hassle free and cloud based solution and an easy way to increase the comprehensiveness of Selenium tests I suggesting giving Saucelabs a try. They offer a popular service that gives developers instant access to over 300 browser/OS combinations. They have a free plan for trying things out also they provide unlimited free accounts to open source projects. http://saucelabs.com/selenium

Similar Posts from the author:

5 thoughts to “Selenium test in Java with Eclipse and JUnit

  • Tom L Mcdavid

    Well done. Thanks for the great post. Bookmarked

  • Nonni

    nice tutorial sir…..

  • Amit

    Thank you .

    Where can I find more examples on selenium so that I can learn more .

  • kamal

    What type of output will give this test case.
    i have just run the data in web browser. i am testing on web service post type data that display on the browser

  • kamal

    $(function(){ window.location.href=’http://www.yahoo.com’; });

Comments are closed.