Repository navigation
Expand file tree
/
Copy pathBaseTest.cs
More file actions
60 lines (50 loc) · 1.64 KB
/
Copy pathBaseTest.cs
File metadata and controls
60 lines (50 loc) · 1.64 KB
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
using System;
using System.Drawing.Imaging;
using System.IO;
using Autofac;
using OpenQA.Selenium.Firefox;
using Xunit.Sdk;
namespace SeleniumWithXunit.Infrastructure
{
public abstract class BaseTest : IDisposable, INeedToKnowTestFailure, INeedToKnowTestSuccess
{
private static readonly object padlock = new object();
private static bool isTestRunSetup;
public FirefoxDriver Browser { get; set; }
protected BaseTest()
{
EnsureTestRunIsSetup();
CurrentLifetimeScope = TestRun.Container.BeginLifetimeScope();
CurrentLifetimeScope.InjectUnsetProperties(this);
}
private ILifetimeScope CurrentLifetimeScope { get; set; }
public void Dispose()
{
CurrentLifetimeScope.Dispose();
}
public void Handle(TestFailed result)
{
TakeScreenshot(result.TestDisplayName);
}
private void TakeScreenshot(string testName)
{
var browserScreenShot = Browser.GetScreenshot();
string pathToScreenShot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("HH_mm_ss_fff_") + testName + ".png");
browserScreenShot.SaveAsFile(pathToScreenShot, ImageFormat.Png);
}
public void Handle(TestPassed result)
{
//All good
}
private static void EnsureTestRunIsSetup()
{
if (isTestRunSetup) return;
lock (padlock)
{
if (isTestRunSetup) return;
TestRun.Setup();
isTestRunSetup = true;
}
}
}
}