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

Many readers are quest me the difference betwixt JUnit annotations @Before VS @BeforeClass and @After VS @AfterClass inward webdriver software automation test. If y'all bring read my JUNIT ANNOTATIONS POST, I bring clearly described difference betwixt @Before together with @BeforeClass annotations together with @After and @AfterClass annotations inward bold text. Now allow me depict i time again together with thence nosotros volition expect at practical illustration for both of them to role them inward software automation test.

Difference betwixt @Before together with @BeforeClass annotations
  • Test method marked amongst @Before tone volition live executed earlier the each @Test method. Means if at that spot are 5 @Test methods inward your cast then @Before software bear witness method volition live executed five times.
  • Test method marked amongst @BeforeClass annotation volition live executed only earlier the class. Means @BeforeClass method volition live executed entirely i time earlier the cast fifty-fifty if at that spot are 5 @Test methods inward your cast of software bear witness case.
Difference between @After and @AfterClass annotations
  • Same as @Before annotation, Test method marked amongst @After annotation volition live executed later on the each @Test method.
  • @AfterClass tone volition live executed entirely i time later on last @Test method executed.
Execute bellow given @Before together with @After annotations illustration inward your eclipse together with notice effect inward 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;  world cast junittest2 {  mortal static WebDriver driver;    @Before world void openbrowser() {  System.out.print("\nBrowser open");  driver = novel FirefoxDriver();   driver.manage().window().maximize();  driver.get(" "); }  @After world void closebrowser() {  System.out.print("\nBrowser close");  driver.quit(); }    @Test  world 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  world 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 y'all execute inward a higher house illustration inward eclipse, Bellow given effect volition live displayed inward 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 inward a higher house given console output, nosotros tin toilet country each @Before method is executed earlier each @Test method together with each @After method is executed later on each @Test method. Now allow nosotros supervene upon @Before tone with @BeforeClass together with @After tone with @AfterClass inward same illustration together with thence notice result. Replace bellow given @BeforeClass together with @AfterClass business office amongst @Before together with @After business office inward in a higher house illustration every bit bellow.

@BeforeClass world static void openbrowser() {  System.out.print("\nBrowser open");  driver = novel FirefoxDriver();   driver.manage().window().maximize();  driver.get(" "); }  @AfterClass world static void closebrowser() {  System.out.print("\nBrowser close");  driver.quit(); }

Now move inward a higher house illustration together with expect at console result. Console effect volition live every bit bellow.
Console Output :
Browser open
junittest2 class-test1 method is executed
junittest2 class-test2 method is executed
Browser close

As per console result, nosotros tin toilet say @BeforeClass method is executed i time only. Same way @AfterClass tone marked method is too executed i time only. This means y'all tin toilet role dissimilar annotations of testng inward your software automation test.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post