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 http://junit.org/.

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 = null;
 
 @Before
 public void beforetest() {
  // set geckodriver path.
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  //initialize firefox driver.
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html");
 }
 
 @After
 public void aftertest() {
  driver.quit();
  
 }
 
 @Test
 public void test() {
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  driver.findElement(By.xpath("//input[@value='Bike']")).click();
  boolean str1 = driver.findElement(By.xpath("//input[@value='Bike']")).isSelected();
  if(str1 = true) {
   System.out.print("Checkbox is checked");
  }
  else
  {
   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

9 comments:

  1. Hi,

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

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

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

    ReplyDelete
  4. Beautiful Explanations! thank you so much

    ReplyDelete
  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();

    @BeforeClass
    public static void setUp() throws Exception {

    // baseUrl = "http://slc05ezg.us.oracle.com:5522/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }


    @Test
    @Order(order=1)
    public void loginPageTest() throws Exception {
    ApplicationUtil.loginPage();

    }


    @Test
    @Order(order=2)
    public void expendTreeViewTest() throws Exception
    {
    // DataAndAlertViewPage.expendTree();

    }
    some of the Examples of Junit annotations

    Test Automation

    ReplyDelete
  6. Please correct below line :
    if(str1 = true)
    as

    if(str1 == true)

    ReplyDelete