如何通过测试用例名称传递测试数据

时间:2019-04-09 06:28:57

标签: c# testing automation frameworks

我想使用测试用例名称和测试用例的所有测试数据在同一张表中获取测试数据,但是在我的代码中,我想为测试用例的每个数据提供行号和列名。

命名空间SeleniumBL {     公共类ExcelLibrary     {         公共静态System.Collections.Specialized.NameValueCollection AppSettings { }         静态字符串fileName = ConfigurationSettings.AppSettings [“ ExcelPath”];

    private static DataTable ExcelToDataTable(string fileName, string excelSheet)
    {
        //open file and returns as Stream
        FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
        //Createopenxmlreader via ExcelReaderFactory
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx
                                                                                       //Set the First Row as Column Name
        excelReader.IsFirstRowAsColumnNames = true;
        //Return as DataSet
        DataSet result = excelReader.AsDataSet();
        //Get all the Tables
        DataTableCollection table = result.Tables;
        //Store it in DataTable
        DataTable resultTable = table[excelSheet];

        //return
        return resultTable;
    }

    static List<Datacollection> dataCol = new List<Datacollection>();

    public static void PopulateInCollection(string excelSheet)
    {
        if (!dataCol.Any(x => x.excelSheet == excelSheet))
        {
            DataTable table = ExcelToDataTable(fileName, excelSheet);

            //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()
                    {
                        excelSheet = excelSheet,
                        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
        {
            //var dataabc = (from colData in dataCol
            //               where colData.colName == columnName && colData.rowNumber == rowNumber
            //               select colData.colValue).ToList();

            //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();

            return data.ToString();
        }
        catch (Exception e)
        {
            return null;
        }
    }


}

public class Datacollection
{
    public string excelSheet { get; set; }
    public int rowNumber { get; set; }
    public string colName { get; set; }
    public string colValue { get; set; }
}

}

0 个答案:

没有答案
相关问题