TestNG- Selenium Automation



TestNG annotations and their use with example.

@BeforeSuite

This annotation method will run before the execution of all the test methods in the suite.

@AfterSuite

This annotation method will run after the execution of all the test methods in the suite.

@BeforeTest

This annotation method will execute before the execution of all the test methods of available classes belonging to that folder.

@AfterTest

This annotation method will execute after the execution of all the test methods of available classes belonging to that folder.

@BeforeClass

This annotation method will execute before the first method of the current class is invoked.

@AfterClass

This annotation method will invoke after the execution of all the test methods of the current class.

@BeforeMethod

This anotation method will execute before each test method will run.

@AfterMethod

This annotation method will run after the execution of each test method.



@BeforeSuite
 public void Test1(){
  System.out.println("BeforeSuite");
 }
 @AfterSuite
 public void Test2(){
  System.out.println("AfterSuite");
 }
 @BeforeTest
 public void Test3(){
  System.out.println("BeforeTest");
 }
 @AfterTest
 public void Test4(){
  System.out.println("AfterTest");
 }
 @BeforeClass
 public void Test5(){
  System.out.println("BeforeClass");
 }
 @AfterClass
 public void Test6(){
  System.out.println("AfterClass");
 }
 @BeforeMethod
 public void Test7(){
  System.out.println("BeforeMethod");
 }
 @AfterMethod
 public void Test8(){
  System.out.println("AfterMethod");
 }
 @Test
 public void Test9(){
  System.out.println("Test");
 }
 @Test
 public void Test10(){
  System.out.println("Test1");
 }

 
 

Output-




========================================================================

@BeforeGroups

This annotation method run only once for a group before the execution of all test cases belonging to that group.

@AfterGroups

This annotation method run only once for a group after the execution of all test cases belonging to that group.





@BeforeClass(groups={"A","B"})
 public void Before1()
 {
  System.out.println("before");
 }
 @Test
 public void Test1()
 {
  System.out.println("Test1");
 }
 @Test(groups={"A","B"})
 public void Test2()
 {
  System.out.println("Test2"); 
 }
 @AfterClass(groups={"A"})
 public void After()
 {
  System.out.println("After");
 }


 

Runable XML file-




<suite name="new suite" parallel="methods" thread-count="2">
 <test name="new test">
  <groups>
   <run>
   <include name="B" />
   </run>
  </groups>
  <classes>
 <class name="org.newpack.Runable" />
  </classes>
 </test>
</suite>



 

Output-










========================================================================

@DataProvider

This annotation is used to pass the data to method. This annotation method will return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation




@DataProvider(name="SearchProvider")
 public Object[][] dataProviderMethod()
 {
return new Object[][] { { "data one" },
{ "data two" } };
        }
 @Test(dataProvider="SearchProvider")
 public void method1(String data)
 {
  System.out.println(data);
 }

Output-



=======================================================================

@Parameters

This annotation is used to pass the parameter, Parameter can be passed from xml file or from Goal.

@Test
@Parameters({"name","lastname"})
public void Test5(String name, String lastname)
{
System.out.print("The Name is:-");
System.out.println(name);
System.out.print("Last name is:-");
System.out.println(lastname);
}

Executable XML file




<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parameter file">
<test name= "new parameter">
 <parameter name="name" value="Yogen"/>
  <parameter name="lastname" value="Thakur"/>
<classes>
<class name="org.newpack.PassPara"/>
</classes>
</test>
</suite>

Output-







=======================================================================

@Test

Below things can be used to make @Test annotation more effective and useful.

-priority-This command sets the priority of the test method. Lower priorities will be scheduled first
-Grouping-It is the list of groups this method depends.
-dataProvider-TestNG dataProvider is used to provide any data for parameterization.
-dependsOnMethods-This command is used to execute a method based on its dependent method
-description: It is the description for the method
-invocationCount: It refers to the number of times a method should be invoked. It will work as a loop
-alwaysRun -This is used when we want to make sure a method always runs even if the parameters on which the method depends, fails. If set to true, this test method will always run.























No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...