0

I have log4net implemented for the logging messages and extent reports using specflow/xunit. Incase if any errors/exception ocurrs,and i dont have the try/catch statement of the log4net, then the extent report catches the exception and prints it out as failed step in the report. But if i have the try and catch statements, and log4net is catching the execeptions, the extent report is not logging that as failed step and making the same step as passed but it should be failed.

How can i make the extent report think that log4net has caught a exception/error and this step has to be failed.

Below is my try/catch statement for a method which will fail

public void ClickonLoginButton()
{
try{
      ClickonElement(LoginbuttonLocator);
      Log.info("successfully clicked");
}
catch(exception ex){
Log.error("unable to click");
Console.WriteLine(ex.StackTrace);
}
}

My extent report afterstep code:

 [AfterStep]
        public void InsertReportingSteps()
        {

            var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType.ToString();

            if (ScenarioContext.Current.TestError == null)
            {
                if (stepType == "Given")
                    _scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "When")
                    _scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "Then")
                    _scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text);
                else if (stepType == "And")
                    _scenario.CreateNode<And>(ScenarioStepContext.Current.StepInfo.Text);
            }
            else if (ScenarioContext.Current.TestError != null)
            {
                if (stepType == "Given")
                    _scenario.CreateNode<Given>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
                else if (stepType == "When")
                    _scenario.CreateNode<When>(ScenarioStepContext.Current.StepInfo.Text).Fail(ScenarioContext.Current.TestError.InnerException);
                else if (stepType == "Then")
                {
                    string Runname = screenshot();
                    _scenario.AddScreenCaptureFromPath("C:\\Users\\xxxx- PC\\Desktop\\XUnit_Assignement_3\\XUnit_Assignement\\target\\ErrorScreenshots\\" 
                        + Runname + ".Png");
                    _scenario.CreateNode<Then>(ScenarioStepContext.Current.StepInfo.Text).
                        Fail(ScenarioContext.Current.TestError.Message);
                    _scenario.Fail("Failed Because of Some issues",
                        MediaEntityBuilder.CreateScreenCaptureFromPath(@"C:\Users\xxxx- PC\Desktop\XUnit_Assignement_3\XUnit_Assignement\TestResults\image.JPG", 
                        "Failed").Build());
                }
            }
Prp
  • 51
  • 5

1 Answers1

1

What happens when you are using try/catch:

It has nothing to do with log4net, but with you catching the exception. If you don't the exception will be passed on through the stack until something catches it or it ends up in a global handler. If you do catch it in your code, it will stop there. It will be considered "handled" and the control flow moves on after the catch block.

So, your report engine does not "see" that there was an exception, because you catch it before it reaches a place where the engine would catch it.

How to make the engine become aware of the exception:

You need to rethrow the exception:

catch(Exception ex){
    Log.error("unable to click");
    Console.WriteLine(ex.StackTrace);
    throw;
}

then it will bubble up as usual. Catching it stops it from bubbling up further - because it is expected to be handled accordingly in the catch block. If you are using the catch block just for logging, then you need to throw the same exception again.

Mind: There is a difference between throw; and throw ex;. They will produce different stacktraces. throw; will keep the stacktrace, while throw ex will reset it. (see https://stackoverflow.com/a/730255/982149)

Fildor
  • 14,510
  • 4
  • 35
  • 67