TestNG Annotations in Selenium

TestNG :


TestNG is an automation framework mostly used in Selenium automation scripts for Web applications.It is very important to understand the annotations while working with TestNG for Selenium WebDriver. Testng providing suitable annotations to prepare Test scripts in Selenium WebDriver where you can pass test data from different files such as Excel,XLS files and You can run the scripts before tarting the class Method etc.

TestNG Annotations in Selenium
TestNG Annotations in Selenium



TestNG has below annotations those are

  1. BeforeSuite 
  2. AfterSuite 
  3. BeforeTest 
  4. AfterTest
  5. BeforeGroups
  6. AfterGroups 
  7. BeforeClass
  8. AfterClass
  9. BeforeMethod 
  10. AfterMethod 
You can read TestNG Tutorials from Here

Explanation:


@BeforeSuite: This annotation method will be run before all tests in this Test suite have to run.
@After Suite: This annotated method will be run after all tests in this Test suite have to run.
@BeforeTest: This annotated method will run before any test method which is belongs to the classes inside the test tag.
@After Test: This annotated method will be run after all the test method which is belongs to the classes inside the test tag have to run.
@BeforeClass: This annotated method will run before the first test method in the current class is invoked.
@After Class: This annotated method will be run after all the test methods in the current class have run.
@BeforeMethod : This annotated method will be run before each test method.
@AfterMethod: This annotated method will be run after each test method.

Example:


Before Test Selenium Web Driver will initiate and run the Tests in classes.

package com.fs.msn.automate;
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;

public class SeleniumWebDriv {
 public static WebDriver driver=null;  
 public String baseUrl;
 
   @BeforeTest public void BeforeTests () throws IOException{ 
   
  //Import Chrome extension from Local machine   
 File ChromeDriver = new File("D:\\Rajesh\\2014\\Java_Applications\\Zip Files\\chromedriver.exe");     
 //Set Chrome driver property    
 System.setProperty("webdriver.chrome.driver", ChromeDriver.getAbsolutePath());
 
    driver = new ChromeDriver();    
 driver.manage().window().maximize();    
 baseUrl="http://www.wikishown.com";   
 driver.get(baseUrl);      
 }
}

Please provide your valuable comments on this post and provide any suggestions in order to get users experience ,so that i can improve the site content to understand easily.Thank you for reading my blog.

Also Read


Complete Selenium Tutorials
Verify Element is Enable
Selenium Xpath Tutorial
Selenium DataDriven Framework