Skip to content Skip to navigation
Check Our New Demo Website!

TestNG Tutorials

Execute Testng.xml using batch file

In previous tutorial we have seen executing testng.xml tests from command line. Now here we will look into executing testng.xml using batch file (.bat) file. A batch file (.bat) is used in DOS and Windows, which is an unformatted text file that consists of a series of commands to be executed by the command line interpreter.

Let us jump into simple example by taking two classes each has three or more @Test methods.

Customize TestNG html Report

When ever weexecute tests with TestNG, it will generate a default html report with basic information.

TestNG has provided an ability to implement 'IReporter' an interface which can be implemented to generate a customized report by users. It has 'generateReport()' method which will be invoked after all the suite has completed its execution and gives the report into the specified output directory.

Syntax for generateReport() method:


Optional annotation in TestNG

As we know, we can pass parameter values to the test methods during run time from testng xml file by specifying Parameters annotation to test method.

To do this, we need to declare parameters tag in xml file using 'name' and 'value' attribute.Where the name attribute of the tag defines name of the parameter and the value attribute defines the value of the parameter.

Passing data to DataProvider from Excel sheet

In this example we will see how to pass the data to Dataproviders by reading the data from excel sheet. DataProvider helps to send multiple sets of data to a test method. But here we need to make sure that the array returned by the dataprovider should match with the test method parameters.

We will write a simple program in which we will validate login screen by taking multiple usernames and passwords. The annotated method must return object[][] where each object[] can be assigned to the test method one as username and the other parameter as password.

Take Screenshot and place it in a folder with Test Class name

In this tutorial, we will see taking a screen shot with test name and placing it in a folder by creating a folder with Test Class Name.

As we are taking screen shots on failure, we need to add logic in creating a screen shot and naming it with test name and place it in its test class name respectively.

We can get the test class name using 'result.getInstanceName()'. Test class name looks some thing like "com.pack.sample.HomePage". But we need to create a folder name with test class name i.e 'HomePage'.

Retry executing only Failed Tests using TestNG

There may be many reasons for a Test case getting failed, may be due to element not found or time out exception or stale element exception etc. Normally in automation after executing scripts/tests, we will check for the results and if the test fails because of above reasons we will re-run then again.

Instead of that we can ask testNG to execute the failed test cases again for X (we can define) number of times and check for the updated results.

Assertions in TestNG

What is Assertion????
Asserts helps us to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception.

Here we are verifying if the page title is equal to 'Google' or not. If the page title is not matching with the text / title that we provided, it will fail the test case.

To explain you what is assertion, lets us look into below code sample


Subscribe to RSS - TestNG Tutorials