When working with Selenium in C#, generating a comprehensive test report is crucial for documenting the results of your automated tests. You can create a structured report in C# using various frameworks and tools, such as NUnit, ExtentReports, or Allure. Below is a template for a Selenium test report in C#, designed to be integrated with NUnit and ExtentReports.
### **Selenium Test Report Template in C# Using ExtentReports**
#### **1. Install Necessary Packages**
First, you need to install the required NuGet packages:
- **NUnit**: For writing and running tests.
- **Selenium.WebDriver**: For browser automation.
- **ExtentReports**: For generating detailed test reports.
You can install these packages via NuGet Package Manager or using the Package Manager Console:
```powershell
Install-Package NUnit
Install-Package Selenium.WebDriver
Install-Package ExtentReports
```
#### **2. Set Up ExtentReports in Your Project**
Here's how you can integrate ExtentReports into your Selenium tests using C#:
```csharp
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
namespace SeleniumTestReport
{
public class SeleniumTests
{
private IWebDriver driver;
private ExtentReports extent;
private ExtentTest test;
[OneTimeSetUp]
public void Setup()
{
// Initialize ExtentReports
var htmlReporter = new ExtentHtmlReporter("extentReport.html");
extent = new ExtentReports();
extent.AttachReporter(htmlReporter);
// Initialize WebDriver
driver = new ChromeDriver();
}
[Test]
public void TestExample()
{
test = extent.CreateTest("TestExample").Info("Test started");
try
{
// Navigate to the website
driver.Navigate().GoToUrl("https://www.example.com");
test.Log(Status.Info, "Navigated to example.com");
// Check title
string title = driver.Title;
Assert.AreEqual("Example Domain", title);
test.Log(Status.Pass, "Title is correct");
// Capture screenshot on success
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
string screenshotPath = "screenshot.png";
screenshot.SaveAsFile(screenshotPath, ScreenshotImageFormat.Png);
test.AddScreenCaptureFromPath(screenshotPath);
}
catch (AssertionException ex)
{
test.Log(Status.Fail, "Test failed: " + ex.Message);
// Capture screenshot on failure
Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();
string screenshotPath = "screenshot.png";
screenshot.SaveAsFile(screenshotPath, ScreenshotImageFormat.Png);
test.AddScreenCaptureFromPath(screenshotPath);
throw;
}
}
[OneTimeTearDown]
public void TearDown()
{
// Close WebDriver
driver.Quit();
// Write the report to the file
extent.Flush();
}
}
}
```
#### **3. Structure of the Report**
Here’s a basic template for what your ExtentReports-based report will include:
---
**# Selenium Test Report**
**Date:** `{{Date of Test Execution}}`
**Test Suite:** `{{Name of Test Suite}}`
**Test Environment:**
- **Browser:** `{{Browser Name and Version}}`
- **OS:** `{{Operating System}}`
- **Resolution:** `{{Screen Resolution}}`
- **Selenium Version:** `{{Selenium Version}}`
- **Test Framework:** `{{Framework Used (e.g., NUnit)}}`
---
## 1. **Summary**
- **Total Tests:** `{{Total Number of Tests}}`
- **Tests Passed:** `{{Number of Passed Tests}}`
- **Tests Failed:** `{{Number of Failed Tests}}`
- **Tests Skipped:** `{{Number of Skipped Tests}}`
- **Execution Time:** `{{Total Execution Time}}`
---
## 2. **Test Results**
| Test Case ID | Test Case Description | Status | Execution Time | Screenshot |
|--------------|-----------------------|---------|----------------|------------|
| `{{ID1}}` | `{{Description1}}` | `Passed`| `{{Time1}}` | `[Link1]` |
| `{{ID2}}` | `{{Description2}}` | `Failed`| `{{Time2}}` | `[Link2]` |
| `{{ID3}}` | `{{Description3}}` | `Skipped`| `{{Time3}}` | `N/A` |
| ... | ... | ... | ... | ... |
---
## 3. **Detailed Test Case Results**
### **Test Case ID:** `{{ID of Failed Test}}`
- **Description:** `{{Description of the test case}}`
- **Steps Executed:**
1. `{{Step 1}}`
2. `{{Step 2}}`
3. `{{Step 3}}`
- **Expected Result:** `{{Expected Result}}`
- **Actual Result:** `{{Actual Result}}`
- **Error Message/Log:**
```
{{Error log or stack trace}}
```
- **Screenshot:** ![Screenshot](path/to/screenshot.png)
---
## 4. **Conclusion**
- **Overall Result:** `{{Overall result summary, e.g., Passed/Failed with X issues}}`
- **Next Steps:** `{{Recommendations for next steps, e.g., Retesting, Fixes Required}}`
---
### **How to Use and Extend the Report**
- **Adding More Information**: You can log additional information, such as browser console logs or network activity, using `test.Log(Status.Info, "message")` in ExtentReports.
- **Customizing Reports**: ExtentReports allows you to customize the look and feel of the report by using different themes, charts, and more.
- **Automatic Generation**: You can integrate this setup with CI/CD pipelines (e.g., Jenkins, Azure DevOps) to automatically generate and publish reports.
### **Generating and Viewing Reports**
After running your tests, you can view the report by opening `extentReport.html` in a web browser. This file will contain all the details of your test execution, including screenshots and logs.
### **Conclusion**
This template provides a solid foundation for generating detailed and visually appealing Selenium test reports in C#. By using NUnit and ExtentReports, you can easily log test results, capture screenshots, and provide detailed feedback to stakeholders, all within a single, automated process.
댓글