TestNG Tutorial : How to use TestNG annotations in Selenium?

TestNG is a unit testing framework for Java programming language and NG stand for Next Generation.



Hey folks, In this article I'm going to cover the most important topic that is called TestNG Framework. In this article, we will cover the following topics

  1. Introduction to TestNG
  2. How to integrate TestNG with selenium?
  3. How to add TestNG into your project?
  4. How to use different annotations in TestNG?

1. Introduction to TestNG

TestNG is a unit testing framework for Java programming language and NG stand for Next Generation. It is also called the Java unit testing framework. It is a very famous framework used by the developers to test the code. When developers develop some code, they test their code at the class level with the help of the TestNG framework. It is also used by AUtomation Engineers to design the test cases. They can integrate it with selenium. It is a very powerful tool, it gives a lot of options and features to design your test cases. It can also generate HTML reports. The purpose of TestNG is to design the test cases in a systematic way. Here are some key features
  • Open source, free to use
  • Available in the form of jar files
  • Only applicable with Java
  • Generate very good HTML reports
  • Supports a lot of annotations
  • Manage the sequence of test cases
  • Manage priorities of test cases 
  • Manage test cases dependencies
  • Manage test cases grouping
The most important feature is the data provider. You can execute your test case multiple times with different sets of data by using a data provider. To execute the same test cases again and again with a different set of data TestNG provides the amazing feature which is called data provider. Suppose you want to test the login functionality with different user names and passwords, you can use this feature. With data provider, you can read data from excel sheets.

Whenever automation engineers design an automation framework they must use the TestNG because of its rich support in designing the test cases. So in another form, it is also called TDD framework. TDD means Test Driven Development. In TDD tests are written before writing the actual code. Developers write the tests first before implementing the actual code. Developers use TestNG for unit testing.

When you join a new company and you have a fresh project to start the automation framework from scratch, you can use TestNG to design the test cases in a systematic way.

2. TestNG Integration with Selenium

Now let's start from scratch, how to download and install TestNG in Eclipse. For this, you can visit the following article which will explain in detail how to install TestNG in Eclipse.
How To Download And Install TestNG In Eclipse For Selenium WebDriver

3. Add TestNG into Your Project

Now let's see how to add the TestNG library into your project. The following steps are needed to add TestNG into your project

Step 1)
  1. Right-click on your project
  2. Click on Properties


Step 2)
  1. Click on Java Build Path
  2. Click on Add Library


Step 3)
  1. Select the library TestNG
  2. Click on Next button

















Step 4)
  1. Click on Finish button

















Step 5)
  1. Click on Apply and Close button
Step 6)
  1. Go to your project
  2. Check the TestNG folder
  3. As shown in the below image

















4. Annotations in TestNG

Now let's try to explore how automation engineers design test cases with the TestNG framework. What are the different annotations which are used to design test cases? Let's create a separate project as I have created "TestNGBasics" for demonstration purposes.

Remember one thing while creating a class doesn't select the main method because in TestNG we don't' need the main method to run the class. TestNG automatically executes the class. We never work with the main method in the TestNG class.













After creating a class let's try to write the selenium code with annotations. First, we explore what different annotations are available in TestNG.

TestNG provides different kinds of annotations. Every annotation is associated with one method. Annotations always start with @. In TestNG class, we simply write Java code with some other features provided by TestNG in the form of annotations. So when have you ever seen that some code is starting with "@" it means that is an annotation. In the TestNG framework, we will explore the following annotations.

  • @BeforeSuite
  • @BeforeTest
  • @BeforeClass
  • @BeforeMethod
  • @Test
  • @AfterMethod
  • @AfterCLass
  • @AfterTest
  • @AfterSuite

