using System;
using System.Data;
using System.IO;
using NUnit.Framework;
using RestSharp;
using Newtonsoft.Json;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using ExcelDataReader;
namespace APITesting
{
[TestFixture]
[Parallelizable(ParallelScope.Fixtures)]
public class APITest
{
private static ExtentReports extent;
private ExtentTest test;
private RestClient client;
[OneTimeSetUp]
public static void Setup()
{
// Initialize Extent Reports
extent = new ExtentReports();
var htmlReporter = new ExtentHtmlReporter("extent-report.html");
extent.AttachReporter(htmlReporter);
}
[SetUp]
public void BeforeTest()
{
// Create a new test in the Extent Report
test = extent.CreateTest(TestContext.CurrentContext.Test.Name);
// Set the base URL of the API
string baseUrl = "https://api.example.com";
// Create a RestSharp client
client = new RestClient(baseUrl);
}
[Test]
[TestCaseSource(nameof(UserData))]
public void TestAPI(UserDataModel data)
{
// Create a RestSharp request
RestRequest request = new RestRequest("/endpoint", Method.POST);
// Set the request body using the data model
string requestBody = JsonConvert.SerializeObject(data);
request.AddParameter("application/json", requestBody, ParameterType.RequestBody);
// Send the request and get the response
IRestResponse response = client.Execute(request);
// Extract the response details
int statusCode = (int)response.StatusCode;
string content = response.Content;
// Deserialize the response content to a data model
var responseModel = JsonConvert.DeserializeObject<ResponseModel>(content);
// Log the response details in the extent report
test.Log(Status.Info, $"API Response Code: {statusCode}");
test.Log(Status.Info, $"API Response Content: {content}");
// Perform assertions or further processing on the response
Assert.AreEqual(200, statusCode, "Unexpected status code");
// Add a pass status to the extent report
test.Pass("API test passed");
}
[TearDown]
public void AfterTest()
{
// End the test in the extent report
extent.EndTest(test);
}
[OneTimeTearDown]
public static void TearDown()
{
// Flush and close the extent report
extent.Flush();
extent.Close();
}
private static IEnumerable UserData()
{
// Path to the Excel file
string excelFilePath = "data.xlsx";
// Open the Excel file
using (var stream = File.Open(excelFilePath, FileMode.Open, FileAccess.Read))
{
// Create the ExcelDataReader
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// Read the Excel data into a DataTable
var result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
// Get the first DataTable from the result
var dataTable = result.Tables[0];
// Iterate over each row in the DataTable
foreach (DataRow row in dataTable.Rows)
{
// Map the row data to the UserDataModel
var data = new UserDataModel()
{
Username = row["Username"].ToString(),
Password = row["Password"].ToString()
};
// Yield the data for each test case
yield return new TestCaseData(data);
}
}
}
}
}
public class UserDataModel
{
public string Username { get; set; }
public string Password { get; set; }
}
public class ResponseModel
{
// Define properties for the response data
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
// Add other properties as needed
}
}
No comments:
Post a Comment