TestNG in Selenium is a Java testing framework, inspired by JUnit and NUnit. It overcomes the constraints and disadvantages of JUnit and introduces an entirely new set of properties, making TestNG more powerful and easy to use. The suffix ‘NG’ stands for Next Generation, signifying the new functionalities that TestNG brings to the table. From simple unit testing to complex integrated testing, it is designed to simplify all your testing requirements such as functional testing, regression, end-to-end testing, and more.
Check out this Intellipaat video to know ‘What is TestNG in Selenium?’:
Before moving ahead and knowing what exactly TestNG in Selenium is all about, let us understand why did TestNG come into existence?
Why did TestNG come into existence?
You must be wondering when you already have the Selenium framework, why do you need TestNG with Selenium WebDriver for automation testing? Well, the answer is, since Selenium does not have any built-in hardware or framework for generating test reports, you require the help of an external framework like TestNG to fulfill the need for generating test reports and simplifying testing requirements such as functional testing, regression, end-to-end testing, and more.
TestNG in Selenium: An Overview
In Selenium, there are mainly two testing frameworks available and they are:
1) JUnit
2) TestNG
What is TestNG in Selenium?
TestNG is an open-source testing framework where NG stands for ‘Next Generation.’ It is architected to simplify a broad range of testing needs starting from unit testing to integrated system testing. Initially, both JUnit and TestNG were designed solely for unit testing. TestNG is inspired by the JUnit Java platform and NUnit .NET platform, and some new functionalities were introduced in TestNG, making it more powerful and easy to use than the JUnit testing framework.
If you want to use Selenium with .NET, then you have to use the NUnit testing framework, since NUnit supports the .NET platform.
Get 100% Hike!
Master Most in Demand Skills Now!
Advantages of TestNG over JUnit
- TestNG Annotations are used to create test cases easily.
- Test cases can be ‘grouped,’ ‘prioritized,’ and ‘executed’ more efficiently.
- It supports parameterization.
- It supports data-driven testing using Data Providers.
- It can generate HTML test reports of the results representing: the number of test cases runs, the number of test cases failed, the number of test cases skipped
- It effortlessly supports integration with various other tools and plugins like Eclipse IDE and built automation tools like Ant and Maven.
- It supports parallel execution.
- Logs can be generated.
- In TestNG, there is no need to state @AfterClass and @BeforeClass in a project, which is present in JUnit.
- You can specify any test method name in TestNG as the method’s name constraint is not present in TestNG like it is in JUnit.
- TestNG supports the following three additional setup and teardown levels: @Before/AfterSuite, @Before/AfterTest, and @Before/AfterGroup. TestNG goes beyond the idea of just writing @Test annotated methods and allows you to define these methods that will be run after or before your test suites, test groups, or test methods. This is very useful for your Selenium tests because you can create a Selenium server and browser instance before you start running your test suite.
- TestNG in Selenium does not require extending any class; hence, no need for the inheritance functionality.
- TestNG allows us to define the dependent test cases.
- TestNG in Selenium allows us to execute test cases based on the group. Let’s take a scenario where you have created two sets of groups ‘Regression’ and ‘Sanity.’ If you want to execute the test cases under the Sanity group, then you can do so easily with the TestNG framework.
In this TestNG in Selenium tutorial, let us now see how to install TestNG.
Installation of TestNG
Since Selenium does not have any built-in hardware for generating test reports, you need the help of an external framework like TestNG to fulfill the need for generating test reports and simplifying all your testing requirements such as functional testing, regression, end-to-end testing, and more.
Now that you know the need for installing TestNG, let us get started with installing this testing framework.
For this, it is assumed that you have Java JDK and Eclipse IDE for Java Developers already installed in your system.
Step 1: In the Eclipse IDE for Java Developers
- Click on the Help menu
- Click on Install New Software
Step 2: The below-shown window will popup
- Click on the Add button
- In the Name text box, type ‘TestNG’
- In the Location text box, type the URL ‘http://beust.com/eclipse’
- Click on Add
Step 3: The below-shown window will popup:
- Select TestNG
- Click on Next and then on Finish
Step 4: When the below-shown window pops up, click on Next
Step 5: Then, a window as shown below will popup:
- Click on the ‘I accept the terms of the license agreement’ radio button
- Click on Finish
Now, you have to restart the Eclipse IDE, select ‘Yes’ on the prompt box asking you to restart the Eclipse IDE.
There you go. You have successfully installed TestNG on the Eclipse IDE!
In this section of the TestNG in Selenium tutorial, let us move ahead and see how you can write your first multiple TestNG test cases in a single configuration file (testng.xml) in Eclipse:
Step 1: In the Eclipse IDE, click on the File menu
- Click on New
- Click on Java Project
Step 2:
- Give your project name as ‘Verification.’ You can give any name as per your wish
- Click on Finish
Step 3: When the below window pops up, click on ‘Don’t Create,’ for not creating a module for your project
Step 4:
- Right-click on your project name
- Then, click on Properties
Step 5: The below-shown window will popup
- Click on Java Build Path
- Click on Libraries
- Select Classpath
- Click on Add External JARs
Step 6:
- The below location will popup, wherein you will have all the Selenium Client and WebDriver Language Bindings JAR files which were extracted into a new folder from the Download menu of the www.selenium.dev site.
- Press Ctrl+A to select all the files in that folder
- Click on Open
Step 7: All the .jar files should be populated in the Classpath
- Click on Apply
- Then, click on Apply and Close
Under your project folder (here ‘Verification’) in the Package Explorer, you will find the Referenced Libraries folder being created containing all those .jar files within it.
Step 8: Now, add TestNG library to your project.
Note: The addition of external jars and the addition of the TestNG library need to be done for every new project you create.
After right-clicking on your project name and navigating to properties,
- Click on Java Build Path
- Click on Libraries
- Select Classpath
- Click on Add Library
Step 9: The below-shown window will popup
- Select TestNG
- Click on Add and then on Finish
A new folder named TestNG will get added under your project folder.
Now, you have successfully installed the TestNG framework with the help of Eclipse IDE. Let’s now write the first test case using Test annotations.
TestNG Annotations
In this section of the TestNG in Selenium tutorial, you will be introduced to the @Test annotations, various other advanced annotations and their usages. Annotations differ from project to project depending on their necessities. Though the requirements change, the execution flow will be the same for every single project.
In this section of the tutorial, you will learn about various advanced TestNG annotations and their usages.
Here are the steps to perform web application automation testing by creating multiple test annotated method and passing multiple attributes/parameters in @Test annotations:
Step 1:
- Click on your project name (here, ‘Verification’)
- Then, click on the src folder
- Select New
- Then, click on Package
Step 2:
- Enter the Name of the package, say VerifyTitle
- Click on Finish
Step 3: Create a TestNG class
- Right-click on your package name (here, VerifyTitle)
- Click on New
- Click on Other
Step 4:
- Click on TestNG class
- Click on Next
Step 5:
- Enter your TestNG class name, say VerifyNew
- Click on Finish
Step 6: Copy the below code and execute it in your local system’s Eclipse IDE
package VerifyTitle;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class VerifyNew {
public WebDriver driver;
@Test(priority =-1)
public void f() {
System.setProperty("webdriver.chrome.driver", "C:\Users\Intellipaat-Team\Downloads\driver\chromedriver.exe");
driver = new ChromeDriver();
}
@Test(priority = 0)
public void launchBrowser() {
String expectedTitle = "Online Professional Training Courses and Certification - Intellipaat";
driver.get("https://intellipaat.com/");
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
@Test(dependsOnMethods = {"launchBrowser"}, priority =3)
public void closeBrowser() {
driver.quit();
}
}
Code Explanation
Line 9: Global object (here, driver) declaration which is of type WebDriver; we need a global declaration of driver-object so that all the test-annotated methods can perform driver-driven actions efficiently
Note: If it shows an error, mouse-over the red line. A suggestions list appears, from which you have to import the TestNG WebDriver library, import org.openqa.selenium.WebDriver;
Line 10: A method named ‘f’ is annotated under the test annotation with priority set to -1, will be run first.
Note: If you see a red line as a sign of error, mouse-over it and import the import org.testng.annotations.Test; test annotation library.
Line 13: The System.setProperty(key,value) method, sets the system property key (here, webdriver.chrome.driver) to have the value,
C:\\Users\\Intellipaat-Team\\Downloads\\driver\\chromedriver.exe
In Selenium, you use this method because the browser doesn’t have a built-in server to run the automation code, so you will need a Chrome/IE/Gecko (according to your requirement) driver server for communicating your Selenium code to the browser.
Note: At the end of the path location of the WebDriver, in the value parameter, make sure to add (path…\chromedriver.exe”);
Line 14: By using this, your scripts are now flexible and you can use any WebDriver object (here, driver) which is required to invoke a particular browser (here, Chrome Browser). The reference variable (driver) of type WebDriver class allows us to assign the driver object to different browser-specific drivers.
Note: If you see a red line as a sign of error, mouse hover it and import the library
import org.openqa.selenium.chrome.ChromeDriver;
Line 19: You have defined a second test annotated method named launchBrowser(), with a priority set to 0, which will be run after the f() method has run.
Line 21: A variable named expectedTitle of the data type string holds a string value which is the title of the website to be visited
Line 22:
driver.get(“https://intellipaat.com/”)
is used to navigate to a URL provided by you and waits until the page is fully loaded.
Line 23: The title of the site, retrieved by using the driver.getTitle()method, is stored in a string data type variable, named actualTitle
Line 24:
Assert.assertEquals(actual, expected)
will compare the actual value in the first parameter to the expected value in the second parameter and will show the result in the console window, to check whether all the test annotated methods passed or not.
Note: If you see a red line as a sign of error, mouse-over it and import the required library import org.testng.Assert;
Line 26: You are using dependsOnMethod attribute within @Test annotation; we can specify the name of the parent test method on which the test should be dependent, here on method launchBrowser()
Line 27: You have defined a third test annotated method named closeBrowser(), with a priority set to 3, which will be run after the launchBrowser()method has run.
Line 29: driver.quit() will close all the browser windows and end the WebDriver session gracefully.
Step 7: To run your TestNG test case, save it by pressing Ctrl+S and then
- Right-click on the coding window
- Select Run As
- Click on 1 TestNG Test
Step 8: Take a look at the console window. This shows that the methods ran as per the priority assigned to each of the test annotated methods and not in alphabetical order (the default way)
First, the method ‘f’ got executed, then ‘launchBrowser’, and then ‘closeBrowser’
View the HTML Test Report
In this TestNG in Selenium tutorial, till now you must have understood how to perform web application automation testing by creating multiple test annotated method and passing multiple attributes as a parameter in the @Test annotations. Let us move ahead and see how to view the html test report.
Step 1: (1) Right-click on your project name folder (Verification)
(2) Click on Refresh
Step 2: A new test-output folder will be added
- Click on test-output
- Double-click on index.html
Step 3: An html test report is generated, which looks like the image shown below:
Step 4: You can also view the emailable test report by double-clicking on the emailable-report.html under test-output folder, which would look like the image shown below:
Test Execution Flow:
- First, f()launches the browser.
- Then, launchBrowser() verifies the title and matches the actual title with the expected title.
- After that, closeBrowser() closes the browser.
Note:
f(): Launching of the browser is a precondition for every test case.
closeBrowser(): It is a postcondition for every test case.
Hence, we have two annotations which serve the same purpose, and they are @BeforeMethod and @AfterMethod.
- @BeforeMethod: The annotated method will be run before each test method. A method annotated with @BeforeMethod annotation is a good place to initialize some setup which can be used and eventually altered by the @Test annotated method.
- @AfterMethod: The annotated method will get executed after each test method. The @AfterMethod annotated method is a handy place to clean up the setup created (like the initialization of the browser) in the @BeforeMethod and updated by the @Test method.
(1) verifyTitle2() with priority 1 will be run first.
(2) verifyTitle1() with priority 2 will be run second.
Code Explanation
Line 11: Global object (here, driver) declaration which is of type WebDriver class; we need a global declaration of driver-object so that all the test-annotated methods can perform driver-driven actions efficiently.
Note: If it shows an error, mouse-over the red line. A suggestions list appears, from which you have to import the TestNG WebDriver library, import org.openqa.selenium.WebDriver;
Line 13: A method launchBrowser() defined under @BeforeMethod annotation. The method launchBrowser() annotated with @BeforeMethod annotation is a good place to initialize some setup which can be used and eventually altered by the @Test annotated method.
Note: If you see a red line as a sign of error, mouse-over it and import the import org.testng.annotations.BeforeMethod; test annotation library.
Line 14: The System.setProperty(key,value) method, sets the system property key (here, webdriver.chrome.driver) to have the value,
C:\\Users\\Intellipaat-Team\\Downloads\\driver\\chromedriver.exe
In Selenium, you use this method because the browser doesn’t have a built-in server to run the automation code, so you will need a Chrome/IE/Gecko (according to your requirement) driver server for communicating your Selenium code to the browser.
Note: At the end of the path location of the WebDriver, in the value parameter, make sure to add (path…\chromedriver.exe”);
Line 19: A method named verifyTitle1() is annotated under the test annotation with priority set to 2.
Note: If you see a red line as a sign of error, mouse-over it and import the import org.testng.annotations.Test; test annotation library.
Line 20: driver.get(“https://intellipaat.com/”)used to navigate to a URL provided by you and waits until the page is fully loaded.
Line 21:
Assert.assertEquals((“Online Professional Training Courses and and Certificates - Intellipaat”,driver.getTitle())
matches the actual title(string passed in the first parameter) with the expected title(string received at the run time using driver.getTitle()in the second parameter).
Note: If you see a red line as a sign of error, mouse-over it and import the required library import org.testng.Assert;
Line 25: A method named verifyTitle2() is annotated under the test annotation with priority set to 1.
Note: Do make a note that by default the TestNG test cases are run in alphabetical order, to control the flow of test cases we used priority attribute, here, method verifyTitle2() with priority equal to 1 will be executed before verifyTitle1() whose priority was 2.
Line 31: A method closeBrowser() defined under @AfterMethod annotation. The method closeBrowser() annotated with @AfterMethod is a good place to clean up the setup created (like the initialization of the browser) and used to close the chrome browser currently in focus.
Test Execution Flow:
Result in Console Window:
Note: It shows only two test cases in the console, but the execution flow is of six steps i.e. launchBrowser() -> verifyTitle2()-> closeBrowser()-> launchBrowser()-> verifyTitle1()-> closeBrowser()
Now, let’s see the implementation of the other two test annotations which are @BeforeClass and @AfterClass.
- @BeforeClass: The annotated method that runs before the first test method in the current class is invoked. It is a good place to do any initialization or configuration setup which is common to all test methods and eventually may be used in those test methods.
- @AfterClass: The annotated method will be run after all the test methods in the current class have been run. It provides a handle to do some cleanups after all tests in the current class have been executed.
The result in the console window looks like this:
Test Execution Flow:
Let us advance in the TestNG in the Selenium tutorial and see the other set of test annotations @BeforeTest and @AfterTest:
@BeforeTest: The annotated method will be run before any test method present in all those classes mentioned inside the <test> tag is run.
@AfterTest: The annotated method will be run after all the test methods present in all those classes which are inside the <test> tag in the .xml file have run.
The result in the console window looks like this:
Note the difference between @BeforeTest and @BeforeMethod:
- @BeforeTest: It will be called only once before any test methods, no matter how many test-annotated methods are present within the class.
- @BeforeMethod: It will be invoked before every test method; if you have 10 test-annotated methods, it will be called 10 times.
Group Multiple Test Cases
TestNG in Selenium allows us to group several tests. We can group certain tests based on what behavior/aspect they are testing. We may have a scenario where a few tests belong to a certain group (say, sanity) and others belong to another group (say, regression), and yet another one belongs to yet another group (say, functional). With this approach, we may decide to execute only a certain group of tests and skip other ones (only the sanity-related code is required there, so it is preferred to execute only the sanity group-related tests). Several tests may belong to a group, and a test can be included in multiple groups. Let’s begin.
Step 1: Create an XML file in the same package by right-clicking on the package name
Step 2: Click on NewStep 3: Click on OtherStep 4: Select TestNG and then FinishStep 5:
- Type the Class name (say, checkTest1)
- Type the XML suite file name (say, grouping.xml)
- Click on Finish
Step 6: On the coding window, on the bottom-left side, click on Source instead of Design and add three more tags as shown in the image below:
Step 7: Copy and write the below code in your local system’s Eclipse IDE, which depicts the implementation of @BeforeGroups and @AfterGroups annotations
package VerifyTitle;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
public class checkTest1 {
@BeforeGroups(groups= {"sanity"})
public void login() {
System.out.println("Login Successful.");
}
@Test(groups= {"sanity", "regression"}, priority = -1)
public void search() {
System.out.println("Search Value Entered.");
}
@Test(groups= {"sanity"}, priority = 0)
public void searchFromDropdown() {
System.out.println("Search from dropdown chosen successfully.");
}
@Test(groups= {"regression"}, priority = 1)
public void courseSelect() {
System.out.println("Specific Course Selected.");
}
@AfterGroups(groups = {"sanity"})
public void logout() {
System.out.println("Logged out successfully.");
}
}
Step 8: Right-click on the grouping.xml file (because here, we are executing test cases using XML file), navigate to Run As, and click on 1 TestNG suite. You will see the following result in the console window
Note: It shows only two tests run in the console, but we wrote three test-annotated methods and it did not show any error. Why? Because the group value mentioned in the parameter of the @Test annotation for the courseSelect() test annotated method is a regression that is not the one included in the <include> tag within the <groups> tag of its .xml file (which is sanity), and hence TestNG framework doesn’t identify this method to be a part of the ‘sanity’ group. Thus, it ignores and does not include it for testing, and further, the successive method gets executed readily.
Step 9: Click on 2 groups. We will see regression and sanity groups and the methods defined under their names
Step 10:
- Click on Ignored methods to see which method gets ignored. Here, the courseSelect() method gets ignored.The ignored method is courseSelect().
- Click on the show text to see the methods’ names that passed the execution, here search() and searchFromDropdown()
In this TestNG in Selenium tutorial, let us now understand what is parameterization, the types of parameterization and how you can use it to perform automation testing with the help of example code.
What is Parameterization in TestNG?
Role of Parameterization
In the real world, we always wish that our system works valiantly with different sets of input data. The same scenario happens when it comes to testing. Here again, we need to verify that our system is taking the set of combinations that is expected to support data-driven testing in your automated tests. Here comes the key role of ‘Parameterization’ in automation testing. To pass multiple information to the application at runtime, we need to parameterize our test scripts. This concept achieved by parameterization is termed Data-driven Testing. Parameterization (Data-driven Test) is an execution approach, which lets users run a test case automatically, multiple times, with different input values. This test design will let them read data from storage, for example, from a file or database, rather than using hard-coded values. Since the Selenium WebDriver automation testing tool doesn’t have any inbuilt structure or technique to parameterize the test case, we would use the TestNG testing framework.
Types of Parameterization in TestNG
To make parameterization clearer, we will go through the parameterization options using the most popular testing framework, TestNG.
There are two ways by which we can achieve parameterization in TestNG:
- With the assistance of the Parameter annotation and the TestNG XML file
- With the help of the DataProvider annotation
- Parameters annotation with the Testng.xml file can be provided at the suite or the test level
Let’s study parameterization using TestNG and define parameters at suite and test levels in the Testng.xml file.
To do so, you need to:
- Create an XML file that will store the parameters. In the testng.xml file, we have two attributes for the parameter tag, the name attribute which defines the name of the parameter, and the value attribute defines the value of the parameter.
- In the test, add annotation @Parameters
I hope, by now it is clear how to create a TestNG class with no XML file; if not, then follow these basic steps: Right-click on your package name > New > Other > TestNG > Next > Finish
Now, to create an XML file, follow these steps:
Step 1:
- Type the Class name as checkTest2
- Type the XML suite file as XML
- Click on Finish
Step 2: Add two-parameter tags with name and value attributes, each assigned with some values to them.
Step 3: Copy and paste the below code in your local system’s Eclipse IDE:
package VerifyTitle;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class checkTest2 {
WebDriver driver;
@Parameters({"browse","URL"})
@Test
public void launchBrowser(String browse,String URL) {
switch(browse) {
case "chrome":
System.setProperty("webdriver.chrome.driver","C:\Users\Intellipaat-Team\Downloads\driver\chromedriver.exe");
driver = new ChromeDriver();
break;
case "firefox":
System.setProperty("webdriver.gecko.driver","C:\Users\Intellipaat-Team\Downloads\driver\geckodriver.exe");
driver = new FirefoxDriver();
break;
case "IE":
System.setProperty("webdriver.ie.driver","C:\Users\Intellipaat-Team\Downloads\driver\iexploredriver.exe");
driver = new InternetExplorerDriver();
break;
}
driver.get(URL);
System.out.println(URL);
}}
Step 4: Run the code from the parameterization.xml file, and the index.html test report will look like the image shown below:
- It opens the Chrome browser
- It navigates to ‘https://intellipaat.com/’; the value provided is the one present in one of the parameter tags in the parameterization.xml file
In this TestNG in Selenium tutorial, so far, we learned about why TestNG came into existence, and the answer is that since the Selenium framework did not have its built-in testing framework, we require the help of some external testing framework that will help us generate test reports and simplify all our testing requirements such as functional testing, regression, end-to-end testing, and more. Then we learned what TestNG is all about and its advantages over JUnit. Also, we have seen how the installation of TestNG is done and how to write our first test case.
Later, we studied various TestNG annotations, viewed how to get the HTML test report, saw how to group multiple test cases, and understood what is parameterization. We can also integrate Selenium IDE with various search engines to automate testing.