Delivered by FeedBurner

How to Use JUnit Annotations in webdriver test case with example

Unit testing framework JUnit has many annotations to control the flow and activity of code execution of selenium webdriver software test cases. You must need to insert JUnit annotation inside the java code to execute your software test case as junit test case. You can look in my previous post where i have used JUnit @Test annotation before software test method. Let me describe you mostly used 3 JUnit Annotations with example. You can view more details on JUnit at

1. @Before
2. @Test
3. @After

2 other frequently used annotations are @BeforeClass and @AfterClass.

Depending on annotation names, JUnit framework will decide the code execution flow. i.e. First JUnit framework will execute @Before method, Second it will execute @Test method and at last it will execute @After method.

1. @Before Annotation 
As name suggest. method written under @Before annotation will be executed before the method written under @Test annotation. Based on @Before annotation, JUnit framework will execute that before method first. Generally method under @Before annotation is used to initializing software website and other environment related setup. @Before annotation method will be executed before each @Test annotation method means if there are two @Test methods in your class then @Before method will be executed two times.

2. @After
Method under @After annotation is known as after method and execution of @After method will start as soon as completion of @Test method execution completion. Same as @Before , @After annotation method will be executed two times if there are two @Test methods in your class.

3. @Test
Method under @Test annotation is known as test method. Execution of @Test method will start as soon as @Before method execution completed. Generally we are writing all testing related activity under @Test. We can use multiple @Test methods in single class too.

Video on usage of JUnit @Before @After and @Test annotations

4. @BeforeClass
Methods under @BeforeClass annotation will be executed before the any software test method starts execution. It will be executed only once even if there are multiple @Test methods in your class.

5. @AfterClass
@AfterClass method will be run on completion of all the test method's execution from that class. Same as  @BeforeClass, @AfterClass method will be executed once only.
Execute bellow given example in your eclipse as a debugging mode to verify its execution.

package Testjunit;

import java.util.concurrent.TimeUnit;

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

public class Mytest {
 WebDriver driver = new FirefoxDriver();
 public void beforetest() {
 public void aftertest() {
 public void test() {
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  boolean str1 = driver.findElement(By.xpath("//input[@value='Bike']")).isSelected();
  if(str1 = true) {
   System.out.print("Checkbox is checked");
   System.out.print("Checkbox is not checked");

Here @After method is written before @Test method but JUnit Framework will tells eclipse to execute @Test method first and then @After method


  1. Hi,

    I didn't get check box with checked sign in Firefox browser when i run the program.

  2. put one more = in this line if(str1 = true) as if(str1 == true)

    1. This comment has been removed by the author.

  3. no str1!=str2 this will work. try this

  4. Beautiful Explanations! thank you so much

  5. @RunWith(OrderedRunner.class)
    public class StopStartKllProcessesHostTest extends ModuleTestBase{
    // private static WebDriver driver;
    static WebDriver driver = ModuleTestBase.driver();
    private static String baseUrl;
    private boolean acceptNextAlert = true;
    WaitForPageLoading WaitForPageLoading=new WaitForPageLoading();
    private static StringBuffer verificationErrors = new StringBuffer();
    DataAccessBase DataAccessBase=new DataAccessBase();
    DataAndAlertViewPage DataAndAlertViewPage=new DataAndAlertViewPage();
    Properties properties = ApplicationUtil.loadPropFileForMP();

    public static void setUp() throws Exception {

    // baseUrl = "";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    public void loginPageTest() throws Exception {


    public void expendTreeViewTest() throws Exception
    // DataAndAlertViewPage.expendTree();

    some of the Examples of Junit annotations

    Test Automation