-
Notifications
You must be signed in to change notification settings - Fork 23
/
BrowserFactory.cs
85 lines (72 loc) · 2.71 KB
/
BrowserFactory.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Concurrent;
using BoDi;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using TechTalk.SpecFlow;
namespace SpecFlowMasterClass.SpecOverflow.Specs.WebUI.Support
{
public class BrowserFactory
{
public class BrowserInstance : IDisposable
{
private readonly Lazy<IWebDriver> _webDriver;
public BrowserInstance()
{
_webDriver = new Lazy<IWebDriver>(CreateWebDriver);
}
private IWebDriver CreateWebDriver()
{
return new ChromeDriver();
}
public IWebDriver GetWebDriver()
{
return _webDriver.Value;
}
public void Dispose()
{
if (_webDriver.IsValueCreated)
_webDriver.Value.Dispose();
}
}
private readonly IObjectContainer _browserObjectContainer;
private IWebDriver _browserCreated;
public BrowserFactory(ScenarioContext scenarioContext)
{
// creating browser per test-thread (requires TestThreadContainerDisposer below)
_browserObjectContainer = scenarioContext.ScenarioContainer.Resolve<TestThreadContext>().TestThreadContainer;
// creating browser per feature
//_browserObjectContainer = scenarioContext.ScenarioContainer.Resolve<FeatureContext>().FeatureContainer;
// creating browser per scenario
//_browserObjectContainer = scenarioContext.ScenarioContainer;
}
public IWebDriver CreateBrowser()
{
if (_browserCreated == null)
_browserCreated = _browserObjectContainer.Resolve<BrowserInstance>().GetWebDriver();
return _browserCreated;
}
#region TestThreadContainerDisposer
[Binding]
public class TestThreadContainerDisposer
{
private static readonly ConcurrentBag<IObjectContainer> TestThreadContainers = new();
[BeforeScenario(Order = -1)]
public void EnsureTestThreadContainerRegistered(ScenarioContext scenarioContext)
{
var testThreadContainer = scenarioContext.ScenarioContainer.Resolve<TestThreadContext>().TestThreadContainer;
TestThreadContainers.Add(testThreadContainer);
}
[AfterTestRun]
public static void DisposeTestThreadContainers()
{
var containers = TestThreadContainers.ToArray();
foreach (var container in containers)
{
container.Dispose();
}
}
}
#endregion
}
}