Basic Java JUnit Test Setup

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

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);
  }
}

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.

Leave a Comment