Here is a sample WebDriver Java test using the JUnit framework:

[code]
package WebDriverTest1;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverTest1
{
private WebDriver driver;

@Before
public void setup() throws Exception
{
driver = new FirefoxDriver();
}

@After
public void teardown() throws Exception
{
driver.quit();
}

@Test
public void WebDriverTips()
{
driver.get(“http://www.webdrivertips.com”);
String title = driver.getTitle();
assertEquals(“Title mismatch”, “WebDriver Tips”, title);
}
}
[/code]

As we can see from my earlier C# Example, there isn’t a world of difference between the two. There is no corresponding Test Fixture attribute but there are attributes for Before, After and Test. Similar to NUnit, the Before method gets executed before every test case, and the After method gets executed after every test case. Each test case is indicated with the @Test annotation.

There are a couple of setup steps required before you can run a Java JUnit WebDriver test on your Mac. As always there are a number of different ways this can be done but I will outline one that has worked well for me. I’ll just outline the steps involved first and then go through each of them in a little more detail after:

  1. Download and install “Eclipse IDE for Java EE Developers”
  2. Download and install Java
  3. Download JUnit
  4. Download and install Apache Ant
  5. Download Selenium WebDriver
  6. Create Project and lib folders
  7. Configure the build path (in Eclipse)

(1) Download and install “Eclipse IDE for Java EE Developers”

Go to http://www.eclipse.org and click on the downloads link. From this page download and install “Eclipse IDE for Java EE Developers”
Download Eclipse for Java EE Developers

(2) Download and install Java

  • Setup $JAVAHOME
  • Setup $CLASSPATH
  • Verify your Java installation by using [code]java -version[/code]

(3) Download JUnit

Download the following JAR files from http://www.junit.org/ :

  • JUnit.jar
  • hamcrest.jar

(4) Download and install Apache Ant

Download and install Apache Ant. For further details on this please see my earlier post on Installing Ant. You can also find a link to Apache Ant page from my Resources page.
Don’t forget to verify your installation with [code]ant -version[/code]

(5) Download Selenium WebDriver

Download the following two JAR file from Selenium HQ Downloads: (see link in the Resources page.)

  • WebDriver jar file
  • Java Bindings jar file

(6) Create Project and lib folders

Eclipse uses a folder called a “workspace” in which all of its projects are located. On first opening Eclipse you will be prompted to specify a default workspace.

Inside this folder create a folder for your project. Inside this project folder create a “lib” folder and add the 4 JAR files you have downloaded earlier. Note you will have to unzip the Java Bindings zip file to get the seelnium-java JAR file.

(7) Configure the build path (in Eclipse)

Right click on the project name, and select Build Path > Configure Build Path. When the dialog is displayed, click on the button for Add External JARs.
ConfigureJavaBuildPath
From here select the 4 JAR files (hamcrest, junit, selenium-java & selenium-server-standalone) we have added to the /Workspace/Project/lib folder in the previous step.

And that concludes the basic setup required to run Java/JUnit Selenium WebDriver tests on your Mac. You are now ready to create and run a Selenium WebDriver Java test using the JUnit framework. In the next post I will create a simple test and execute it from within Eclipse and also from the Command Line/Terminal using Ant.

Is your JUnit Assert class now deprecated? Well here’s how to fix it. JUnit have moved things around, and as a result the Assert class is now in a different location as and from JUnit 4.11

But fear not, rectifying this is a simple one-liner. Simply change your import statement from [code]import junit.framework.Assert; [/code] to this: [code] import org.junit.Assert; [/code]

Simples!