Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP [dotnet] Reusable DriverService instance by Drivers #14662

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
17 changes: 14 additions & 3 deletions dotnet/src/webdriver/Chrome/ChromeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public ChromeDriver(string chromeDriverDirectory, ChromeOptions options)
/// <param name="options">The <see cref="ChromeOptions"/> to be used with the Chrome driver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public ChromeDriver(string chromeDriverDirectory, ChromeOptions options, TimeSpan commandTimeout)
: this(ChromeDriverService.CreateDefaultService(chromeDriverDirectory), options, commandTimeout)
: this(ChromeDriverService.CreateDefaultService(chromeDriverDirectory), options, commandTimeout, disposeService: true)
{
}

Expand All @@ -136,7 +136,12 @@ public ChromeDriver(string chromeDriverDirectory, ChromeOptions options, TimeSpa
/// <param name="service">The <see cref="ChromeDriverService"/> to use.</param>
/// <param name="options">The <see cref="ChromeOptions"/> used to initialize the driver.</param>
public ChromeDriver(ChromeDriverService service, ChromeOptions options)
: this(service, options, RemoteWebDriver.DefaultCommandTimeout)
: this(service, options, RemoteWebDriver.DefaultCommandTimeout, disposeService: true)
{
}

public ChromeDriver(ChromeDriverService service, ChromeOptions options, bool disposeService)
: this(service, options, RemoteWebDriver.DefaultCommandTimeout, disposeService)
{
}

Expand All @@ -147,7 +152,13 @@ public ChromeDriver(ChromeDriverService service, ChromeOptions options)
/// <param name="options">The <see cref="ChromeOptions"/> to be used with the Chrome driver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public ChromeDriver(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
: base(service, options, commandTimeout)
: base(service, options, commandTimeout, disposeService: true)
{
this.AddCustomChromeCommands();
}

public ChromeDriver(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout, bool disposeService)
: base(service, options, commandTimeout, disposeService)
{
this.AddCustomChromeCommands();
}
Expand Down
27 changes: 16 additions & 11 deletions dotnet/src/webdriver/Chromium/ChromiumDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public class ChromiumDriver : WebDriver, ISupportsLogs, IDevTools
/// </summary>
public static readonly string SetPermissionCommand = "setPermission";

private readonly ChromiumDriverService driverService;
private readonly bool disposeDriverService;
private readonly string optionsCapabilityName;
private DevToolsSession devToolsSession;

Expand All @@ -126,9 +128,11 @@ public class ChromiumDriver : WebDriver, ISupportsLogs, IDevTools
/// <param name="service">The <see cref="ChromiumDriverService"/> to use.</param>
/// <param name="options">The <see cref="ChromiumOptions"/> to be used with the ChromiumDriver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
protected ChromiumDriver(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
: base(GenerateDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
protected ChromiumDriver(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout, bool disposeService)
: base(StartDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
{
this.driverService = service;
this.disposeDriverService = disposeService;
this.optionsCapabilityName = options.CapabilityName;
}

Expand All @@ -140,14 +144,7 @@ protected static IReadOnlyDictionary<string, CommandInfo> ChromiumCustomCommands
get { return new ReadOnlyDictionary<string, CommandInfo>(chromiumCustomCommands); }
}

/// <summary>
/// Uses DriverFinder to set Service attributes if necessary when creating the command executor
/// </summary>
/// <param name="service"></param>
/// <param name="commandTimeout"></param>
/// <param name="options"></param>
/// <returns></returns>
private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
private static ICommandExecutor StartDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
{
if (service.DriverServicePath == null)
{
Expand All @@ -161,7 +158,10 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
options.BrowserVersion = null;
}
}
return new DriverServiceCommandExecutor(service, commandTimeout);

service.Start();

return new HttpCommandExecutor(service.ServiceUrl, commandTimeout);
}

/// <summary>
Expand Down Expand Up @@ -471,6 +471,11 @@ protected override void Dispose(bool disposing)
}

base.Dispose(disposing);

if (this.disposeDriverService)
{
this.driverService?.Dispose();
}
}

