Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dotnet/src/support/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load(
"csharp_library",
"generated_assembly_info",
"nuget_pack",
"nuget_package",
)
load(
"//dotnet:selenium-dotnet-version.bzl",
Expand Down Expand Up @@ -44,6 +45,8 @@ csharp_library(
],
deps = [
"//dotnet/src/webdriver:webdriver-netstandard2.0",
nuget_package("Microsoft.Bcl.AsyncInterfaces"),
nuget_package("System.Threading.Tasks.Extensions"),
],
)

Expand Down Expand Up @@ -84,6 +87,8 @@ csharp_library(
],
deps = [
"//dotnet/src/webdriver:webdriver-netstandard2.0-strongnamed",
nuget_package("Microsoft.Bcl.AsyncInterfaces"),
nuget_package("System.Threading.Tasks.Extensions"),
],
)

Expand Down
45 changes: 32 additions & 13 deletions dotnet/src/support/Events/EventFiringWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,38 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Asynchronously disposes this instance.
/// </summary>
/// <returns>A task representing the asynchronous dispose operation.</returns>
public async ValueTask DisposeAsync()
{
await this.DisposeAsyncCore().ConfigureAwait(false);
this.Dispose(false);
GC.SuppressFinalize(this);
}

/// <summary>
/// Stops the client from running.
/// </summary>
/// <param name="disposing">If <see langword="true"/>, managed resources are disposed.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
WrappedDriver.Dispose();
}
}

/// <summary>
/// Asynchronously performs the core dispose logic.
/// </summary>
/// <returns>A task representing the asynchronous dispose operation.</returns>
protected virtual async ValueTask DisposeAsyncCore()
{
await WrappedDriver.DisposeAsync().ConfigureAwait(false);
}

/// <summary>
/// Executes JavaScript in the context of the currently selected frame or window.
/// </summary>
Expand Down Expand Up @@ -580,19 +612,6 @@ public Screenshot GetScreenshot()
return screenshotDriver.GetScreenshot();
}

/// <summary>
/// Frees all managed and, optionally, unmanaged resources used by this instance.
/// </summary>
/// <param name="disposing"><see langword="true"/> to dispose of only managed resources;
/// <see langword="false"/> to dispose of managed and unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.WrappedDriver.Dispose();
}
}

/// <summary>
/// Raises the <see cref="Navigating"/> event.
/// </summary>
Expand Down
19 changes: 17 additions & 2 deletions dotnet/src/webdriver/Chromium/ChromiumDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ public void StopCasting(string deviceName)
}

