…with Java in Eclipse

This Tutorial will guide you through the first steps of how to code your own automated tests in Selenium using Java in Eclipse. This tutorial will also work with any other Java-Development-Environment besides Eclipse, but all screenshots and basic instructions will be for Eclipse.

Tutorial Sections:

  1. Prerequisites
  2. Download Selenium
  3. Create your first Selenium project
  4. First Selenium Test With Firefox
  5. Explanation of the code sample
  6. Cross-Browser Testing: Firefox, Chrome and Internet Explorer

Prerequisites

  • To get going you need to have Eclipse installed on your computer
  • You should be able to run a basic Hello-World-Example in Eclipse or in the java development environment of your choice.
  • The browser in which you want to test your web application with selenium should be installed on your computer. To complete this tutorial you should have at least installed Firefox and optionally Chrome and Internet Explorer.

Download Selenium

The first thing you need to to is to download the Selenium library. This library will provide you with all the classes and their functions of the Selenium WebDriver framework which are the core when coding your own web automation scripts for testing.

Download the Selenium library files here. To do that open the link, scroll down to “Selenium Client & WebDriver Language Bindings”, click on the download link of the line for “Java” and extract the downloaded zip-archive to a new seperate folder of your choice. Remember this folder because in the next section we will connect your new java project to these library files.

Create your first Selenium project

  1. Create a new Java Project in Eclipse. Give the project a neet name like “SeleniumSample” and click “Finish”.
  2. The new java project should now be visible in the project list on your left side (the “Package Explorer”). Click on the project folder you just created and then go to “Project” > “Properties”.
  3. Go to “Java Build Path” and choose the register “Libraries”. Then click the button “Add External JARs…”.
  4. Browse to the folder where you extracted the selenium library files. In the main folder select the file “selenium-java-x.xx.x.jar” (exact name depending on the version of the selenium library you downloaded.). Click “Open”.
  5. Again, click the button “Add External JARs…”, go to the “lib” folder of the downloaded folder, press Ctrl+A to select all jar-files and then click “Open” again.
  6. In the “Properties” dialog click “OK”. Your project ist ready to run Selenium scripts!

First Selenium Test With Firefox

  1. Create a new Java Class within your SeleniumSample-Project and name it “Selenium Test”.
  2. Open the new class to edit it.
  3. Overwrite every code in the class with the following code.
  4. Save the class (e.g. press Ctrl+S).
  5. Execute the class, see what happens and view the test results in the eclipse console.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 * @author Nils Schuette via frontendtest.org
 */
public class SeleniumTest {

	/**
	 * @param args
	 * @throws InterruptedException
	 */
	public static void main(String