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());
}
}