我在尝试运行数据驱动测试时遇到了以下问题,并试图解决这个问题,但似乎没有什么对我有用。
请你看一下我的代码,告诉我哪里出了问题,怎么做才能纠正它。
[Test]
public void ExecuteTest()
{
//Import from excel
ExcelLib.PopulateInCollection(@"C:\Users\CUBA\Desktop\Book1.xlsx");
//Login to app
EALoginPage loginPage = new EALoginPage();
EAFormPage formPage = loginPage.Login(ExcelLib.ReadData(1, "UserName"), ExcelLib.ReadData(1, "Password"));
PropertiesCollection.Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
formPage.FillUserForm(ExcelLib.ReadData(1, "Initial"), ExcelLib.ReadData(1, "MiddleName"), ExcelLib.ReadData(1, "FirstName"));
}下面是我的excelLib.cs课程
private static DataTable ExcelToDataTable(string fileName)
{
//open file and returns as Stream
FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
//Createopenxmlreader via ExcelReaderFactory
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx
// excelReader.IsFirstRowAsColumnNames = true;
var result = excelReader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
//Return as DataSet
//DataSet result = excelReader.AsDataSet();
//Get all the Tables
DataTableCollection table = result.Tables;
//Store it in DataTable
DataTable resultTable = table["Sheet1"];
//return
return resultTable;
}
static List<Datacollection> dataCol = new List<Datacollection>();
public static void PopulateInCollection(string fileName)
{
DataTable table = ExcelToDataTable(fileName);
//Iterate through the rows and columns of the Table
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 0; col <= table.Columns.Count; col++)
{
Datacollection dtTable = new Datacollection()
{
rowNumber = row,
colName = table.Columns[col].ColumnName,
colValue = table.Rows[row - 1][col].ToString()
};
//Add all the details for each row
dataCol.Add(dtTable);
}
}
}
public static string ReadData(int rowNumber, string columnName)
{
try
{
//Retriving Data using LINQ to reduce much of iterations
string data = (from colData in dataCol
where colData.colName == columnName && colData.rowNumber == rowNumber
select colData.colValue).SingleOrDefault();
//var datas = dataCol.Where(x => x.colName == columnName && x.rowNumber == rowNumber).SingleOrDefault().colValue;
return data.ToString();
}
catch (Exception e)
{
return null;
}
}
}
public class Datacollection
{
public int rowNumber { get; set; }
public string colName { get; set; }
public string colValue { get; set; }
}}
任何帮助都将不胜感激!
发布于 2018-08-28 18:39:48
你的问题似乎就在这里
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 0; col <= table.Columns.Count; col++)
{
Datacollection dtTable = new Datacollection()
{
rowNumber = row,
colName = table.Columns[col].ColumnName,
colValue = table.Rows[row - 1][col].ToString()
};
//Add all the details for each row
dataCol.Add(dtTable);
}
}您应该仔细检查索引,因为例如,这个循环
for (int col = 0; col <= table.Columns.Count; col++)将执行超过列计数的一次。假设表中有2列。您的循环从0开始,然后是1,最后是2 (因为2 <= 2),它将构成总共3个循环周期。我相信你在这里会有例外的。
了解有关获取行这里的更多信息。下面是来自后一链接的MSDN文档的示例:
private void PrintRows(DataTable table)
{
// Print the CompanyName column for every row using the index.
for(int i = 0; i < table.Rows.Count; i++)
{
Console.WriteLine(table.Rows[i]["CompanyName"]);
}
}https://sqa.stackexchange.com/questions/35391
复制相似问题