Selenium with C# 63 - Selenium Data Driven Testing using excel in MS Test and EPPlus library
Вставка
- Опубліковано 6 вер 2024
- Data driven testing using excel EPPlus library
Create a utility method which reads excel file & returns data row by row
What is EPPlus library
EPPlus is a .NET library
Reads and writes Excel files using the Open Office XML format (*.xlsx)
ExcelPackage class
Represents an Excel XLSX file package.
This is the top level object to access all parts of the document.
Has a FileInfo parameterized
ExcelWorksheet class Represents an Excel worksheet and provides access to its properties and methods
Cells Property
ReadOnly property
Returns a cell value when we provide Row and Column parameters
ReadExcel() Method Code:
public static IEnumerable lessthan object[]greaterthan ReadExcel()
{
using (ExcelPackage package = new ExcelPackage(new FileInfo("data.xlsx")))
{
//get the first worksheet in the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];
int rowCount = worksheet.Dimension.End.Row; //get row count
for (int row = 2; row lessthan= rowCount; row++)
{
yield return new object[] {
worksheet.Cells[row, 1].Value?.ToString().Trim(), // First name
worksheet.Cells[row, 2].Value?.ToString().Trim(), // Last name
worksheet.Cells[row, 3].Value?.ToString().Trim() // Enrollment date
};
}
}
}
Test method code:
[DynamicData(nameof(ReadExcel), DynamicDataSourceType.Method)]
[TestMethod]
public void DataDrivenTestingUsingExcelSheet(string fName, string lName, string eDate)
{
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Url = "uitestpractice....";
driver.FindElement(By.Id("FirstName")).SendKeys(fName);
driver.FindElement(By.Id("LastName")).SendKeys(lName);
driver.FindElement(By.Id("EnrollmentDate")).SendKeys(eDate);
driver.FindElement(By.XPath("//input[@type='submit']")).Click();
driver.Quit();
}
Possible Interview Questions
How to achieve the data driven testing using excel sheet
What is EPPlus library?