TestNG Annotations in Selenium with Example - TestNG Annotations Sequence

.
TestNG Annotations With Example in Selenium Webdriver

TestNG Annotations in Selenium with Example


What Are TestNG Annotations?


TestNG (Test Next Generation) is a testing framework for the Java programming language inspired by JUnit and NUnit. It provides annotations to define test methods, configuration methods, and suite-level setup/teardown methods. Here are some commonly used TestNG annotations.

TestNG Annotations Sequence
TestNG annotations sequence of execution are as follows 
  • @BeforeSuite
  • @BeforeTest 
  • @BeforeClass 
  • @BeforeMethod 
  • @Test 
  • @AfterMethod 
  • @AfterClass 
  • @AfterTest 
  • @AfterSuite
Above is the hierarchy of the TestNG Annotations 

TestNG Annotations in Selenium Webdriver with Examples



Selenium Code 

package testNGPackage;


import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@BeforeSuite

public void testsuite() {

System.out.println("This is before suite");

}

@BeforeTest

public void beofretest() {

System.out.println("This is before test");

}

@BeforeClass

public void beofreclass() {

System.out.println("This is before Class ");

}

@BeforeMethod

public void beofremethod() {

System.out.println("This is before method ");

}

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@AfterMethod

public void aftermethod() {

System.out.println("This is after Method ");

}

@AfterClass

public void afterclass() {

System.out.println("This is after Class ");

}

@AfterTest

public void aftertest() {

System.out.println("This is after Test ");

}

@AfterSuite

public void aftersuite() {

System.out.println("This is after suite ");

}

}




@BeforeSuite
The method with BeforeSuite annotation will run only once before all tests in the suite are executed.

Selenium Code

package testNGPackage;


import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@BeforeSuite

public void beforesuite() {

System.out.println("This is before suite");

}


}


XML File 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->



@BeforeTest 
The method with BeforeTest annotation will run before the execution of all the test methods of available classes belonging to that folder. BeforeTest annotation can be executed multiple times before the @Test.

Selenium Code 

package testNGPackage;


import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@BeforeTest

public void beforemethod() {

System.out.println("This is before method");

}


}



XML File 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->

 

@BeforeClass 

The @BeforeClass method will execute before the execution of the method in the current class.

Selenium Code


package testNGPackage;


import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@BeforeClass

public void beforeclass() {

System.out.println("This is before Class ");


}


}



XML File 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite

> <!-- Suite -->


@BeforeMethod 

Selenium Code

package testNGPackage;


import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@BeforeMethod

public void beforemethod() {

System.out.println("This is Before method");

}


}


XML File 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->



@Test 


@AfterMethod 

Selenium Code

package testNGPackage;


import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@Test

public void seconfmethod() {

System.out.println("This is second Method");

}

@AfterMethod

public void Aftermethod() {

System.out.println("This is After method");

}


}



XML File 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->



@AfterClass 

Selenium Code

package testNGPackage;


import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@AfterClass

public void afterclass() {

System.out.println("This is after Class ");


}


}



XML File


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->




@AfterTest 

Selenium Code 


package testNGPackage;


import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@AfterTest

public void aftertest() {

System.out.println("This is after method");

}


}


XML File 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->



@AfterSuite


Selenium Code:

package testNGPackage;


import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeSuite;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;


public class TestNGAnnotaions {

@Test

public void testmetnod() {

System.out.println("This is Test Method");

}

@AfterSuite

public void aftersuite() {

System.out.println("This is After suite");

}


}



XML File 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="User Login Details">


<test name="userSignup">

<classes>

<class name="testNGPackage.FirstTestngClass"/>

<class name="testNGPackage.SecondTestngClass"/>

</classes>

</test> <!-- Test -->

<test name="Vendorsignup">

<classes>

<class name="testNGPackage.TestNGAnnotaions"/>

<class name="testNGPackage.ThirdTestNGClass"/>

</classes>

</test> <!-- Test -->

</suite> <!-- Suite -->



Hope !!! The above Tutorial on TestNG Annotation in Selenium with examples is helpful for you...

Team,
QA acharya



Related Tags: TestNG annotations, TestNG annotations sequence, TestNG annotations with example, testng annotations sequence of execution, testng annotations priority, testng annotations in selenium,testng annotation hierarchy,testng annotation in selenium , testng annotations and uses

Post a Comment

0 Comments