How to Save Selenium WebDriver TestNG Result to Excel

In this post, we’ll teach you an easy method for saving Selenium WebDriver TestNG result. So, first of all, we’ll create a basic Java project in Eclipse and add the Selenium WebDriver TestNG packages. Also, we’ll show how to use multiple TestNG <@Test> methods for executing the different testing tasks.

You’ll see how easy it is to generate the TestNG XML using Eclipse and then use it for grouping the multiple tests into a single TestNG suite. More importantly, we’ll list the steps to save Selenium WebDriver TestNG result to excel file. Interestingly, you would be curious to know how we used the <@BeforeClass> and <@AfterClass> TestNG annotations for the purpose of test setup and test cleanup.

But before we proceed further and you too have an interest in viewing the step-by-step detail of setting up a TestNG and Selenium Webdriver project. Then, make sure you read the following blog posts.

We’ve split this post into multiple sections because it is easy to grasp a smaller set of instructions.

How to Save Selenium WebDriver TestNG Result to Excel?

Save Selenium WebDriver TestNG Result in Excel

1- Create a Selenium Webdriver TestNG project in Eclipse.

Start by launching the Eclipse IDE and create a new Java project. Then, name the test project as <TestNGSeleniumDemo>, you can change the name if you wish. Also, add a new package to the project and save it as <com.techbeamers.testng>.

Next, you’ll be adding the latest <Selenium Webdriver standalone jar> to your project. You can download it from the <> website.

Also, we’ve to save the test results into an Excel file which we can do by using the <Apache POI library>. So, download it as well from the <> website.

Consequently, please check that you add both of these <.jar> files as external jars into the <TestNGSeleniumDemo> project. Here is a quick snapshot to help you in doing this.

Save Selenium WebDriver TestNG Result to Excel - Add External Jar Files

Save Selenium WebDriver TestNG Result to Excel – Add External Jar Files

Furthermore, there are three main things you have to do which are as following.

i- First of all, create a <Resources> folder under your <TestNGSeleniumDemo> project.

ii- Then, add a new file and name it as <>.

Just create a file with the specified name and copy/paste the below lines.

iii- Add the <> file. Do the same steps as mentioned in the previous point.


2- Define multiple <@Test> methods.

As we’ve explained above that, we need three test methods to run the following tasks.

  • Opens the TestNG Demo Website for Login Test.
  • Fill the Login Details.
  • Perform Login.

Here is the source code for the above three functions.


3- Write a function to save Selenium WebDriver TestNG report to Excel file.

Now, we’ll write a function to save the results of each step executed into an excel file. For this purpose, we’ll use the Apache POI Apis. You should note that we’ve used the <@AfterClass> annotation for this function. So, it’ll get executed after executing all methods of the TestNG class. Please check out the below source code.

Code Snippet.


4- Consolidate the full source code.

We’ve copied all the code of the three test methods and the function to save results to Excel file into the <> file. We’ve attached the full source code of the file required to run this example. You can directly copy it to your project. There shouldn’t be any error if you would have followed all the steps specified above.

Please make sure you add the <> under the <com.techbeamers.testng> package.

Add <UIMap.Java> from an another TestNG tutorial of this blog.

However, there is one more class file <> which we’ve used in this project. This file reads the user/password from the <> file. Also, it helps in fetching the pre-set locators from the <> file.

You can get the source code of <> from this link on our blog. Once you copy its code, then add this file under the same <com.techbeamers.testng> package in the current project.

Code Snippet.


5- Generate <TestNG.XML> file.

It’s an optional step which you may want to skip. If you ignore, then eclipse will auto-generate the <TestNG.XML> file. But sometimes you need to create it for supplying settings like the test order and parallel execution etc.

Though, you can easily generate the <TestNG.XML> file by right-clicking on the <> file from project explorer in Eclipse. See the below screenshot for proper directions to generate the file. Also, see sample <TestNG.XML> file produced after pressing the TestNG >> Convert to TestNG option.

Save Selenium WebDriver TestNG Result to Excel - Generate TestNG XML File

Save Selenium WebDriver TestNG Result to Excel – Generate TestNG XML File

Now, we are attaching the code of the <TestNG.XML> file created after the above step. You just need to add the include directives for specifying the three test methods that we’d defined in the second section. It will also help in grouping the methods into the TestNG test suite. From the below XML code, you verify that we’ve created a test suite as <SaveTestNGResultToExcel>.


6- Execute Selenium WebDriver TestNG test suite.

To run the example project or the TestNG test suite, just right-click the <TestNG.XML> file and choose the Run as >> TestNG Suite option. A screenshot is available below for your reference.

Save Selenium WebDriver TestNG Result to Excel - Run as TestNG suite

Save Selenium WebDriver TestNG Result to Excel – Run as TestNG suite

After execution, when you would refresh the project, you’ll see the <SaveTestNGResultToExcel.xls> file appearing in the project folder. This file contains all the steps and their pass/fail status. The same is also visible in the above screenshot.


While writing this post, we only thought to come up with something that could add value in the way you currently use the TestNG framework. Hopefully, you now know what to do for archiving the Selenium WebDriver TestNG result in an Excel file.

Even the interviewers ask these types of questions from the automation candidates. So, this post could be quite useful for those who are preparing for the job interviews. However, you can use it in your projects as well.

Finally, if anyone has any suggestions for us, then please use the comment box to share it with us.

All the Best,



  1. studio7t7 Reply
    • Meenakshi Agarwal Reply
  2. Ravindra M Reply
    • Meenakshi Agarwal Reply

Leave a Reply