Example Of Difference Between @Before/@After VS @BeforeClass/@AfterClass In JUnit With WebDriver

Many readers are asking me the difference between JUnit annotations @Before VS @BeforeClass and @After VS @AfterClass in webdriver software automation test. If you have read my JUNIT ANNOTATIONS POST, I have clearly described difference between @Before and @BeforeClass annotations and @After and @AfterClass annotations in bold text. Now let me describe once more and then we will look at practical example for both of them to use them in software automation test.

Difference between @Before and @BeforeClass annotations
  • Test method marked with @Before annotation will be executed before the each @Test method. Means if there are 5 @Test methods in your class then @Before software test method will be executed 5 times.
  • Test method marked with @BeforeClass annotation will be executed just before the class. Means @BeforeClass method will be executed only once before the class even if there are 5 @Test methods in your class of software test case.
Difference between @After and @AfterClass annotations
  • Same as @Before annotation, Test method marked with @After annotation will be executed after the each @Test method.
  • @AfterClass annotation will be executed only once after last @Test method executed.
Execute bellow given @Before and @After annotations example in your eclipse and observe result in console.
package junitpack;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class junittest2 {
 private static WebDriver driver = null;
  
@Before
public void openbrowser() {
 System.out.print("\nBrowser open");
 // 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 closebrowser() {
 System.out.print("\nBrowser close");
 driver.quit();
}
 
 @Test
 public void test1() throws InterruptedException{  
 driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test1");
 System.out.print("\njunittest2 class-test1 method is executed");
 Thread.sleep(2000);
 }

 @Test
 public void test2() throws InterruptedException {
 driver.findElement(By.xpath("//input[@name='fname']")).clear();
 driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test2");
 Thread.sleep(2000);
 System.out.print("\njunittest2 class-test2 method is executed");
 }
}
When you execute above example in eclipse, Bellow given result will be displayed in console.
Console Output :
Browser open
junittest2 class-test1 method is executed
Browser close
Browser open
junittest2 class-test2 method is executed
Browser close

@Before/@After VS @BeforeClass/@AfterClass


Based on above given console output, we can say each @Before method is executed before each @Test method and each @After method is executed after each @Test method. Now let we replace @Before annotation with @BeforeClass and @After annotation with @AfterClass in same example and then observe result. Replace bellow given @BeforeClass and @AfterClass part with @Before and @After part in above example as bellow.

@BeforeClass
public static void openbrowser() {
 System.out.print("\nBrowser open");
 // 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");
}

@AfterClass
public static void closebrowser() {
 System.out.print("\nBrowser close");
 driver.quit();
}

Now run above example and look at console result. Console result will be as bellow.
Console Output :
Browser open
junittest2 class-test1 method is executed
junittest2 class-test2 method is executed
Browser close

As per console result, we can say @BeforeClass method is executed once only. Same way @AfterClass annotation marked method is also executed once only. This way you can use different annotations of testng in your software automation test.

7 comments:

  1. Very clear..thank you

    ReplyDelete
  2. can we run our test scripts in orderly in Junit if we run more number of test scripts?

    ReplyDelete
  3. My sendKeys could not be identified. Giving the error The method sendKeys(CharSequence...) from the type WebElement refers to the missing type CharSequence... please help .

    ReplyDelete
  4. if suppose we have 3 @Test annotations, then is there a order in which the method will be executed ?

    ReplyDelete
    Replies
    1. You can set priority. Ref http://software-testing-tutorials-automation.blogspot.in/2014/11/set-priority-for-selenium-webdriver.html

      Delete
  5. If we have used both @BeforeClass and @Before ...which will execute first??

    ReplyDelete
  6. Wanted to know why i get the below result
    Browser Open
    junittest2 class-test2 method is executed
    Browser Close
    Jun 24, 2015 3:43:50 AM org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
    INFO: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@123d8f4
    Browser Open
    junittest2 class-test1 method is executed
    Browser Close
    Jun 24, 2015 3:44. Here test 2 is getting executed first unable to understand the reason.

    ReplyDelete