Taking screenshot in Selenium C# and Specflow
The below code helps you to take the screenshot in Selenium using Specflow.
And the code is executed only After the scenario is executed, So when the step of the scenario fails, it takes the screenshot, stores and kills the browser
The stored url is displayed on the html report in Jenkins using console.Writeline
[AfterScenario] public void CleanUp() { try { if (ScenarioContext.Current.TestError != null) { Screenshot ss = ((ITakesScreenshot)Browser).GetScreenshot(); string title = ScenarioContext.Current.ScenarioInfo.Title; string Runname = title + DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"); string drive = Path.GetPathRoot("X:\\screenshots\\"); if (!Directory.Exists(drive)) { ss.SaveAsFile("C:\\Recording\\" + Runname + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } else { string screenshotfilename = "X:\\screenshots\\" + Runname + ".jpg"; ss.SaveAsFile(screenshotfilename, System.Drawing.Imaging.ImageFormat.Jpeg); string urlfile = "http://storage/screenshots/" + Runname + ".jpg"; //Console.WriteLine(" "); Console.WriteLine(""+urlfile); } } } catch { //Console.WriteLine("catch"); } finally { if (Browser != null) { Browser.Quit(); Browser = null; } }