Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Working with TestNG.XML to execute Selenium test suite

While using TestNG, we can execute multiple tests or methods in the project based on the test annotations, the methods, and classes in the Project. We will execute the test suite from eclipse using TestNG.xml in this article to keep it simple, although we can execute the Test Suite from command line or through Ant.

Pre-condition:

Focus of this article:

  • How to create TestNG xml file to run test suite.
  • How to run test from the TestNG xml file in eclipse.


Let us consider the below two classes in a Java Project. To Keep it simple, we are just outputting a value in each of the Test method. Suppose we have two different classes, we can create a test suite using testng.xml file and run the test suite from eclipse as shown in the below image.


Creating TestNG.xml file for different scenarios:


A. Run all the tests in both classes NewTest and NewTest1. This will execute all the methods with beforetest annotation in both the classes, followed by methods with test annotation, and then methods with afterText annotation.

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
  <test name="Test">  
   <classes>  
    <class name="testNG.NewTest"/>  
       <class name="testNG.NewTest1"/>  
   </classes>  
  </test>  
 </suite>  


B. Run test in class NewTest and then the methods in NewTest1 class:

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
  <test name="Test1">  
   <classes>  
    <class name="testNG.NewTest"/>  
         </classes>  
  </test>  
  <test name="Test2">  
   <classes>  
    <class name="testNG.NewTest1"/>  
   </classes>  
  </test>  
 </suite>  

C. Executing all tests in a package with name as TestNG(Note: TestNG is the name of package in our example, it can be any name other than TestNG also)

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
   <packages>  
    <package name="testNG" />  
   </packages>  
  </test>  
 </suite>  

D. Executing all tests based on the group Name:

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
  <groups>  
   <run>  
    <include name="testemail"/>  
    <exclude name="testnoemail"/>  
   </run>  
  </groups>  
   <classes>  
    <class name="testNG.NewTest"/>  
    <class name="testNG.NewTest1"/>  
   </classes>  
 </test>  
 </suite>  

E. Executing specific methods by including methods in the class:

 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
 <suite name="Suite1">  
 <test name="Test1">  
 <classes>  
   <class name="testNG.NewTest">  
    <methods>  
     <include name="emailval" />  
    </methods>  
   </class>  
   <class name="testNG.NewTest1"/>  
 </classes>  
 </test>  
 </suite>  

What are Annotations in TestNG: Selenium Automation


In the Previous tutorials, we explained how to add TestNG to eclipse and how to set up for creating the first test using TestNG. In this article, we will understand what annotations in TestNG are and how they are used:


When we create a test in TestNG, we divide the code into different annotations based on which section needs to be executed and in which order. 

Let us take an example, We have three different tests to be automated. Each of the test starts with Login into application, In the next step, the user has either to view e-mails, write e-mail or verify successful login only. In the final step, we have to log out of application.


To code this, we have to login into application in each of the three test, so we can define this method or code in @BeforeTest.
Next we will define all the tests to be executed with annotation @Test and can assign group of test to be executed using group with test.

Once the test is executed, user will log out from application, this we will tag with annotation @AfterTest.

Below code explains how the three annotations can be used in the test. There are many more annotations which we can add in the test to give logical structure to the test.

 package testNG;  
 import org.testng.annotations.Test;  
 import org.testng.annotations.BeforeTest;  
 import org.testng.annotations.AfterTest;  
 public class NewTest {  
  @BeforeTest  
  public void Login()  
  {  
       System.out.println("Login into application");  
  }  
  @Test(groups = { "testemail" })  
  public void mailval()  
  {  
       System.out.println("test e-mail");  
  }  
  @Test(groups = { "testemail" })  
  public void emailval()   
  {  
       System.out.println("e-mailvalidated");  
  }  
  @AfterTest  
  public void Logout()  
  {  
       System.out.println("Logout from application");  
  }  
 } 

                                  Annotations in TestNG

   Below table explains the different annotation used in TestNG:


Annotation
Description
@BeforeSuite
Annotated method to be executed before all tests in the suite have run. 
@AfterSuite
Annotated method to be executed after all tests in the suite has run. 
@BeforeTest
Annotated method to be executed before any test method belonging to the classes inside the <test> tag is run. 
@AfterTest
Annotated method to be executed after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeGroups
The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroups
The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@BeforeClass
The annotated method to be executed before the first test method in the current class is invoked. 
@AfterClass
The annotated methods to be executed after all the test methods in the current class have been run. 
@BeforeMethod
The annotated method to be executed before each test method. 
@AfterMethod
The annotated method to be executed after each test method.

In the next article, we will explain how a test suite is defined in TestNG and how does a test pass or fail in TestNG i.e how to add assertions in TestNG.

Creating first TestNG test using eclipse for Selenium WebDriver


TestNG provides a testing framework to test the application. TestNG framework can be used to write unit test, integration as well as functional tests. Our objective in this blog will be to understand and implement TestNG framework while automating with Selenium WebDriver using eclipse.


In the previous article, we explained how to install TestNG libraries with eclipse. In this article, we will explain how to create first TestNG test in eclipse.

Pre-Requisite: We have installed TestNG as discussed in previous article.


Steps to create first TestNG test in eclipse:

  • Create a new Project in eclipse as shown in the image below and add the TestNG library:

Add TestNG library to eclipse java Project



Add TestNG library
Add caption

  • Once we have added the library, we can use the library in our test.

Creating a testNG Class in the java Project:


A testNG test is executed based on the testNG annotations in the TestNG class.

Creating a testNG class
New TestNG class

Below code will be generated once we click on finish.


 package testNG;  
 import org.testng.annotations.Test;  
 import org.testng.annotations.BeforeTest;  
 import org.testng.annotations.AfterTest;  
 public class NewTest {  
  @Test  
  public void f() {  
  }  
  @BeforeTest  
  public void beforeTest() {  
  }  
  @AfterTest  
  public void afterTest() {  
  }  
 }