/
SpecflowLifecycleExample.cs
68 lines (60 loc) · 2.03 KB
/
SpecflowLifecycleExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Configuration;
using System.Drawing.Imaging;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium;
using TechTalk.SpecFlow;
namespace YourNamespace
{
/// <summary>
/// NOTE: this has to be in the same DLL as your Specflow
/// tests, hence it is out of the src directory here for you
/// to copy elsewhere.
/// </summary>
[Binding]
public class SpecflowLifecycle
{
[BeforeFeature("SpecflowLifecycle")]
public static void BeforeFeature()
{
Scenario.AddContextWithName(Scenario.DefaultContextName);
}
[AfterFeature("SpecflowLifecycle")]
public static void AfterFeature()
{
foreach (var c in Scenario.Contexts)
{
c.Value.Driver.Quit();
}
Scenario.Contexts.Clear();
}
[AfterScenario("SpecflowLifecycle")]
public void AfterScenario()
{
foreach (var c in Scenario.Contexts)
{
c.Value.Driver.Manage().Cookies.DeleteAllCookies();
}
TestIssue.TearDown();
}
[AfterStep("SpecflowLifecycle")]
public static void AfterStep()
{
if (ScenarioContext.Current.TestError == null)
{
return;
}
foreach (var c in Scenario.Contexts)
{
var screenShotsFolderPath = ConfigurationManager.AppSettings["WebDriverScreenshotsFolder"];
ScreenshotCapturingHelpers.CreateDirectoryIfNotExists(screenShotsFolderPath);
var imagePath = Path.Combine(screenShotsFolderPath, ScenarioContext.Current.ScenarioInfo.Title + ".png");
if (bool.Parse(ConfigurationManager.AppSettings["WebDriverScreenshotsEnabled"]))
{
var screenShot = ((ITakesScreenshot)c.Value.Driver).GetScreenshot();
screenShot.SaveAsFile(imagePath, ImageFormat.Png);
}
}
}
}
}