首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决使用Selenium OutOfRangeException C#的问题

如何解决使用Selenium OutOfRangeException C#的问题
EN

Stack Exchange QA用户
提问于 2018-08-28 17:08:54
回答 1查看 190关注 0票数 2

我在尝试运行数据驱动测试时遇到了以下问题,并试图解决这个问题,但似乎没有什么对我有用。

请你看一下我的代码,告诉我哪里出了问题,怎么做才能纠正它。

代码语言:javascript
复制
[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课程

代码语言:javascript
复制
    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; }
    }

}

任何帮助都将不胜感激!

EN

回答 1

Stack Exchange QA用户

发布于 2018-08-28 18:39:48

你的问题似乎就在这里

代码语言:javascript
复制
 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);
            }
        }

您应该仔细检查索引,因为例如,这个循环

代码语言:javascript
复制
for (int col = 0; col <= table.Columns.Count; col++)

将执行超过列计数的一次。假设表中有2列。您的循环从0开始,然后是1,最后是2 (因为2 <= 2),它将构成总共3个循环周期。我相信你在这里会有例外的。

了解有关获取行这里的更多信息。下面是来自后一链接的MSDN文档的示例:

代码语言:javascript
复制
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"]);
    }
}
票数 0
EN
页面原文内容由Stack Exchange QA提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://sqa.stackexchange.com/questions/35391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档