Last active
June 20, 2019 00:00
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AfterScenario] | |
public void OnError() | |
{ | |
if (ScenarioContext.Current.TestError != null) | |
{ | |
//TODO: save useful information to a file | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AfterStep] | |
public void OnError() | |
{ | |
if (ScenarioContext.Current.TestError != null) | |
{ | |
var stepInfo = ScenarioContext.Current.StepContext.StepInfo; | |
var stepDescription = stepInfo.StepDefinitionType + stepInfo.Text; | |
// ... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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