-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathBrowserTestBase.cs
89 lines (72 loc) · 2.31 KB
/
BrowserTestBase.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
86
87
88
89
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.ExceptionServices;
using OpenQA.Selenium;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.E2ETesting;
[CaptureSeleniumLogs]
public class BrowserTestBase : IClassFixture<BrowserFixture>, IAsyncLifetime
{
private static readonly AsyncLocal<IWebDriver> _asyncBrowser = new AsyncLocal<IWebDriver>();
private static readonly AsyncLocal<ILogs> _logs = new AsyncLocal<ILogs>();
private static readonly AsyncLocal<ITestOutputHelper> _output = new AsyncLocal<ITestOutputHelper>();
private ExceptionDispatchInfo _exceptionDispatchInfo;
private IWebDriver _browser;
public BrowserTestBase(BrowserFixture browserFixture, ITestOutputHelper output)
{
BrowserFixture = browserFixture;
_output.Value = output;
}
public IWebDriver Browser
{
get
{
if (_exceptionDispatchInfo != null)
{
_exceptionDispatchInfo.Throw();
throw _exceptionDispatchInfo.SourceException;
}
return _browser;
}
set
{
_browser = value;
}
}
public static IWebDriver BrowserAccessor => _asyncBrowser.Value;
public static ILogs Logs => _logs.Value;
public static ITestOutputHelper Output => _output.Value;
public BrowserFixture BrowserFixture { get; }
public Task DisposeAsync()
{
return Task.CompletedTask;
}
public virtual Task InitializeAsync()
{
return InitializeAsync("");
}
public virtual Task InitializeAsync(string isolationContext)
{
InitializeBrowser(isolationContext);
InitializeAsyncCore();
return Task.CompletedTask;
}
protected virtual void InitializeAsyncCore()
{
}
protected void InitializeBrowser(string isolationContext)
{
try
{
var (browser, logs) = BrowserFixture.GetOrCreateBrowser(Output, isolationContext);
_asyncBrowser.Value = browser;
_logs.Value = logs;
Browser = browser;
}
catch (Exception ex)
{
_exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);
throw;
}
}
}