I am creating some test cases in Selenium WebDriver using C# (I can't use Java as client has said so). These are sample test cases which will be used for presenting a demo to client and senior management, so my intentions are to make the test cases as robust as I can.

Currently, I am using an Excel file as input and this excel file contains information for all the elements which are there on the page under test. So, I will be creating one excel per page/module. Further, I am reading this excel file in a separate class (.cs file) and then using that in another class (.cs file). So, you can say this excel is a kind of object repository for me (earlier I used CSV file but then switched to Excel).

For making it more flexible I added following code in Excel class, where I will first find which column contains a matching element name and then using its Locator value (I have done this, because I want to get rid of the sequence in which elements are added in excel file, so that if some other member adds elements in this file, he/she don't need to care about the order of elements in file and same used in code).


public void GetTestPageElements()

            List<string> rowValue = new List<string> { };

            var ExcelFilePath = "C:\\Dhiman\\Practice-Automation-Form.xls";
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(ExcelFilePath);
            Excel.Worksheet xlWorksheet = xlWorkbook.Worksheets.get_Item("PageElements");
            Excel.Range xlRange = xlWorksheet.UsedRange;
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            for (int j = 1; j <= rowCount; j++)
                rowValue.Add(xlRange.Cells[j, 1].Value2.ToString());
                Console.WriteLine(rowValue[j-1] + "  ");

            for (int i = 0; i < rowValue.Count; i++)
                if (rowValue[i].Equals("Partial Link Test"))
                    LocatorOfPartialLinkTest = xlRange.Cells[i + 1, 2].Value2.ToString();
                if (rowValue[i].Equals("Link Test"))
                    LocatorOfLinkTest = xlRange.Cells[i + 1, 2].Value2.ToString();
                if (rowValue[i].Equals("First name"))
                    LocatorOfFirstName = xlRange.Cells[i + 1, 2].Value2.ToString();
                if (rowValue[i].Equals("Last name"))
                    LocatorOfLastName = xlRange.Cells[i + 1, 2].Value2.ToString();
                if (rowValue[i].Equals("Sex"))
                    LocatorOfSex = xlRange.Cells[i + 1, 2].Value2.ToString();
                if (rowValue[i].Equals("Years of Experience"))
                    LocatorOfExperience = xlRange.Cells[i + 1, 2].Value2.ToString();
                if (rowValue[i].Equals("Date"))
                    LocatorOfDate = xlRange.Cells[i + 1, 2].Value2.ToString();

Excel File:

enter image description here


  1. I fear that if there are 50 or more such elements on a page then I will end up with lots of IF statements, is this a correct and efficient way of doing this?
  2. Will using such a large number of conditions, make my test case slow for execution?
  3. What's the alternative/better way of doing this like using CSV/XML instead of Excel, etc.?
  • Have you thought about using Fitnesse? You can copy/paste from Excel into its Wiki formatm, wrap your columns into fixture field definitions, and run the tests from Fitnesse too
    – raven
    Dec 2, 2015 at 5:30
  • No, but I will read about Fitnesse and Try it too. Thanks for your input.
    – Dhiman
    Dec 2, 2015 at 13:53

3 Answers 3


I found one more way (definitely there can be other ways too) to do this, and using that I don't need to use IF statement and Loop. This has also reduced my lines of code by 50%.

LocatorOfPartialLinkTest = xlRange.Cells[rowValue.IndexOf("Partial Link Test") + 1, 2].Value2.ToString();
LocatorOfLinkTest = xlRange.Cells[rowValue.IndexOf("Link Test") + 1, 2].Value2.ToString();
LocatorOfFirstName = xlRange.Cells[rowValue.IndexOf("First name") + 1, 2].Value2.ToString();    
LocatorOfLastName = xlRange.Cells[rowValue.IndexOf("Last name") + 1, 2].Value2.ToString();

Posting it as an answer, because I found this as an alternate to my existing implementation.


Even many hundreds of if statement will not cause any noticeable slowdown, but you may consider switch/case statement

If your goal is only parsing and reformatting text files, you may consider learning IronPython. It has .NET bindings (so you can create Excel spreadsheets and write to them), and you will learn new skill :-) One of best uss cases for python is flexible text manipulation.

SQLite is flexible and simple SQL (single user) database, if you want simple yet flexible persistent data repository.

Warning: After you learn Python, C# will seem to you clunky. I know that Python did it to me, and I had to switch jobs :-/

  • I can't go with Python as I have to use C# (my manager said so after a long debate over why not using Java :( with Selenium WebDriver). Anyways, you answer is helpful to me and will look for IronPython and Switch/Case Statements, and moreover, it increased my confidence of using IF statements. Thanks :)
    – Dhiman
    Oct 28, 2015 at 17:02
  • Is there any limit or standard that how many IF statements should be used? (I know there will be hardly any such rule), but asked just for sake of my knowledge and curiosity.
    – Dhiman
    Oct 28, 2015 at 17:04
  • Typically a single method should never be larger than a screen. An immense amount of if statements is horrible for upkeep, typically.
    – Paul Muir
    Oct 28, 2015 at 17:30
  • 1
    Hundred of if statements would be ugly code but fully functional, with very little effect on the performance. You may want to wrap all those ifs into single function so there is no extra code unrelated to ifs. Yes, in theory single method should fit on a single screen. Real life problems might be untypically hairy. Complexity grows on you, and sometimes you know you should refactor, but postpone it for valid reasons. In theory, there is no difference between theory and practice. In practice, there is Oct 28, 2015 at 18:14
  • Completely agree with the concept of Theory and Practical mentioned by @PeterMasiar
    – Dhiman
    Oct 28, 2015 at 20:41

I would recommend using LINQ. This will allow you to 'query' the spreadsheet and pull in the data based on these queries. An example using excel can be found here.

As for the execution of it, I would use an enum or array of the different parameters accepted, select those items and then process by the result.

If the test cases grow out of hand (IE millions/billion of rows) than this may have issues iterating over every row, but I wouldn't think this would be a legitimate problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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