/// <summary>
/// Stops the driver from running
/// Disposes of the resources used by the <see cref="ChromiumDriver"/> instance, including any active DevTools session.
/// </summary>
/// <param name="disposing">if its in the process of disposing</param>
/// <param name="disposing">Indicates whether the method is being called from a Dispose method (true) or from a finalizer (false).</param>
protected override void Dispose(bool disposing)
{
if (disposing)
Expand All @@ -497,6 +497,21 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

/// <summary>
/// Asynchronously disposes of the resources used by the <see cref="ChromiumDriver"/> instance, including any active DevTools session.
/// </summary>
/// <returns>A task representing the asynchronous dispose operation.</returns>
protected override async ValueTask DisposeAsyncCore()
{
if (this.devToolsSession != null)
{
this.devToolsSession.Dispose();
this.devToolsSession = null;
}

await base.DisposeAsyncCore().ConfigureAwait(false);
}

private static ICapabilities ConvertOptionsToCapabilities(ChromiumOptions options)
{
if (options == null)
Expand Down
11 changes: 0 additions & 11 deletions dotnet/src/webdriver/Firefox/FirefoxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,6 @@ protected virtual void PrepareEnvironment()
// Does nothing, but provides a hook for subclasses to do "stuff"
}

/// <summary>
/// Disposes of the FirefoxDriver and frees all resources.
/// </summary>
/// <param name="disposing">A value indicating whether the user initiated the
/// disposal of the object. Pass <see langword="true"/> if the user is actively
/// disposing the object; otherwise <see langword="false"/>.</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}

private static ICapabilities ConvertOptionsToCapabilities(FirefoxOptions options)
{
if (options == null)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/IWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace OpenQA.Selenium;
/// more fully featured browser when there is a requirement for one.
/// </para>
/// </remarks>
public interface IWebDriver : ISearchContext, IDisposable
public interface IWebDriver : ISearchContext, IDisposable, IAsyncDisposable
Comment thread
nvborisenko marked this conversation as resolved.
{
/// <summary>
/// Gets or sets the URL the browser is currently displaying.
Expand Down
72 changes: 59 additions & 13 deletions dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Asynchronously disposes the WebDriver Instance
/// </summary>
/// <returns>A task representing the asynchronous dispose operation.</returns>
public async ValueTask DisposeAsync()
{
await this.DisposeAsyncCore().ConfigureAwait(false);
this.Dispose(false);
Comment thread
nvborisenko marked this conversation as resolved.
GC.SuppressFinalize(this);
}

/// <summary>
/// Executes JavaScript "asynchronously" in the context of the currently selected frame or window,
/// executing the callback function specified as the last argument in the list of arguments.
Expand Down Expand Up @@ -672,25 +683,60 @@ protected bool RegisterInternalDriverCommand(string commandName, [NotNullWhen(tr
/// <param name="disposing">if its in the process of disposing</param>
protected virtual void Dispose(bool disposing)
{
try
if (disposing)
{
if (this.SessionId is not null)
{
this.Execute(DriverCommand.Quit, null);
try
{

this.Execute(DriverCommand.Quit, null);

}
catch (NotImplementedException)
{
}
catch (InvalidOperationException)
{
}
catch (WebDriverException)
{
}
finally
{
this.SessionId = null!;
}
}

this.CommandExecutor.Dispose();
}
catch (NotImplementedException)
{
}
catch (InvalidOperationException)
{
}
catch (WebDriverException)
{
}
finally
}

/// <summary>
/// Asynchronously performs the core dispose logic.
/// </summary>
/// <returns>A task representing the asynchronous dispose operation.</returns>
protected virtual async ValueTask DisposeAsyncCore()
{
if (this.SessionId is not null)
{
this.SessionId = null!;
try
{
await this.ExecuteAsync(DriverCommand.Quit, null).ConfigureAwait(false);
}
catch (NotImplementedException)
{
}
catch (InvalidOperationException)
{
}
catch (WebDriverException)
{
}
finally
{
this.SessionId = null!;
}
}

this.CommandExecutor.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/assets/nuget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Selenium is a set of different software tools each with a different approach to
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;

using var driver = new ChromeDriver();
await using var driver = new ChromeDriver();

driver.Url = "https://www.google.com";
driver.FindElement(By.Name("q")).SendKeys("webdriver" + Keys.Return);
Expand Down
10 changes: 10 additions & 0 deletions dotnet/test/common/StubDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace OpenQA.Selenium;

Expand Down Expand Up @@ -101,4 +102,13 @@ public void Dispose()
}

#endregion

#region IAsyncDisposable Members

public ValueTask DisposeAsync()
{
throw new NotImplementedException();
Comment thread
nvborisenko marked this conversation as resolved.
}

#endregion
}
1 change: 1 addition & 0 deletions dotnet/test/support/Events/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dotnet_nunit_test_suite(
"//dotnet/src/support",
"//dotnet/src/webdriver:webdriver-net8.0",
"//dotnet/test/common:fixtures",
nuget_package("Microsoft.Bcl.AsyncInterfaces"),
nuget_package("NUnit"),
nuget_package("Moq"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
Comment thread
nvborisenko marked this conversation as resolved.
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\webdriver\Selenium.WebDriver.csproj" />
<ProjectReference Include="..\..\src\support\Selenium.WebDriver.Support.csproj" />
<ProjectReference Include="..\common\Selenium.WebDriver.Common.Tests.csproj" />
</ItemGroup>
Expand Down
Loading