// Get the test data in table format and store it in DataRow type. DataTable DTExcelresult = IO.ReadExcel( excelLocation, methodName ); for( int rowNo = 0; rowNo < DTExcelresult.Rows.Count; rowNo++) { // Represents a row of data in a DataTable. System.Data.DataRow resultRows = DTExcelresult.Rows[rowNo]; // Return the tesdata in DataRow type. yield return new Tuple<System.Data.DataRow>( resultRows ); } // end for
public class IO { /// <summary> /// This method will initiate connection to open testdata document using OleDbConnection. /// </summary> /// <param name="excelPath">Path of the test data.</param> /// <returns>Returns the object initiated foe test data connection.</returns> private static OleDbConnection CreateExcelConnection(string excelPath) { // Provides connection information as a string. string oleDbCon = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Jet OLEDB:Engine Type=5;Extended Properties=Excel 12.0;", excelPath); // An instance of the OleDbConnection class is initiated. return new OleDbConnection(oleDbCon); } /// <summary> /// This method will read excel data. /// </summary> /// <param name="excelPath">Path of the test data.</param> /// <param name="methodName">Test method name.</param> /// <returns>Return test data value in DataTable format.</returns> public static DataTable ReadExcel( string excelPath, string methodName) { // 'using' block to dispose the object once the connection is no more needed. using (var excelconnection = CreateExcelConnection(excelPath)) { try { string columnName = "S No"; // Column name 'Sno' is assigned. string excelSheetName = methodName.Split('_')[1]; // Test sheet name for each case has been taken from method name. // Opens the excel connection to read data required for the test method. excelconnection.Open(); // Initiating the DataTable class. DataTable DTExcelresult = new DataTable(); // Fetches the data from excel. OleDbDataAdapter DataAdapter = new OleDbDataAdapter(string.Format("Select * from [{0}$] where UCase([" + columnName + "]) IS NOT NULL ", excelSheetName), excelconnection); // Stores the test data in a table format. DataAdapter.Fill(DTExcelresult); // Ends the excel connection. excelconnection.Close(); // Retun the data in table format. return DTExcelresult; } catch (Exception exception) { // Write the exception to console. Console.WriteLine("Exception", exception); // Return null. return null; } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)