Selenium with TestNG Tutorial

 TestNG is a testing framework developed in the lines of JUnit and NUnit, however it introduces some new functionalities that make it  more powerful and easier to use. It is designed to cover all categories  of tests − unit, functional, end-to-end, integration, etc., and it requires JDK 5 or higher. Automation Testers make use of TestNg to execute our tests. And it also provides some cool features in the form of parameters of annotations.

Table of Index

  1. TestNG Features
  2. TestNG Components
  3. Install TestNG in Eclipse
  4. TestNG Annotations
  5. Annotation Parameters
  6. Selenium with TestNG Integration
  7. Verify and Assert
  8. Creating test suite using testng.xml
  9. TestNG Page Factory
  10. TestNG Data Provider


1. TestNG Features

  • Annotations (@BeforeMethod, @Test, @@AfterMethod etc.)
  • Flexible test configuration
  • Support for data-driven testing (with @DataProvider)
  • Support for annotation parameters
  • DependsOnMethods
  • Priority
  • Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
  • Default JDK functions for runtime and logging (no dependencies)


2. TestNG Components

  1. Annotations 
  2. Annotation Parameters
  3. Verify and Assert
  4. Page Factory
  5. Data Provider
  6. Creating suite using testng.xml


3. Install and Setup TestNG in Eclipse

  1. Open Eclipse.
  2. Click on the 'Help' menu option.
  3. Click on "Eclipse Marketplace"
You need to install TestNG plugin in your Eclipse to work with it. Below are the steps:



4. Type Testng in Find text box and click on search icon. After TestNG is appeared in the search click on the Install button.


5. After clicking Install, it will show some other dialogues, just click accept the terms and click Finish.
6. Installation will take some time to complete.
7. Eclipse will ask you for restart, click yes.
8. After restart, you can verify if TestNG is installed correctly or not. Click Window -> Preferences , TestNG should display in Preferences list.


4. What are Annotations in TestNG?

TestNG annotations are the most important part, they control the execution of test and other methods in the automation script. And in this post I am going to explain some of the important annotations. So, we will be covering following
  1. @BeforeClass
  2. @AfterClass
  3. @BeforeMethod
  4. @AfterMethod
  5. @Test
Let's summarize annotation:
  • Annotation in TestNG actually is code snippet which lies just ahead of the method declaration and controls the execution of the method
  • Annotation starts with @ sign. 
Sample Code:
import org.testng.annotations.Test;

public class TestngPriority {
 
 
  @Test(priority=0) // <<<<==== This is a TestNG annotation
  public void one() {
   System.out.println("This is test case 1");
  }

In below example, there are two methods f and g with @test annotation and every other annotation has one method each.

Code Example:
package com.techlistic.selenium;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

public class TestNGAnnotations {
 
  @Test
  public void f() {
   System.out.println("f Test Case/Method");
  }
  
  @Test
  public void g() {
   System.out.println("g Test Case/Method");
  }
  
  @BeforeMethod
  public void beforeMethod() {
   System.out.println("Before Method");
  }

  @AfterMethod
  public void afterMethod() {
   System.out.println("After Method");
  }

  @BeforeClass
  public void beforeClass() {
   System.out.println("Before Class");
  }

  @AfterClass
  public void afterClass() {
   System.out.println("After Class");
  }

}

Output:

[RemoteTestNG] detected TestNG version 6.14.3
Before Class
Before Method
f Test Case/Method
After Method
Before Method
g Test Case/Method
After Method
After Class
PASSED: f
PASSED: g

Observe the execution order:
  1. First of all @BeforeClass is executed.
  2. Then @BeforeMethod
  3. @Test for f
  4. @AfterMethod
  5. @BeforeMethod
  6. @Test for g
  7. @AfterMethod
  8. @AfterClass
Now, you would be able to make out the execution flow. So, this way you can create your setup, teardown methods and use whatever before/after annotation with it as per your requirement. If you want to execute your setup only once at class level then use @BeforeClass or if you want to execute setup before every test case/method then use @BeforeMethod (Same for teardown).

5. Annotation Parameters

i. Set Test Methods Priority

TestNG also provides a way to set the priority of test cases. By priority I mean, priority of execution. It can be used with @Test annotation. Below is the syntax for it:

@Test(priority=0)

In below code example,
  • We have four test methods 
  • And we have set the priority of each method as 0,1,2,3 
  • The method which has 0 priority will be executed first 
  • Then method with priority 1 will be executed and so on 
So, this how TestNG gives us a very easy way to control the execution of our test methods/cases.
Example Program:

import org.testng.annotations.Test;

public class TestngPriority {
 
 
  @Test(priority=0)
  public void one() {
   System.out.println("This is test case 1");
  }
  
