Run group of Excel check tests cause the issues break as per an exception

0

I use Office. Interop.Excel library in my code to check excels, when I execute all tests I found that most issues had break at the code line:

    Application xlApp = new Application();

due to the error --> System.Runtime.InteropServices.COMException : Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

How can I handle this.

This is a sample of my tests:

 public sealed class regression_1958 : TestFixtureBase
    {
        [Test]
        public void Sch_issue_1958()
        {
            GHtml GHtml1 = new GHtml();

            double load_time = 0;
            string results_header = "";

            var reporter = ReporterFactory.Create();

            reporter.Save("Report_1958.xls");

            WebDriverWait wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(120));
            wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.CssSelector("div.loading")));

            Boolean Error = false;
            // --------------------------------------------------------------------------------------------

            string userRoot = System.Environment.GetEnvironmentVariable("USERPROFILE");

            string path = userRoot + @"\Downloads";


            System.IO.DirectoryInfo di = new DirectoryInfo(path);

            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true);
            }


            GivenIAmLoggedIn_citi1();
            wait_till_cube_disapeared(out load_time, reporter, "Report_1958.xls");
            wait_till_cube_disapeared(out load_time, reporter, "Report_1958.xls");


            Pause(1000);
            String current_Ver = _webDriver.FindElement(By.CssSelector("span.product-version")).Text;


            string login_str = "As " + Settings.UserName_citi1;
            reporter.Report_ver(login_str, "", "", 1);
            reporter.Report_ver("Regression test Last Modified Against :Cube Regulatory Data Service v3.0.0 (build 00000)", "For testers: they are updating this manually when there is a modifications required", "", 2);
            reporter.Report_ver("Regression test Last Run Date :" + System.DateTime.Now.ToString(), "Last time Regression test triggerd", "", 3);
            reporter.Report_ver("Regression test Last Ran Against :" + current_Ver, "Regression test just triggred Against which Cube version", "", 4);

            reporter.Save("Report_1958.xls");
            // --------------------------------------------------------------------------------------------


            //start code1
            //-----------------------------



            Analytics_page das_page = new Analytics_page();
            Policy_All_Tasks_page sch_task = new Policy_All_Tasks_page();
            Policy_Report_page pol_rep_page = new Policy_Report_page();

            _webDriver.Navigate().GoToUrl(Settings.LoginUrl + "/main/#/schedule/report");
            wait_till_disapeared_without_time(reporter, "Report_1958.xls");

            pol_rep_page.set_driver(_webDriver);
            pol_rep_page.Click_reset();
            pol_rep_page.set_driver(_webDriver);
            pol_rep_page.select_juridiction_item("Algeria");
            wait_till_disapeared_without_time(reporter, "Report_1958.xls");
            wait_till_disapeared_without_time(reporter, "Report_1958.xls");
            pol_rep_page.set_driver(_webDriver);
            pol_rep_page.Click_filter();
            wait_till_disapeared_without_time(reporter, "Report_1958.xls");
            pol_rep_page.set_driver(_webDriver);
            pol_rep_page.Downoad_All_Excels();
            wait_till_disapeared_without_time(reporter, "Report_1958.xls");

            path = path + @"\schedule-report.xlsx";

            Application xlApp = new Application();
            Workbook xlWorkBook = xlApp.Workbooks.Open(path);
            Worksheet xlWorkSheet = new Worksheet();
            xlWorkSheet = xlWorkBook.Sheets[1];
            Range xlRange = xlWorkSheet.UsedRange;
            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;



            int pre_unit = 0;
            int Pre_period = 0;



            for (int j = 1; j <= colCount; j++)
            {
                if (xlRange.Cells[1, j].Value2 == "Previous Unit")
                {
                    pre_unit = j;
                }

                if (xlRange.Cells[1, j].Value2 == "Previous Period")
                {
                    Pre_period = j;
                }
            }

            Boolean Cond1 = false;
            Boolean Cond2 = false;

            for (int i = 1; i <= rowCount; i++)
            {

                if (xlRange.Cells[i, pre_unit] != null && xlRange.Cells[i, pre_unit].Value2 != null)
                {
                    Cond1 = true;
                    Console.Write(xlRange.Cells[i, pre_unit].Value2.ToString() + "\t");

                }

                if (xlRange.Cells[i, Pre_period] != null && xlRange.Cells[i, Pre_period].Value2 != null)
                {
                    Cond2 = true;
                    Console.Write(xlRange.Cells[i, Pre_period].Value2.ToString() + "\t");

                }
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            Marshal.ReleaseComObject(xlRange);
            Marshal.ReleaseComObject(xlWorkSheet);
            xlWorkBook.Close();
            xlApp.Quit();
            Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);


            if (Cond1 && Cond2)
            {
                reporter.Report("CUBETWO-1958", "Pass", "", "");

                GHtml1.AddRows("CUBETWO-1958", "Pass", "", "");
            }
            else
            {
                reporter.Report("CUBETWO-1958", "Fail", "", "");

                GHtml1.AddRows("CUBETWO-1958", "Fail", "", "");
            }

            reporter.Save("Report_1958.xls");
            GHtml1.CreatePage("Report_1958.html");

        }

    }

}
c#
excel
selenium-webdriver
asked on Stack Overflow Aug 19, 2018 by Sarah

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0