Skip to content

Instantly share code, notes, and snippets.

@gasparnagy
Last active June 20, 2019 00:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gasparnagy/39f1999ec7f3f7fd1e34d824788f7053 to your computer and use it in GitHub Desktop.
Save gasparnagy/39f1999ec7f3f7fd1e34d824788f7053 to your computer and use it in GitHub Desktop.
Code examples for post: SpecFlow Tips: Collect more information on error (part 1)
[AfterScenario]
public void OnError()
{
if (ScenarioContext.Current.TestError != null)
{
//TODO: save useful information to a file
}
}
[AfterStep]
public void OnError()
{
if (ScenarioContext.Current.TestError != null)
{
var stepInfo = ScenarioContext.Current.StepContext.StepInfo;
var stepDescription = stepInfo.StepDefinitionType + stepInfo.Text;
// ...
}
}
[AfterScenario]
public void OnError()
{
if (ScenarioContext.Current.TestError != null)
{
// get and save a screenshot from a webdriver
var takesScreenshot = myWebDriver as ITakesScreenshot;
if (takesScreenshot != null)
{
string screenshotFileName = ToPath(string.Format("{0}_{1}_{2}.png",
FeatureContext.Current.FeatureInfo.Title,
ScenarioContext.Current.ScenarioInfo.Title,
DateTime.Now.ToString("s")));
string screenshotFilePath = TestFolders.GetOutputFilePath(screenshotFileName);
var screenshot = takesScreenshot.GetScreenshot();
screenshot.SaveAsFile(screenshotFilePath, ImageFormat.Png);
Console.WriteLine("Screenshot: {0}", new Uri(screenshotFilePath));
}
}
}
/// <summary>
/// Makes string path-compatible, ie removes characters not allowed in path and replaces whitespace with '_'
/// </summary>
public static string ToPath(string s)
{
var builder = new StringBuilder(s);
foreach (var invalidChar in Path.GetInvalidFileNameChars())
{
builder.Replace(invalidChar.ToString(), "");
}
builder.Replace(' ', '_');
return builder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment