public void CreateCustomerData(ref ISelenium browser) { try { var admincommonObj = new AdminSuite.Common(); string user, password = "******"; _testDataFilePath = _currentDirPath.Parent.FullName; _app = new Application(); //var testDataObj = new TestData(); _app.DisplayAlerts = false; _workBook = _app.Workbooks.Open(_testDataFilePath + "\\TestData\\TestData.xls", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); _workSheets = _workBook.Worksheets; var _userSheet = (Microsoft.Office.Interop.Excel.Worksheet)_workSheets.get_Item("Users"); Random rnd = new Random(); int rndNumber; for (int i = 0; i < 10; i++) { rndNumber = rnd.Next(10000); user = "******" + rndNumber; admincommonObj.CreateCustomer(browser, user, password); _userSheet.Cells[i + 2, "B"] = user; _userSheet.Cells[i +2, "C"] = password; admincommonObj.PerformManualAdjustment(browser, user, "Test Accounts", "100", "Yes", "Test Accounts", "", "", "", "Today"); _workBook.Save(); } } catch (Exception Ex) { Framework.BaseTest.CaptureScreenshot(browser, ""); Console.WriteLine(Ex.StackTrace); Framework.BaseTest.Fail(Ex.Message); } finally { _workBook.Save(); _workBook.Close(); _workSheet = null; _workBook = null; _app.Quit(); } }
/// <summary> /// Converts a data table to an excel file /// </summary> /// <param name="dt">Data table to be converted</param> /// <param name="fileName">File path along with file name</param> /// <param name="sheetName">sheet name to be specified</param> private static void Excel_FromDataTable(System.Data.DataTable dt, string fileName, string sheetName) { _app = new Application(); Workbook workbook = _app.Workbooks.Add(true); // Adding column headings with existing column headers int iCol = 0; foreach (DataColumn c in dt.Columns) { iCol++; _app.Cells[1, iCol] = c.ColumnName; } // for each row of data int iRow = 0; foreach (DataRow r in dt.Rows) { iRow++; // add each row's cell data... iCol = 0; foreach (DataColumn c in dt.Columns) { iCol++; _app.Cells[iRow + 1, iCol] = r[c.ColumnName]; } } _workSheet = (Worksheet)workbook.Worksheets.get_Item(1); _workSheet.Name = sheetName; // Global missing reference for objects we are not defining... object missing = System.Reflection.Missing.Value; //Delete the file if already exists if (File.Exists(fileName)) { File.Delete(fileName); } //Saving the work book as .xls workbook.SaveAs(fileName, XlFileFormat.xlWorkbookNormal, missing, missing, false, false, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); _app.Quit(); }
/// <summary> /// Fills the TestData.xls file based from Events,market and selections files based on Mapping.xls /// </summary> public void MapTestDataXls() { try { _testDataFilePath = _currentDirPath.Parent.FullName; System.Data.DataTable mappingDataTable = XlsReader.LoadExcelData(_testDataFilePath + "\\TestData\\FillUploadFiles\\Mapping.xls", "Events"); System.Data.DataTable eventsDataTable = XlsReader.LoadExcelData(_testDataFilePath + "\\TestData\\UploadFiles\\EVENTS.xls", "EVENTS"); System.Data.DataTable marketsDataTable = XlsReader.LoadExcelData(_testDataFilePath + "\\TestData\\UploadFiles\\market.xls", "market"); System.Data.DataTable selectionsDataTable = XlsReader.LoadExcelData(_testDataFilePath + "\\TestData\\UploadFiles\\SELECTIONS.xls", "SELECTIONS"); _app = new Application(); var testDataObj = new TestData(); _app.DisplayAlerts = false; _workBook = _app.Workbooks.Open(_testDataFilePath + "\\TestData\\TestData.xls", 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); _workSheets = _workBook.Worksheets; //GUITestData,BetSlipTestData,GUITestData,Others for (int i = 0; i < mappingDataTable.Rows.Count; i++) { string sourceFile = mappingDataTable.Rows[i]["Source"].ToString(); string sourceSheet = mappingDataTable.Rows[i]["SourceSheet"].ToString(); int sourceRow = Convert.ToInt32(mappingDataTable.Rows[i]["SourceRow"]); string destinationFile = mappingDataTable.Rows[i]["Destination"].ToString(); string destinationSheet = mappingDataTable.Rows[i]["DestSheet"].ToString(); int destinationRow = Convert.ToInt32(mappingDataTable.Rows[i]["DestRow"]); _workSheet = (Microsoft.Office.Interop.Excel.Worksheet)_workSheets.get_Item(destinationSheet); Microsoft.Office.Interop.Excel.Range range = _workSheet.UsedRange; int colCount = range.Columns.Count; int rowCount = range.Rows.Count; int colIndexParam1 = 1; int colIndexParam2 = 1; int colIndexParam3 = 1; int colIndexParam4 = 1; int colIndexParam5 = 1; int colIndexParam6 = 1; int colIndexParam7 = 1; switch (sourceFile.ToUpper().Trim()) { case "EVENTS.CSV": if (mappingDataTable.Rows[i]["param1"].ToString().ToUpper().Trim() != "NULL") { _param1 = eventsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param1"].ToString()].ToString(); colIndexParam1 = GetColumnIndex(mappingDataTable.Rows[i]["param1"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam1] = _param1; } if (mappingDataTable.Rows[i]["param2"].ToString().ToUpper().Trim() != "NULL") { _param2 = eventsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param2"].ToString()].ToString(); colIndexParam2 = GetColumnIndex(mappingDataTable.Rows[i]["param2"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam2] = _param2; } if (mappingDataTable.Rows[i]["param3"].ToString().ToUpper().Trim() != "NULL") { _param3 = eventsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param3"].ToString()].ToString(); colIndexParam3 = GetColumnIndex(mappingDataTable.Rows[i]["param3"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam3] = _param3; } if (mappingDataTable.Rows[i]["param4"].ToString().ToUpper().Trim() != "NULL") { _param4 = eventsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param4"].ToString()].ToString(); colIndexParam4 = GetColumnIndex(mappingDataTable.Rows[i]["param4"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam4] = _param4; } if (mappingDataTable.Rows[i]["param5"].ToString().ToUpper().Trim() != "NULL") { _param5 = eventsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param5"].ToString()].ToString(); colIndexParam5 = GetColumnIndex(mappingDataTable.Rows[i]["param5"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam5] = _param5; } if (mappingDataTable.Rows[i]["param6"].ToString().ToUpper().Trim() != "NULL") { _param6 = eventsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param6"].ToString()].ToString(); colIndexParam6 = GetColumnIndex(mappingDataTable.Rows[i]["param6"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam6] = _param6; } break; case "MARKET.CSV": if (mappingDataTable.Rows[i]["param1"].ToString().ToUpper().Trim() != "NULL") { _param1 = marketsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param1"].ToString()].ToString(); colIndexParam1 = GetColumnIndex(mappingDataTable.Rows[i]["param1"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam1] = _param1; } if (mappingDataTable.Rows[i]["param2"].ToString().ToUpper().Trim() != "NULL") { _param2 = marketsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param2"].ToString()].ToString(); colIndexParam2 = GetColumnIndex(mappingDataTable.Rows[i]["param2"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam2] = _param2; } if (mappingDataTable.Rows[i]["param3"].ToString().ToUpper().Trim() != "NULL") { _param3 = marketsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param3"].ToString()].ToString(); colIndexParam3 = GetColumnIndex(mappingDataTable.Rows[i]["param3"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam3] = _param3; } if (mappingDataTable.Rows[i]["param4"].ToString().ToUpper().Trim() != "NULL") { _param4 = marketsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param4"].ToString()].ToString(); colIndexParam4 = GetColumnIndex(mappingDataTable.Rows[i]["param4"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam4] = _param4; } if (mappingDataTable.Rows[i]["param5"].ToString().ToUpper().Trim() != "NULL") { _param5 = marketsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param5"].ToString()].ToString(); colIndexParam5 = GetColumnIndex(mappingDataTable.Rows[i]["param5"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam5] = _param5; } if (mappingDataTable.Rows[i]["param7"].ToString().ToUpper().Trim() != "NULL") { _param7 = marketsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param7"].ToString()].ToString(); colIndexParam7 = GetColumnIndex(mappingDataTable.Rows[i]["param7"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam7] = _param7; } break; case "SELECTIONS.CSV": if (mappingDataTable.Rows[i]["param1"].ToString().ToUpper().Trim() != "NULL") { _param1 = selectionsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param1"].ToString()].ToString(); colIndexParam1 = GetColumnIndex(mappingDataTable.Rows[i]["param1"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam1] = _param1; } if (mappingDataTable.Rows[i]["param2"].ToString().ToUpper().Trim() != "NULL") { _param2 = selectionsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param2"].ToString()].ToString(); colIndexParam2 = GetColumnIndex(mappingDataTable.Rows[i]["param2"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam2] = _param2; } if (mappingDataTable.Rows[i]["param3"].ToString().ToUpper().Trim() != "NULL") { _param3 = selectionsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param3"].ToString()].ToString(); colIndexParam3 = GetColumnIndex(mappingDataTable.Rows[i]["param3"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam3] = _param3; } if (mappingDataTable.Rows[i]["param4"].ToString().ToUpper().Trim() != "NULL") { _param4 = selectionsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param4"].ToString()].ToString(); colIndexParam4 = GetColumnIndex(mappingDataTable.Rows[i]["param4"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam4] = _param4; } if (mappingDataTable.Rows[i]["param5"].ToString().ToUpper().Trim() != "NULL") { _param5 = selectionsDataTable.Rows[sourceRow - 2][mappingDataTable.Rows[i]["param5"].ToString()].ToString(); colIndexParam5 = GetColumnIndex(mappingDataTable.Rows[i]["param5"].ToString(), ref _workSheet, ref colCount); _workSheet.Cells[destinationRow, colIndexParam5] = _param5; } break; default: break; } } } catch (Exception Ex) { Console.WriteLine(Ex.StackTrace); Framework.BaseTest.Fail(Ex.Message); } finally { _workBook.Save(); _workBook.Close(); _workSheet = null; _workBook = null; _app.Quit(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } }