0

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();
        ExcelLib.PopulateInCollection(@"C:\Users\Numg\Desktop\da\Copy_of_webusers.xlsx");
        PropertiesCollections.driver.FindElement(By.Id("web_user_user_id")).SendKeys(ExcelLib.ReadData(2, "Id"));             
        PropertiesCollections.driver.FindElement(By.LinkText("Ok")).Click();
        Thread.Sleep(5000);
    }
    Thread.Sleep(4000);
    PropertiesCollections.driver.FindElement(By.Id("4")).Click();
    Thread.Sleep(4000);
    var option = PropertiesCollections.driver.FindElement(By.Id("aggrupations_653_shares"));
    var SelectElement = new SelectElement(option);
    Thread.Sleep(4000);
    SelectElement.SelectByValue("3");
    Thread.Sleep(4000);
    PropertiesCollections.driver.FindElement(By.CssSelector("#bet-group > #send > i.fa.fa-arrow-right")).Click();
    Thread.Sleep(4000);
    PropertiesCollections.driver.FindElement(By.Id("code")).SendKeys("testQA");
    Thread.Sleep(4000);
    PropertiesCollections.driver.FindElement(By.CssSelector("#new_reduction > input[name=\"commit\"]")).Click();
    Thread.Sleep(4000);
    PropertiesCollections.driver.FindElement(By.LinkText("Clear")).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
       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;
       }
   }
1

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.