Skip to content

Commit

Permalink
[dotnet] IJavascriptEngine implements IDisposable where available (#1…
Browse files Browse the repository at this point in the history
…1594)

JavascriptExecutor takes an IWebDriver and, on a lazy basis, opens a
DevToolsSession which it currently leaves open. This PR disposes on
that DevToolsSession if it was instantiated.
  • Loading branch information
RenderMichael authored Feb 17, 2023
1 parent e11ab24 commit c1ac4c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/IJavaScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace OpenQA.Selenium
/// <summary>
/// Defines an interface allowing the user to manage settings in the browser's JavaScript engine.
/// </summary>
public interface IJavaScriptEngine
public interface IJavaScriptEngine : IDisposable
{
/// <summary>
/// Occurs when a JavaScript callback with a named binding is executed.
Expand Down
22 changes: 22 additions & 0 deletions dotnet/src/webdriver/JavaScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class JavaScriptEngine : IJavaScriptEngine
private Dictionary<string, PinnedScript> pinnedScripts = new Dictionary<string, PinnedScript>();
private List<string> bindings = new List<string>();
private bool isEnabled = false;
private bool isDisposed = false;

/// <summary>
/// Initializes a new instance of the <see cref="JavaScriptEngine"/> class.
Expand Down Expand Up @@ -394,5 +395,26 @@ private void OnConsoleApiCalled(object sender, ConsoleApiCalledEventArgs e)
});
}
}

public void Dispose()
{
this.Dispose(true);
}

protected virtual void Dispose(bool disposing)
{
if (!this.isDisposed)
{
if (disposing)
{
if (this.session.IsValueCreated)
{
this.session.Value.Dispose();
}
}

this.isDisposed = true;
}
}
}
}

0 comments on commit c1ac4c7

Please sign in to comment.