We can Read Data from Excel (XLS/XLSX) file just as below and utilize them in the Test cases.  If we require to read data from Multipl...

How to Read Data from Excel XLS/XLSX file and utilize it for Data Driven Testing


We can Read Data from Excel (XLS/XLSX) file just as below and utilize them in the Test cases. 
If we require to read data from Multiple Excel file or Different Excel files, We can just change the file name in "Data Source=C:\\Purush\\data.xls" in the below DataSource attribute to the file name from which we wanted to read the data and pass to test case. Also we can change the sheet name accordingly in DataSource attribute below.

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Purush_CSV
{
    [TestClass]
    public class Purush_ReadDataExcel
    {
        private TestContext testContext;
        public TestContext TestContext
        {
            get { return testContext; }
            set { testContext = value; }
        }

        
        [TestMethod]
        [DataSource("System.Data.OleDb", "Provider=Microsoft.ACE.OLEDB.12.0;Data 
         Source=C:\\Purush\\data.xls;Persist Security Info=False;Extended Properties='Excel 
         12.0 Xml;HDR=YES'", "Sheet1$", DataAccessMethod.Sequential)]
        public void Purush_Read()
        {
            string a = TestContext.DataRow["ResultsColumn"].ToString(); 
            // Will be reading value from ResultsColumn in Excel File which is located in  
               Data Folder under this solution
        }
    }
}

0 comments: