logo logo

Selenium: Generate Customized Excel Reports Using TestNG

main post image

There are many ways to Generate Reports in Selenium. Now I’d like to generate Excel reports.

Note : TestNG is required. Without TestNG this code won’t work.
There are few steps to follow in order to generate Excel reports. Here I am sharing the snippet.

Click here to get 0.0.1 jar (older version).

Download the latest Jar from here ExcelReportGenerator version 4.0.1. Add this Jar into your project BuildPath.

If you’re using Jar 4.0.1 version then you can specify the desired location and File name in the code itself.

Follow the steps specified for creating customized excel reports using TestNG :

Step 1 : Create a Package ‘ExcelResults’ under your Project. (Optional, if you’re using 4.0.1 version).

Step 2 :Create  the testcases for automation testing using TestNg. (by using @Test,BeforeTest,…….) as shown.

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class Test_98 {
@Test(priority = 1)
public void VerfyingTestCaseID_001() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 2)
public void VerfyingTestCaseID_002() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 3)
public void VerfyingTestCaseID_003() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 4)
public void VerfyingTestCaseID_004() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 5)
public void VerfyingTestCaseID_005() {
System.out.println(“test”);
System.out.println(“this”);
Assert.assertEquals(“validText”, “InvalidText”);
}

@Test(priority = 6)
public void VerfyingTestCaseID_006() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 7)
public void VerfyingTestCaseID_007() {
System.out.println(“test”);
System.out.println(“this”);
Assert.assertEquals(“validText”, “InvalidText”);

}

@Test(priority = 8)
public void VerfyingTestCaseID_008() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 9)
public void VerfyingTestCaseID_009() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 10)
public void VerfyingTestCaseID_010() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 11)
public void VerfyingTestCaseID_011() {
System.out.println(“test”);
System.out.println(“this”);
Assert.assertTrue(false);
}

@Test(priority = 12)
public void VerfyingTestCaseID_012() {
System.out.println(“test”);
System.out.println(“this”);
Assert.assertTrue(false);

}

@Test(priority = 13)
public void VerfyingTestCaseID_013() {
System.out.println(“test”);
System.out.println(“this”);
}

@Test(priority = 14)
public void VerfyingTestCaseID_014() {
System.out.println(“test”);
System.out.println(“this”);
}
}

Step3 : Create a testng.xml file under your Project as Shown.

<suite name=”Build 2.0.1″>

<test name=”TestReport”>
<classes>
<class name=”Test_98″ />
</classes>
</test>
</suite>

Now run the testng.xml file.

Step 4 : Now Create a Class ‘ExcelGenerate’  and paste the following code:

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;
public class ExcelGenerate {

public static void main(String[]args) throws ParserConfigurationException, IOException, SAXException
{
ExcelReportGenerator.generateExcelReport(“MyProject.xls”, “D:\\workspace”);

}
}

Step 5 : Refresh the package ‘ExcelResults’ (If you’re using an older version).

Step 6 : Results will be generated at your desired path given. (If you’re using 4.0.1 version).

Click here to see the generated report.

Now you see the excel Report Generated. It’s so simple huh !!!

*If you see the columns in the excel were disturbed, please change the settings or use the latest version. I have made this using  ‘Libre’.


🙂 Did you find this Excel report generation tutorial easy to implement? let us know!


Reference


Hari Charan

About the author

Hari Charan

Join TestProject Community

Get full access to the world's first cloud-based, open source friendly testing community. Enjoy TestProject's end-to-end test automation Platform, Forum, Blog and Docs - All for FREE.

Join Us Now  

Comments

13 2 comments

Leave a Reply

popup image

Selenium for Teams

Sharing and distributing Selenium tests has never been so easy! With TestProject's FREE Selenium based platform, you can finally create awesome tests with the freedom to collaborate with your team effortlessly.
Sign Up Now right arrow
FacebookLinkedInTwitterEmail