Given below is a demo example of TestNG annotations. This example is showing the use and sequence of annotations.
package com.test;
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 TestNGBasics {
 @BeforeSuite
 public void setUp() {

  System.out.println("Set system properties for chrome");
 }
 @BeforeTest
 public void launchBrowser() {
  System.out.println("Launch chrome");
 }
 @BeforeClass
 public void enterUrl() {
  System.out.println("Enter URL");
 }
 @BeforeMethod
 public void login() {
  System.out.println("Login to app");
 }
 @Test
 public void testTitle() {
  System.out.println("Title test");
 }
 @AfterMethod
 public void logOut() {
  System.out.println("Log out from app");
 }
 @AfterClass
 public void closeBrowser() {
  System.out.println("Close Browser");
 }
 @AfterTest
 public void generateReport() {
  System.out.println("Delete Cookies");
 }
}
The output is here
Output:
Set system properties for chrome
Launch chrome
Enter URL
Login to app
Google title test
Log out from the app
Close Browser
Delete Cookies
A TestNG class has three sections. The first section is the preconditions section, test case, and postconditions.
  • Pre-Conditions
  • Test Cases
  • Post-Conditions
The annotations that start with @Before keyword come into the pre-conditions section. The second section contains the @Test annotation. The third section contains the annotations that start with @After keyword. The most important thing is the sequence of annotations. You should be familiar with the sequence of annotations. As you can see the sequence in the below diagram.































In the above example, we have executed only one test case. Now let's see the scenario for more than one test case. If we add the following one more test case in the above example, the output will be a little bit different in terms of annotations sequence.

public class TestNGBasics {
 @BeforeSuite
 public void setUp() {
  System.out.println("Set system properties for chrome");
 }
 @BeforeTest
 public void launchBrowser() {
  System.out.println("Launch chrome");
 }
 @BeforeClass
 public void enterUrl() {
  System.out.println("Enter URL");
 }
 @BeforeMethod
 public void login() {
  System.out.println("Login to app");
 }
 // Test Case 1
 @Test
 public void testTitle() {
  System.out.println("Title test");
 }
 Test Case 2
 @Test
 public void searchTest() {
  System.out.println("Search test");
 }
 @AfterMethod
 public void logOut() {
  System.out.println("Log out from app");
 }
 @AfterClass
 public void closeBrowser() {
  System.out.println("Close Browser");
 }
 @AfterTest
 public void generateReport() {
  System.out.println("Delete Cookies");
 }
}
The following is the output of the code
Output:
Set system properties for chrome
Launch chrome
Enter URL
// Test Case 2
Login to app
Search test
Log out from the app
// Test Case 1
Login to app
Title test
Log out from the app
Close Browser
Delete Cookies
Remember one thing, the two annotations @BeforeMethod and @AfterMethod are executed for every test case. Suppose if you have n number of test cases, then these annotations will be executed n times. One more thing in the above output that is the test case 2 is executing first then test case 1. The reason is that TestNG executes the tests alphabetically. We can also set the priorities for test cases which we will cover in the next section. 

Now let's try to write the selenium code with TestNG annotations. In the following example, you will also learn how to control the execution of test cases by using priorities. Suppose you have n number of the test case, you can set the priority of each test case. Based on these priorities execution will happen.

package com.test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class GoogleTest {
 public String baseUrl = "http://google.com";
 public WebDriver driver;
 @BeforeMethod
 public void launchBrowser() {
  System.setProperty("webdriver.chrome.driver", "/home/khawer/Desktop/chromedriver");
  driver = new ChromeDriver();
  driver.get(baseUrl);
 }
 @Test(priority = 1)
 public void googleTitle() {
  String expectedTitle = "Google";
  String actualTitle = driver.getTitle();
  Assert.assertEquals(actualTitle, expectedTitle);
 }
 @Test(priority = 2)
 public void logoTest() { 
  // Please write the logic here
 }
 @AfterMethod
 public void close() {
  driver.close();
 }
}

Mostly the three annotations (@BeforeMethod, @Test, @AfterMethod) are used in TestNG. In t above example, we have used priority to control the execution of test cases. Priority 1 means this test case will be executed first then priority 2 and so on. I hope this article will make sense for you guys so far we have covered. In the next article, we will cover the @dataprovider annotation, HTML report some other stuff related to TestNG.

0 comments:

Post a Comment