Create a PDF report with screenshots and logs using Selenium WebDriver in Java

Create a PDF report with screenshots and logs using Selenium WebDriver in Java

Creating a PDF report with screenshots and logs using Selenium WebDriver in Java involves several steps. You'll need to use additional libraries like iText PDF for PDF generation and log4j for logging. Here's a step-by-step guide on how to achieve this:


**Step 1: Set up your Java project**


Create a new Java project or use an existing one. Make sure you have Selenium WebDriver and other necessary dependencies added to your project.


**Step 2: Add dependencies**


You'll need the following dependencies in your project:


- Selenium WebDriver: For browser automation.

- iText PDF: For generating PDF reports.

- log4j: For logging.


You can add these dependencies to your project using a build tool like Maven or by downloading JAR files manually and adding them to your classpath.


**Step 3: Configure log4j**


Create a log4j configuration file (e.g., `log4j.xml`) to configure logging settings. Place this file in your project's resources folder. Here's an example configuration:


```xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="fileAppender" class="org.apache.log4j.FileAppender">

        <param name="File" value="logs/test.log" />

        <layout class="org.apache.log4j.PatternLayout">

            <param name="ConversionPattern" value="%d %-5p [%c] %m%n" />

        </layout>

    </appender>


    <root>

        <priority value="debug" />

        <appender-ref ref="fileAppender" />

    </root>

</log4j:configuration>

```


Make sure to adjust the log file path and logging settings as needed.


**Step 4: Create a test script**


Write your Selenium WebDriver script that performs the actions you want to log and capture screenshots of. Also, add logging statements throughout your script to capture relevant information.


Here's a simplified example:


```java

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.apache.log4j.Logger;


public class SeleniumPDFReport {

    private static final Logger logger = Logger.getLogger(SeleniumPDFReport.class);


    public static void main(String[] args) {

        // Set the path to the ChromeDriver executable

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");


        // Initialize WebDriver

        WebDriver driver = new ChromeDriver();


        // Open a website

        driver.get("https://example.com");


        // Log a message

        logger.info("Opened the website: https://example.com");


        // Capture a screenshot

        // Implement code to take a screenshot and save it


        // Perform other actions and log as needed


        // Close the WebDriver

        driver.quit();

    }

}

```


**Step 5: Generate a PDF report**


After executing your test script, you can generate a PDF report containing logs and screenshots. You'll need to use the iText PDF library to create the PDF.


Here's a simplified example of how to generate a PDF report:


```java

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.PdfWriter;


import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;


public class PDFReportGenerator {

    public static void main(String[] args) {

        // Create a new document

        Document document = new Document();


        try {

            // Define the path where you want to save the PDF report

            String outputPath = "path/to/report.pdf";

            PdfWriter.getInstance(document, new FileOutputStream(outputPath));


            // Open the document

            document.open();


            // Add content to the document

            document.add(new Paragraph("Selenium Test Report"));

            document.add(new Paragraph("==================================="));


            // Add logs and screenshots to the document

            // Implement code to read logs and add them to the PDF

            // Implement code to add screenshots to the PDF


            // Close the document

            document.close();


            System.out.println("PDF report generated successfully at: " + outputPath);

        } catch (DocumentException | IOException e) {

            e.printStackTrace();

        }

    }

}

```


In this example, you would need to implement the code to read logs and add them to the PDF, as well as the code to add screenshots to the PDF. You can use libraries like log4j to read log files and capture screenshot files from your Selenium script's output directory.


**Step 6: Execute your test script and generate the PDF report**


Run your Selenium WebDriver script, which logs actions and captures screenshots. After the script execution is complete, run the PDFReportGenerator class to create the PDF report.


This is a simplified example, and in a real-world scenario, you would need to handle exceptions, format the report, and properly organize the content within the PDF document.


Note: You may need to adapt the code and dependencies based on your specific requirements and project structure.

Comments

Popular posts from this blog

Log4j2 configuration for Selenium Framework

Building Cucumber BDD Framework from Scratch using Selenium and TestNG