Try Catch in c#

When I was automating my test case today, I found an interesting thing: The case was failed, but it was reported "passed" in my test report!

My lead and I checked it carefully, then we found that as I created a test log in for the case, the LogStatus.Info is true, so in test report, the case's status was reported as "Pass" . But Actually, because an error occured, the case didn't go to the validation part.

The previous codes were as following:

                OwnersPropertiesPage();
                EnterPropertyDetails();
                EnterFinancialDetails();
                EnterTenantDetails();

                OwnersPropertiesPage(); //the case failed here

                // Screenshot
                String img = SaveScreenShotClass.SaveScreenshot(Driver.driver, "FinalScreenshot");
                Base.test.Log(LogStatus.Info, "FinalScreenshot: " + img);


                //open the "PropertyDetails" sheet
                ExcelLib.PopulateInCollection(Base.ExcelPath, "PropertyDetails");

                //name1=name of the property which has just been added.
                var name1 = ExcelLib.ReadData(2, "propertyName");
                Thread.Sleep(2000);

                //name2=name of the property which is the first property displayed on the Owners>Properties page
                var name2 = Driver.driver.FindElement(By.XPath(".//*[@id='main-content']/section/div[1]/div[1]/div[4]/div[1]/div/div/div[2]/div[2]/div[1]/div[1]/div[1]")).Text;

                if (name1 == name2)
                         Base.test.Log(LogStatus.Pass, "ValidateAddNewProperty is Passed");
                 else
                         Base.test.Log(LogStatus.Fail, "ValidateAddNewProperty is Failed");

Here is the TearDown:
[TearDown]
        public void TearDown()
        {
            // Screenshot
            String img = SaveScreenShotClass.SaveScreenshot(Driver.driver, "Report");//AddScreenCapture(@"E:\Dropbox\VisualStudio\Projects\Beehive\TestReports\ScreenShots\");
            test.Log(LogStatus.Info, "Image example: " + img);
            // end test. (Reports)
            extent.EndTest(test);
            // calling Flush writes everything to the log file (Reports)
            extent.Flush();
        }

Later, I used a Try Catch to fix the problem. I changed the codes as following:

 try
            {
                OwnersPropertiesPage();
                EnterPropertyDetails();
                EnterFinancialDetails();
                EnterTenantDetails();

               OwnersPropertiesPage(); //the case still failed here

                // Screenshot
                String img = SaveScreenShotClass.SaveScreenshot(Driver.driver, "FinalScreenshot");
                Base.test.Log(LogStatus.Info, "FinalScreenshot: " + img);


                //open the "PropertyDetails" sheet
                ExcelLib.PopulateInCollection(Base.ExcelPath, "PropertyDetails");

                //name1=name of the property which has just been added.
                var name1 = ExcelLib.ReadData(2, "propertyName");
                Thread.Sleep(2000);

                //name2=name of the property which is the first property displayed on the Owners>Properties page
                var name2 = Driver.driver.FindElement(By.XPath(".//*[@id='main-content']/section/div[1]/div[1]/div[4]/div[1]/div/div/div[2]/div[2]/div[1]/div[1]/div[1]")).Text;

                //Console.WriteLine(name1);
                //Console.WriteLine(name2);

                if (name1 == name2)

                    //Console.WriteLine("test pass");
                    Base.test.Log(LogStatus.Pass, "ValidateAddNewProperty is Passed");

            }

            catch(Exception e)
            {
                Base.test.Log(LogStatus.Fail, "ValidateAddNewProperty is Failed",e.Message);
            }

Then I got the correct report report: Failed, and the fail details was recorded in the report, like "Could not find element by: By.XPath: /html/body/div[5]/div/div[5]/a[1]".
         

0 comments:

Post a Comment