Data Driven testing in Selenium with C#

In this post we will start our discussion with Data Driven testing with Selenium in C#, but this post can well understood only if you have already read the articles listed below

Here is the complete video of the above discussion

Thanks for watching the video and reading the post !!!

Please leave your comments and let me know if there is anything need to be improved in this post.

Karthik KK


  1. Sunday Omale says:

    Please, the link you left for the full code of DDT is not working.
    Kinldy help with a full DDT code with java.
    Thank you

  2. subha anand says:

    Hi Karthik,

    The code in c# .Net is not working. It is throwing IndexOutOfBound exception. Please help.

    • karthik KK says:

      Hi Subha,

      The indexoutofbound exception happens if the row or column is not available in the excel which you are trying to get.

      Can you check whats going on there.

      Karthik KK

  3. subha anand says:

    Hi Karthik,

    The code you have provided in c# .Net is not working. It is throwing System.IndexOutOfRange Exception. cannot find column 3.

    Please help.


      • Ali says:

        Hi Ramon/Subha Anand,

        We can also use

        for (int row =1;row < table.Rows.Count ; row ++)
        for(int row =1 ;row <=table.Rows.Count-1; row ++)

        To avoid getting "System.IndexOutOfBound exception ".

        But it will be good if Karthik can explain how same code is working for him and when we use its throwing exception.


  4. omayer says:

    Hi Karthik,
    Thank you for the The code you have provided in c# .
    Issue w/ xlsx file open as Read-only when i try to update or save the file manually after test run

  5. Sanathan says:

    Hi Karthik,

    Thanks for the posting.
    I am currently running multiple rows with different data.

    I have a scenario and need some guidance, if one row errors out, the selenium script should take us to the next row.
    Can you help how this is possible.


    • Karthik kk says:

      You can do via exception handling or store all the value in a collection and verify just the collection, that way you will have more handle over the data and assertion rather doing directly via UI.

      Karthik KK

  6. swathi says:

    Hi Karthik,

    I need help as I am new to selenium.If I have 100 test cases each test case testing different scenario with different inputs do we need to to use 100 excel sheets?if so isn’t hard to maintain them?Can you please explain how to handle this kind of scenario?

    • Karthik kk says:

      Yap, its hard to maintain, thats why try to use Specflow and put all the datas in feature file tables, which is much efficient and business driven !!!

      Karthik KK

  7. Reshma says:

    Hi Karthikk,

    Thanks a lot for this videos, it really helped me so much !! Appreciate your effort.

    I have a doubt in retrieving data from different sheets in an excel file, Could you help me in that scenario… I am not able to sort it out by myself. It will be very helpful if you share some videos on that too..

    • Karthik kk says:

      Hi Reshama,

      In this post, we discussed code snippet to open excel sheet and return DataTable.

      All you have to do is to change the hardcoded sheet to the one you need

      //Get all the Tables
      DataTableCollection table = result.Tables;
      //Store it in DataTable
      DataTable resultTable = table["Sheet1"]; //any sheet value e.g Sheet2 or Sheet3

      Hope it helps !


  8. mir saadat ali hashmi says:

    Hello Karthik,
    First and above all thanks for easy to use video tutorials.

    I’m using the excel reader in my login methods and able to login by it. But when using the same login method in multiple tests and running all the tests at once it is giving errors.
    For 1st test it reads data from excel.
    For 2nd test onwards it fails to read data, when debugging i’m getting ReadTimeout errors on streams.

    May be can you please look into it.


  9. Dilip Sant says:

    This is a very nice example of reading excel. Although there was one exception which anyone can easily solve. Thanks Kartik for posting this. I appreciate your help.

Leave a Reply

Your email address will not be published. Required fields are marked *