Skip to content

Commit c1ac4c7

Browse files
[dotnet] IJavascriptEngine implements IDisposable where available (#11594)
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.
1 parent e11ab24 commit c1ac4c7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

dotnet/src/webdriver/IJavaScriptEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace OpenQA.Selenium
2525
/// <summary>
2626
/// Defines an interface allowing the user to manage settings in the browser's JavaScript engine.
2727
/// </summary>
28-
public interface IJavaScriptEngine
28+
public interface IJavaScriptEngine : IDisposable
2929
{
3030
/// <summary>
3131
/// Occurs when a JavaScript callback with a named binding is executed.

dotnet/src/webdriver/JavaScriptEngine.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class JavaScriptEngine : IJavaScriptEngine
4040
private Dictionary<string, PinnedScript> pinnedScripts = new Dictionary<string, PinnedScript>();
4141
private List<string> bindings = new List<string>();
4242
private bool isEnabled = false;
43+
private bool isDisposed = false;
4344

4445
/// <summary>
4546
/// Initializes a new instance of the <see cref="JavaScriptEngine"/> class.
@@ -394,5 +395,26 @@ private void OnConsoleApiCalled(object sender, ConsoleApiCalledEventArgs e)
394395
});
395396
}
396397
}
398+
399+
public void Dispose()
400+
{
401+
this.Dispose(true);
402+
}
403+
404+
protected virtual void Dispose(bool disposing)
405+
{
406+
if (!this.isDisposed)
407+
{
408+
if (disposing)
409+
{
410+
if (this.session.IsValueCreated)
411+
{
412+
this.session.Value.Dispose();
413+
}
414+
}
415+
416+
this.isDisposed = true;
417+
}
418+
}
397419
}
398420
}

0 commit comments

Comments
 (0)