  @Test(priority=2)
  public void two() {
   System.out.println("This is test case 2");
  }
  
  
  @Test(priority=1)
  public void three() {
   System.out.println("This is test case 3");
  }
  
  
  @Test(priority=3)
  public void four() {
   System.out.println("This is test case 4");
  }
  
}

Output:
This is test case 1
This is test case 3
This is test case 2
This is test case 4

ii. Set Dependency of Test Methods

TestNG provides a way to manage method dependencies with annotation parameters. For this testNg provides 'DependsOnMethods' parameter for @Test annotation. If you have declared dependency of one method on second, then first of all second method will be executed and then after first method. 

Example Code:


Program Explanation:
  • In above code, method SignIn() is dependent on OpenBrowser() 
  • And method LogOut() is dependent on both SingIn and OpenBrowser methods. 
Below is the execution sequence of above code snippet:
1. OpenBrowser
2. SignIn
3. LogOut.

Output:
This will execute first (Open Browser)
This will execute second (SignIn)
This will execute third (Log Out)

6. How to use Selenium with TestNG

The TestNG annotations that we learned so far can be used with Selenium automation code. Let's take a look at a sample program where we have used TestNG annotations inside Selenium code and then we'll understand it step by step in the program explanation section below:

Sample Program

package com.techlistic.selenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNGAnnotations2 {
 
 WebDriver driver;
 
 @BeforeClass
 public void setupClass() {
  
  // Set Chrome driver property
  System.setProperty("webdriver.chrome.driver", "D:\\mydir\\chromedriver.exe");
  
  // Launch chrome browser
  driver = new ChromeDriver();

  // Opening Browser URL and maximize it
  driver.get("http://www.techlistic.com/");
  driver.manage().window().maximize();
 }
 
 @BeforeMethod
 public void setupMethod() {
  // Webdriver Code for login

 }
 
 @Test
 public void test_HomePage() {
  // Webdriver code to test some functionality on Home page

 }
 
 @Test
 public void test_UsersPage() {
  // Webdriver code to test some functionality on Users page

 }
 
 @AfterMethod
 public void teardownMethod() {
  // Webdriver Logout code

 }
 
 @AfterClass
 public void teardownClass() {
  // Close all webdriver instances
  driver.close()
  driver.quit()
 }
}

Program Explanation:
  1. @Test - This annotation is used with test methods.
  2. @BeforeClass - If this annotation is used with some method then it would be executed only once before all test methods or you can say when the class is about to be executed then @BeforeClass executes. It is generally used for setup method for class. E.g.,
    • Let's say your class has 10 test methods
    • And you want to launch browser only once at the beginning and use the same browser instance for all test methods
    • Then launch browser in you setup_class method with @BeforeClass on top of it.
  3. @BeforeMethod/@AfterMethod - Use this method for setup method to be called before every test. Like in above example, we are logging in before every test method and logging out after every method.
  4. @AfterClass - Use this annotation for teardown class method. For e.g., in above example we used it for closing all the browser instances at the end of executing all the test methods.
Happy Learning!


Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate E-Commerce Website using Selenium Webdriver

Automate Browser Actions on Godaddy.com using Selenium

Automate Amazon Website 'Menu links' using Selenium