About Me

Page views

Powered by Blogger.

Followers

Saturday, 9 May 2015

 

So 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. You must need to insert JUnit annotation inside the java code to execute your test case as junit test case. 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 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.

4. @BeforeClass
Methods under @BeforeClass annotation will be executed before the any 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();

 @Before
 public void beforetest() {
  driver.manage().window().maximize();
  driver.get("http://only-testing-blog.blogspot.in/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

 

0 comments:

Post a Comment

Popular Posts

Copyright © Learn Selenium Yourself | Powered by Blogger
Design by Duan Zhiyan | Blogger Theme by NewBloggerThemes.com