private static ICapabilities ConvertOptionsToCapabilities(ChromiumOptions options)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/Edge/EdgeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public EdgeDriver(EdgeDriverService service, EdgeOptions options)
/// <param name="options">The <see cref="EdgeOptions"/> to be used with the Edge driver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public EdgeDriver(EdgeDriverService service, EdgeOptions options, TimeSpan commandTimeout)
: base(service, options, commandTimeout)
: base(service, options, commandTimeout, disposeService: false)
{
this.AddCustomEdgeCommands();
}
Expand Down
9 changes: 6 additions & 3 deletions dotnet/src/webdriver/Firefox/FirefoxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options)
/// <param name="options">The <see cref="FirefoxOptions"/> to be used with the Firefox driver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options, TimeSpan commandTimeout)
: base(GenerateDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
: base(StartDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
{
// Add the custom commands unique to Firefox
this.AddCustomFirefoxCommands();
Expand All @@ -200,7 +200,7 @@ public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options, TimeS
/// <param name="commandTimeout"></param>
/// <param name="options"></param>
/// <returns></returns>
private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
private static ICommandExecutor StartDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
{
if (service.DriverServicePath == null)
{
Expand All @@ -214,7 +214,10 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
options.BrowserVersion = null;
}
}
return new DriverServiceCommandExecutor(service, commandTimeout);

service.Start();

return new HttpCommandExecutor(service.ServiceUrl, commandTimeout);
}

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions dotnet/src/webdriver/IE/InternetExplorerDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public InternetExplorerDriver(InternetExplorerDriverService service, InternetExp
/// <param name="options">The <see cref="InternetExplorerOptions"/> used to initialize the driver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public InternetExplorerDriver(InternetExplorerDriverService service, InternetExplorerOptions options, TimeSpan commandTimeout)
: base(GenerateDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
: base(StartDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
{
}

Expand All @@ -152,7 +152,7 @@ public InternetExplorerDriver(InternetExplorerDriverService service, InternetExp
/// <param name="commandTimeout"></param>
/// <param name="options"></param>
/// <returns></returns>
private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
private static ICommandExecutor StartDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
{
if (service.DriverServicePath == null)
{
Expand All @@ -161,7 +161,10 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
service.DriverServicePath = Path.GetDirectoryName(fullServicePath);
service.DriverServiceExecutableName = Path.GetFileName(fullServicePath);
}
return new DriverServiceCommandExecutor(service, commandTimeout);

service.Start();

return new HttpCommandExecutor(service.ServiceUrl, commandTimeout);
}

/// <summary>
Expand Down
163 changes: 0 additions & 163 deletions dotnet/src/webdriver/Remote/DriverServiceCommandExecutor.cs

This file was deleted.

9 changes: 6 additions & 3 deletions dotnet/src/webdriver/Safari/SafariDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public SafariDriver(SafariDriverService service, SafariOptions options)
/// <param name="options">The <see cref="SafariOptions"/> to be used with the Safari driver.</param>
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
public SafariDriver(SafariDriverService service, SafariOptions options, TimeSpan commandTimeout)
: base(GenerateDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
: base(StartDriverServiceCommandExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
{
this.AddCustomSafariCommand(AttachDebuggerCommand, HttpCommandInfo.PostCommand, "/session/{sessionId}/apple/attach_debugger");
this.AddCustomSafariCommand(GetPermissionsCommand, HttpCommandInfo.GetCommand, "/session/{sessionId}/apple/permissions");
Expand All @@ -158,7 +158,7 @@ public SafariDriver(SafariDriverService service, SafariOptions options, TimeSpan
/// <param name="commandTimeout"></param>
/// <param name="options"></param>
/// <returns></returns>
private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
private static ICommandExecutor StartDriverServiceCommandExecutor(DriverService service, DriverOptions options, TimeSpan commandTimeout)
{
if (service.DriverServicePath == null)
{
Expand All @@ -167,7 +167,10 @@ private static ICommandExecutor GenerateDriverServiceCommandExecutor(DriverServi
service.DriverServicePath = Path.GetDirectoryName(fullServicePath);
service.DriverServiceExecutableName = Path.GetFileName(fullServicePath);
}
return new DriverServiceCommandExecutor(service, commandTimeout);

service.Start();

return new HttpCommandExecutor(service.ServiceUrl, commandTimeout);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions dotnet/src/webdriver/WebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ protected virtual void Dispose(bool disposing)
{
this.sessionId = null;
}

this.executor.Dispose();
}

Expand Down
3 changes: 1 addition & 2 deletions dotnet/test/common/Environment/DriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
}
}

driver = (IWebDriver)Activator.CreateInstance(driverType);
driver = (IWebDriver)Activator.CreateInstance(driverType, service, true);
return driver;
}

Expand Down Expand Up @@ -193,7 +193,6 @@ protected void OnDriverLaunching(DriverService service, DriverOptions options)
return options;
}


private T MergeOptions<T>(object baseOptions, DriverOptions overriddenOptions) where T : DriverOptions, new()
{
// If the driver type has a static DefaultOptions property,
Expand Down
Loading