Single Post

Header

Sunday, November 1, 2015

JUnit annotations in Selenium WebDriver

Different JUnit annotations using Selenium WebDriver

@Test        --- Each test method
@Ignore      --- Ignore test
@Before      --- Before each test method inside a class
@After       --- After each test method inside a class
@BeforeClass --- Before class tests
@AfterClass  --- After class tests

package javaPrograms;

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 JUnitAnnotations {
 
    private WebDriver driver;
 
    @BeforeClass
    public static void beforeClass() {
        System.out.println("This will execute before the class execution");
    }
 
    @Before
    public void setUp() {
        driver = new FirefoxDriver();
        driver.get("https://www.google.co.in/");
        System.out.println("Before method would execute before of ->Test methods");
    }
 
    @Before       // Generally we do not write multiple Before methods,but can write
    public void setUpDummy() {
        System.out.println("Before method would execute before of ->Test methods");
    }
 
    @Test
    public void testGoogleSearch() throws InterruptedException {
        driver.findElement(By.className("gsfi")).sendKeys("testing");
        driver.findElement(By.name("btnG")).click();
        System.out.println("Test methods would execute after ->Before methods");
        Thread.sleep(3000);
    }
 
    @After
    public void tearDown() {
        driver.close();
        System.out.println("After method would execute after ->Test methods");    
    }
 
    @AfterClass
    public static void afterClass() {
        System.out.println("This will execute after the class execution");
    }
}



Output:
This will execute before the class execution
Before method would execute before of ->Test methods
Before method would execute before of ->Test methods
Test methods would execute after ->Before methods
After method would execute after ->Test methods
This will execute after the class execution



No comments:

Post a Comment