Hello friends, this tutorial provides you with a practical overview of how to generate Extent reports in Selenium with Python, Java, and C#. In this guide, we’ll first explore what is Extent report and what benefits it offers. Both Python and Java provide two distinct ways to generate the Exten reports, so we’ll be covering these aspects in detail. Let’s get into some action now.
Learn About Reporting in Selenium
Extent Reports is a flexible reporting library for Selenium that allows you to create detailed and beautiful HTML reports for your test executions. It provides insights into test pass/fail status, logs, screenshots, and more.
Must Read:
Let’s now start by understanding what Extent Reports are and their benefits. After that, we’ll explore multiple ways to generate Extent Reports in Python, Java, and C#.
What are Extent Reports?
Extent Reports, a handy tool for Selenium WebDriver, makes it easy to create detailed HTML reports. These reports not only have important info but also come with interactive charts and graphs for better understanding. Stakeholders like them because they help interpret and analyze test results efficiently.
Key Benefits of Extent Reports
It has numerous benefits, let’s look at a few important ones.
1. User-Friendly Reports: Extent Reports generate HTML reports with interactive charts and graphs, offering an intuitive interface for in-depth test result analysis.
2. Detailed Information: Reports include pass/fail status, test steps, logs, screenshots, and more, making it easy to identify and diagnose issues quickly.
3. Historical Tracking: Extent Reports maintain historical data, enabling users to track and analyze test execution trends over time, supporting better decision-making and continuous improvement.
4. Seamless Selenium Integration: The tool effortlessly integrates with Selenium WebDriver, making it a top choice for streamlined test automation. This integration ensures a smooth workflow and boosts overall testing process efficiency.
Now, let’s explore multiple ways to generate Extent Reports in both Python and Java.
Setting Up Your Project
Before we get started, ensure you have the necessary tools installed:
- Python (for Python examples)
- Java JDK and Maven (for Java examples)
- Selenium WebDriver
You’ll also need to install the Extent Reports library. For Python, use:
pip install extentreports
For Java, add the following dependency to your Maven project:
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.1.5</version> <!-- Check for the latest version on Maven Central -->
</dependency>
Generating Extent Reports in Python
Simplifying Extent Reports in Python is easy with two options. First, use the Extent Report Python Library for straightforward setup and detailed HTML reports with essential logging. Or, go for pytest
and pytest-html
, ensuring simplicity in test case management and providing clean HTML reports. Both options prioritize a hassle-free experience, ensuring clarity and efficiency in automating tests. Pick the one that aligns with your project needs and preferences for the best impact.
Using Python Extent Report
Let’s talk about Option 1: Using the Extent Report Python Library. This method is clean and simple. You set it up easily and generate detailed HTML reports effortlessly. Please note that you should log crucial info for thorough test analysis.
from selenium import webdriver as wd
from extentreports import ExtentTest as ext, ExtentReports as extr
from selenium.webdriver.common.by import By
# Set up Extent Reports
ext = extr("test-report.html")
test = ext.create_test("MyTest", "Description of my test")
# Set up Selenium WebDriver
driver = wd.Chrome()
driver.get("The url of your tetsing site")
# Perform actions
driver.find_element(By.NAME, "username").send_keys("user")
driver.find_element(By.NAME, "password").send_keys("password")
driver.find_element(By.ID, "login-btn").click()
# Log results to Extent Report
test.log(ExtentTest.INFO, "Performed login actions")
# End the test
ext.flush()
driver.quit()
Using PyTest and PyTest HTML
Now, diving into Option 2: Using pytest
and pytest-html
. This approach is straightforward—manage your test cases with simplicity. It offers clean and comprehensive HTML reports, ensuring an efficient and hassle-free experience in your test automation journey.
import pytest
from selenium import webdriver
@pytest.fixture
def setup_teardown():
# Set up Selenium WebDriver
driver = webdriver.Chrome()
yield driver
# Teardown - executed after the test
driver.quit()
def test_example(setup_teardown):
driver = setup_teardown
driver.get("The url of your testing website")
assert "Example" in driver.title
Generating Extent Reports in Java
Creating Extent Reports in Java is a clear process. It also gives us two options. The first one is to just directly integrate the Extent Report Java Library and create tests. As the test finishes, it produces a detailed HTML report. In the second option, we’ll use the TestNG library. It binds with the Extent Report Listener and ensures seamless integration. Once the test ends, it provides an interactive report for reviewing the results.
Using Java Extent Report Library
Let’s talk about Option 1 in Java. Using the Extent Report Java Library is straightforward—integrate it seamlessly, get detailed HTML reports, and simplify test analysis effortlessly.
import org.openqa.selenium.*;
import com.aventstack.extentreports.*;
public class ExtentReportExample {
public static void main(String[] args) {
ExtentReports ext = new ExtentReports();
ExtentTest test = ext.createTest("MyTest", "Desc of my test");
WebDriver driver = new ChromeDriver();
driver.get("The url of your testing site");
// Perform actions
explore_link_locator = By.linkText("Explore")
subscribe_button_locator = By.id("subscribe-btn")
// Perform unique actions using local variables
driver.findElement(explore_link_locator).click();
driver.findElement(subscribe_button_locator).click();
// Log results to Extent Report
test.log(Status.INFO, "Performed valid actions");
// End the test
ext.flush();
driver.quit();
}
}
Using TestNG / Extent Report Listener
Now, let’s explore Option 2 in Java. It incorporates TestNG and Extent Report Listener. This choice ensures a smooth integration, providing interactive HTML reports for efficient test analysis without unnecessary complexity.
import org.openqa.selenium.*;
import org.testng.annotations.*;
import com.aventstack.extentreports.*;
public class ExtentReportExampleTestNG {
private WebDriver driver;
private ExtentReports ext;
private ExtentTest test;
@BeforeMethod
public void setup() {
ext = new ExtentReports();
test = ext.createTest("MyTest", "Desc of my test");
driver = new ChromeDriver();
}
@Test
public void testExample() {
driver.get("The url of your testing site");
search("Selenium");
}
@AfterMethod
public void tearDown() {
ext.flush();
driver.quit();
}
private void search(String seachStr) {
// Store long parameters in local variables
exp_link_loc = By.linkText(seachStr)
sub_btn_loc = By.id("subscribe-btn")
// Perform unique actions using local variables
driver.findElement(exp_link_loc).click();
driver.findElement(sub_btn_loc).click();
test.log(Status.INFO, "Performed valid actions");
}
}
Choose the option that best fits your project’s requirements and integrates seamlessly with your testing framework. Extent Reports provide a powerful way to visualize and share test results, enhancing the overall testing process.
Generate Extent Reports in C#
To generate Extent Reports in C# using NUnit, you can follow these detailed steps:
1. Create a new NUnit project:
Start by creating a new NUnit project in Visual Studio. Ensure that NUnit and NUnit3TestAdapter NuGet packages are installed.
2. Add ExtentReports NuGet package:
Install the ExtentReports NuGet package to your NUnit project. In your project, go to Solution Explorer, select “Manage NuGet Pkg,” and search for “ExtentReports.”
3. Set up the ExtentReports configuration:
Create a class for setting up the ExtentReports configuration. This class will include methods to initialize and configure ExtentReports, start and end tests, and log information.
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
public class ExtentManager
{
private static ExtentReports ext;
private static ExtentTest test;
public static ExtentReports GetExtent()
{
if (ext == null)
{
var htmlReporter = new ExtentHtmlReporter("ExtentReport.html");
ext = new ExtentReports();
ext.AttachReporter(htmlReporter);
}
return ext;
}
public static ExtentTest CreateTest(string testName, string testDescription)
{
test = ext.CreateTest(testName, testDescription);
return test;
}
public static void Log(Status status, string message)
{
test.Log(status, message);
}
public static void Flush()
{
ext.Flush();
}
}
4. Write NUnit test classes:
Create NUnit test classes and use the ExtentManager to set up and log ExtentReports information.
using NUnit.Framework;
[TestFixture]
public class ExtentReportExample
{
[Test]
public void TestExample()
{
ExtentManager.GetExtent();
var test = ExtentManager.CreateTest("TestEx", "Desc of my test");
// Your test logic here
test.Log(Status.Info, "Performed test actions");
// Log a failure (for demonstration purposes)
// test.Log(Status.Fail, "Test failed");
ExtentManager.Flush();
}
}
5. Run NUnit tests:
Run your NUnit tests using a test runner like NUnit Console Runner or NUnit Visual Studio Adapter.
6. View the Extent Report:
After test execution, find the generated ExtentReport.html file in the project directory. Open it in a web browser to view the Extent Report.
By following these steps, you’ll have a basic setup for generating Extent Reports in C# using NUnit. Customize and expand upon this framework based on your specific project needs and reporting requirements.
How to Get the Extent Report Path
After generating Extent Reports, it’s crucial to know where to find them for future reference or sharing.
The first thing to know here is what happens when you don’t set any path for the report files. Let’s check that out first.
Default Extent Report Path
If you do not specify a path when creating an ExtentReports
instance, your project will default to saving the report files in the current working directory. It’s important to be aware of this default behavior to ensure you can easily locate your generated Extent Reports.
To get the default behavior, you just need to create the ExtentReports object without passing any report path. This will ensure all report files are saved to your project dir. You can expect this behavior to remain the same across all programming languages.
However, you can also provide a path of your choice to save the reports. Here’s how you can set and get the Extent Report path in different programming languages:
Get Extent Report Path in Python
When using Python to generate the report, use the below code to set the report path correctly.
from extentreports import ExtentReports
# Specify the dir for Extent Reports in Python
ext_rpt_path = "reports/test-report.html"
# Create an instance of the report class and set the path
ext = ExtentReports(ext_rpt_path)
# ... (perform your test actions)
# End the test and flush the report
ext.flush()
Get Extent Report Path in Java
Similarly, in Java, you can set and get the extent report path by passing the path to the ExtentReports constructor.
import com.aventstack.extentreports.ExtentReports;
public class ReportExample {
public static void main(String[] args) {
// Set the dir for Saving Reports in Java
String rptPath = "reports/test-report.html";
// Set the path while creating the report instance
ExtentReports ext = new ExtentReports();
ext.attachReporter(rptPath);
// ... (perform your test actions)
// End the test and flush the report
ext.flush();
}
}
Get Extent Report Path in C#
Setting the extent report path in C# is no different than the previous two languages. Check the below C# code.
using AventStack.ExtentReports;
class ReportExample
{
static void Main()
{
// Specify the dir for Saving the Reports in C#
string rptPath = "reports/test-report.html";
// Create an instance of the report class and set the path
var ext = new ExtentReports();
ext.AttachReporter(rptPath);
// ... (perform your test actions)
// End the test and flush the report
ext.Flush();
}
}
Wrapping Up
Congratulations! You’ve learned how to integrate Extent Reports into your Selenium tests using Python and Java. These visually appealing reports will enhance your test automation, providing detailed insights into test execution.
Remember to explore the official Extent online doc for advanced features and customization options.
Happy Testing,
Team TechBeamers