Skip to content

Commit

Permalink
Merged Pull Request '#190 feature/class-init-chrome-driver->version/4…
Browse files Browse the repository at this point in the history
….5: Make `ChromeDriver` initialize once per class rather than per test. (on `v4.5`)'
  • Loading branch information
drasmart authored Feb 7, 2025
1 parent d88727d commit e6d0b5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public class JavaScriptBuilderElementTemplateTests: JavaScriptBuilderElementTest
private JObject jsonData { get; set; } = new();
private string fullJS { get; set; } = "";

[ClassInitialize]
public static new void ClassInit(TestContext context) => JavaScriptBuilderElementTestsBase.ClassInit(context).Wait();

[ClassCleanup]
public static new void ClassCleanup() => JavaScriptBuilderElementTestsBase.ClassCleanup().Wait();

[TestInitialize]
public override async Task Init()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public class JavaScriptBuilderElementTests: JavaScriptBuilderElementTestsBase
private CancellationTokenSource clientServerTokenSource;
private HttpListener _clientServer;

[ClassInitialize]
public static new void ClassInit(TestContext context) => JavaScriptBuilderElementTestsBase.ClassInit(context).Wait();

[ClassCleanup]
public static new void ClassCleanup() => JavaScriptBuilderElementTestsBase.ClassCleanup().Wait();

/// <summary>
/// Initialise the test.
/// </summary>
Expand Down Expand Up @@ -167,8 +173,8 @@ public void JavaScriptBuilder_VerifyFallbackResponse()
_javaScriptBuilderElement =
new JavaScriptBuilderElementBuilder(LoggerFactory)
.SetEndpoint("/json")
.Build();

.Build();

var flowData = new Mock<IFlowData>();

flowData.Setup(d => d.Get<IJsonBuilderElementData>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class JavaScriptBuilderElementTestsBase
{
public string ClientServerUrl { get; private set; }

public ChromeDriver Driver { get; private set; }
public INetwork Interceptor => Driver?.Manage().Network;
public static ChromeDriver Driver { get; private set; }
public static INetwork Interceptor => Driver?.Manage().Network;

public ILoggerFactory LoggerFactory { get; private set; }

Expand All @@ -46,12 +46,10 @@ public class JavaScriptBuilderElementTestsBase
private Mock<IElementData> _elementDataMock;
private IList<IElementPropertyMetaData> _elementPropertyMetaDatas;

public Action<NetworkRequestSentEventArgs> OnRequestSent { get; set; } = null;
public static Action<NetworkRequestSentEventArgs> OnRequestSent { get; set; } = null;

public virtual async Task Init()
public static async Task ClassInit(TestContext context)
{
ClientServerUrl = $"http://localhost:{TestHttpListener.GetRandomUnusedPort()}/";

var chromeOptions = new ChromeOptions();
chromeOptions.SetLoggingPreference(LogType.Browser, OpenQA.Selenium.LogLevel.Info);
chromeOptions.AcceptInsecureCertificates = true;
Expand All @@ -62,18 +60,22 @@ public virtual async Task Init()
var options = new ChromeOptions();

// Set the desired DevTools protocol version
options.AddAdditionalOption("devtoolsProtocolVersion", "127");
options.AddAdditionalOption("devtoolsProtocolVersion", "127");
Driver = new ChromeDriver(chromeOptions);
}
catch (WebDriverException)
{
Assert.Inconclusive("Could not create a ChromeDriver, check " +
"that the Chromium driver is installed");
"that the Chromium driver is installed");
}

Interceptor.NetworkRequestSent += OnNetworkRequestSent;
await Interceptor.StartMonitoring();

}

public virtual async Task Init() {
ClientServerUrl = $"http://localhost:{TestHttpListener.GetRandomUnusedPort()}/";

_mockjsonBuilderElement = new Mock<IJsonBuilderElement>();

_elementPropertyMetaDatas = new List<IElementPropertyMetaData>() {
Expand All @@ -88,7 +90,7 @@ public virtual async Task Init()
LoggerFactory = new LoggerFactory();
}

private void OnNetworkRequestSent(object sender, NetworkRequestSentEventArgs e)
private static void OnNetworkRequestSent(object sender, NetworkRequestSentEventArgs e)
=> OnRequestSent?.Invoke(e);


Expand Down Expand Up @@ -181,16 +183,20 @@ public void Configure(
flowData.Setup(d => d.Get(It.IsAny<string>())).Returns(_elementDataMock.Object);
}

public virtual async Task Cleanup()
public virtual Task Cleanup()
{
// Ignore request monitoring events
OnRequestSent = null;
return Task.CompletedTask;
}

public static async Task ClassCleanup()
{
if (Driver != null)
{
await Interceptor.StopMonitoring();
Driver.Quit();
}

// Ignore request monitoring events
OnRequestSent = null;
}
}
}

0 comments on commit e6d0b5d

Please sign in to comment.