Unable to generate a loop that every userid finishes the test passing to the next userid via the Excel.

        PropertiesCollections.driver.FindElement(By.LinkText("Sales Panel")).Click();
        PropertiesCollections.driver.FindElement(By.Id("web_user_user_id")).SendKeys(ExcelLib.ReadData(2, "Id"));             
    var option = PropertiesCollections.driver.FindElement(By.Id("aggrupations_653_shares"));
    var SelectElement = new SelectElement(option);
    PropertiesCollections.driver.FindElement(By.CssSelector("#bet-group > #send > i.fa.fa-arrow-right")).Click();
    PropertiesCollections.driver.FindElement(By.CssSelector("#new_reduction > input[name=\"commit\"]")).Click();

This is the class for Excel

class ExcelLib

       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
       //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["Sheet1"];

       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

   public static string ReadData(int rowNumber, string columnName)
           //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;

It looks like you aren't using a framework (like TestNG, Microsoft unit tests, JUnit, etc.) that supports defining a data source as a class annotation.

If you were, this would be easy - you'd have something like data-source.currentRow["columnName"].value() and the framework would automatically handle your assertions and moving from row to row.

With the setup you're using you'd use a foreach structure to access each record in your list:

foreach Datacollection rowdata in dataCol do
    string userid = rowdata.colValue;
    // everything you intend to do with that userid
    // Your test assertions

This is pretty basic C# programming - foreach will iterate through your static list automatically, but will register to any test harness as one test.

  • What exactly should I use, and where I'm changing it exactly in the code I'll explain – Daniel Shmayovich Apr 3 '17 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.