Friday, September 18, 2015

Data driven test method using selenium (C#)

Test methods which need to be executed with different set of data are very common in testing and the best approach for this type of test is to create a CSV file with all set of test data and call it in your test. See how we can do it using C#

Step 1: Create a new project and add a new item “text file” and name it as testdata.csv.

Step 2: Add following code in this test data file

SiNo,Client,Client_Initials,Run
1,DemoClient1,DemoClient1,Yes
2,DemoClient2,DemoClient2,Yes
3,DemoClient3,DemoClient3,Yes
4,DemoClient4,DemoClient4,Yes

Step 3: Click on the project name -> Add new item -> Select “Basic Unit Test” -> Click ok

Step 4: Add following code in your CS file

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using System;

namespace WordPressAutomation.SmokeTest
{
    [TestClass()]
    public class BankAccountTest
    {
        private TestContext testContextInstance;
        public TestContext TestContext
        {
            get { return testContextInstance; }
            set { testContextInstance = value; }
        }

        [TestMethod]
        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "C:\\Users\\DemoUser\\Documents\Visual Studio 2013\\Projects\\UnitTestProject2\\UnitTestProject2\\testdata.csv", "TestData#csv", DataAccessMethod.Sequential)]
        public void DebitTest()
        {

            Console.WriteLine(testContextInstance.DataRow["Client"]);
            Console.WriteLine(testContextInstance.DataRow["Client_Initials"]);
            Console.WriteLine(testContextInstance.DataRow["Run"]);
        }
   }
}

Step 5: Click on “Reference” -> Add Reference - > Select system.data -> Click ok


Step 6: It is done. Execute it and see all test data mentioned in CSV file will be executed. 

1 comment:

  1. Hello Rajesh,
    The Article on Data driven test method using selenium is nice. It give detail information about it .Thanks for Sharing the information on SELENIUM. Software Testing Company


    ReplyDelete