Wednesday 8 September 2021

Selenium-C# - MSTest-Excel Data Driven and ExcelDataReader Visual Studio issues




 Selenium-C#- V 4.0.0-rc1

ExcelDataReader - V 3.7.0-develop00310

ExcelDataReader.DataSet - V 3.7.0-develop00310

Visual Studio - Version 16.11.2

=========================================================================

ExcelDataReader:

Public calss ExcelDataRead

{

 public void TestReadExcel()

        {

            FileStream stream = new FileStream(@"TestCreateBugXlsx.xlsx", FileMode.Open, FileAccess.Read);

   ftype = Path.GetExtension(filename);

   if (ftype == ".xls")

  {

IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);

  }

  else

 {

IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

 }

           //To UseHeaderRow or filter the rows

            var result = excelReader.AsDataSet(new ExcelDataSetConfiguration()

            {

                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()

                {

                        UseHeaderRow = true,

                        FilterRow = rowReader => rowReader.Depth > 1

                }

            });

            DataTable result1 = result.Tables["TestCreateBug"];

}

}

====================================================================

Issues in ExcelDataReader:

1. "excelReader.IsFirstRowAsColumnNames = true;" is not working

2. When the ".xlsb" file is read with the "FilterRow = rowReader => rowReader.Depth > 0" in configuration section, it is not working as expected, even when Filter row is commented out, the same output is generated.

3. When the ".xlsb" file is read, the character "d" is randomly generated

Screenshot:



4. Added a new sheet such as "Sheet1" and when the below code is executed the success message is displayed and data reading is unsuccessful when the 1st row is used as header.

Code:
 [TestClass]
    public class TestExcelData
    {

        [TestMethod]
        public void TestReadExcel()
        {
            FileStream stream = new FileStream(@"TestCreateBugXlsb.xlsb", FileMode.Open, FileAccess.Read);
            //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

            var result = excelReader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = true,
                    FilterRow = rowReader => rowReader.Depth > 0
                }
            });

            DataTable table = result.Tables["Sheet1"];
           
            for (int i=0; i < table.Rows.Count; i++)
            {
                var col = table.Rows[i];
                for (int j=0; j < col.ItemArray.Length; j++)
                {
                    Console.WriteLine("Data : {0}", col.ItemArray[j]);
                }
            }
        }
    }
}


4. Added a new sheet such as "Sheet1" and when the below code is executed the success message is displayed and data reading is successful when the 1st row is not used as header.

 [TestClass]
    public class TestExcelData
    {

        [TestMethod]
        public void TestReadExcel()
        {
            FileStream stream = new FileStream(@"TestCreateBugXlsb.xlsb", FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

            DataTable table = excelReader.AsDataSet().Tables["Sheet1"];

            for (int i = 0; i < table.Rows.Count; i++)
            {
                var col = table.Rows[i];
                for (int j = 0; j < col.ItemArray.Length; j++)
                {
                    Console.WriteLine("Data : {0}", col.ItemArray[j]);
                }
            }
        }
    }
}




=================================================================

Excel Data Driven using the Data Source in MSTest:

public class DataDriven

    {

        private TestContext _testContext;

        public TestContext TestContext

        {

            get { return _testContext; }

            set { _testContext = value; }

         }        

        [TestMethod]

        [DataSource("System.Data.Odbc", @"Dsn=Excel Files;dbq=CreateBugXlxs.xlsx", "CreateBug$", DataAccessMethod.Sequential)]

        public void TestDataDriven()

        {

            Console.WriteLine(TestContext.DataRow[0].ToString());

            Console.WriteLine(TestContext.DataRow[1].ToString());

            Console.WriteLine(TestContext.DataRow[2].ToString());

            Console.WriteLine(TestContext.DataRow[3].ToString());

            Console.WriteLine(TestContext.DataRow[4].ToString());

}

Issues:

1. When the '.xlsb' file is being data driven/read, an additional empty row is being executed or stored

Screenshot:



2. Added a new sheet such as "Sheet1" and when the below code is executed the warning is displayed without any details and data reading is unsuccessful as 1st row is used as header by data source as default.

Code:

[TestClass]
    public class TestCreateBugXlxs
    {
        private TestContext _testContext;

        public TestContext TestContext
        {
            get { return _testContext; }
            set { _testContext = value; }
        }

        
        [TestMethod]
        [DataSource("System.Data.Odbc", @"Dsn=Excel Files;dbq=TestCreateBugXlsb.xlsb", "Sheet1$", DataAccessMethod.Sequential)]
        public void TestBugXlxs()
        {
            Console.WriteLine(TestContext.DataRow[0].ToString());
         }
  }




3. When the Excel is Data Driven and Platform target is 'Any CPU' and installed is 'accessdatabaseengine_X64.exe', error is displayed but when the 'x64' is chosen, the error is not displayed here the performance is degraded (Visual Studio 2019)

Screenshot:





No comments:

Post a Comment