Skip to content

Commit

Permalink
[dotnet] Add proxying of CDP commands via remote/grid
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Jun 4, 2021
1 parent dca5756 commit f10cb89
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
38 changes: 26 additions & 12 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public DevToolsSession(string endpointAddress)

CommandTimeout = TimeSpan.FromSeconds(5);
this.debuggerEndpoint = endpointAddress;
if (endpointAddress.StartsWith("ws:"))
{
this.websocketAddress = endpointAddress;
}

sessionSocket = new ClientWebSocket();
sessionSocket.Options.KeepAliveInterval = TimeSpan.Zero;
Expand Down Expand Up @@ -249,23 +253,33 @@ public T GetVersionSpecificDomains<T>() where T: DevToolsSessionDomains
/// <returns>A task that represents the asynchronous operation.</returns>
internal async Task Start(int protocolVersion)
{
string debuggerUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}", this.debuggerEndpoint);
string rawVersionInfo = string.Empty;
using (HttpClient client = new HttpClient())
if (this.websocketAddress == null)
{
client.BaseAddress = new Uri(debuggerUrl);
rawVersionInfo = await client.GetStringAsync("/json/version");
}
string debuggerUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}", this.debuggerEndpoint);
string rawVersionInfo = string.Empty;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(debuggerUrl);
rawVersionInfo = await client.GetStringAsync("/json/version");
}

var versionInfo = JsonConvert.DeserializeObject<DevToolsVersionInfo>(rawVersionInfo);
websocketAddress = versionInfo.WebSocketDebuggerUrl;
var versionInfo = JsonConvert.DeserializeObject<DevToolsVersionInfo>(rawVersionInfo);
this.websocketAddress = versionInfo.WebSocketDebuggerUrl;

if (protocolVersion == AutoDetectDevToolsProtocolVersion)
if (protocolVersion == AutoDetectDevToolsProtocolVersion)
{
bool versionParsed = int.TryParse(versionInfo.BrowserMajorVersion, out protocolVersion);
if (!versionParsed)
{
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Unable to parse version number received from browser. Reported browser version string is '{0}'", versionInfo.Browser));
}
}
}
else
{
bool versionParsed = int.TryParse(versionInfo.BrowserMajorVersion, out protocolVersion);
if (!versionParsed)
if (protocolVersion == AutoDetectDevToolsProtocolVersion)
{
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Unable to parse version number received from browser. Reported browser version string is '{0}'", versionInfo.Browser));
throw new WebDriverException("A WebSocket address for DevTools protocol has been detected, but the protocol version cannot be automatically detected. You must specify a protocol version.");
}
}

Expand Down
53 changes: 49 additions & 4 deletions dotnet/src/webdriver/Remote/RemoteWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.Html5;
using OpenQA.Selenium.Internal;

Expand Down Expand Up @@ -57,13 +58,14 @@ namespace OpenQA.Selenium.Remote
/// }
/// </code>
/// </example>
public class RemoteWebDriver : WebDriver, IFindsById, IFindsByClassName, IFindsByLinkText, IFindsByName, IFindsByTagName, IFindsByXPath, IFindsByPartialLinkText, IFindsByCssSelector
public class RemoteWebDriver : WebDriver, IDevTools, IFindsById, IFindsByClassName, IFindsByLinkText, IFindsByName, IFindsByTagName, IFindsByXPath, IFindsByPartialLinkText, IFindsByCssSelector
{
public readonly string RemoteDevToolsEndPointCapabilityName = "se:cdp";
public readonly string RemoteDevToolsVersionCapabilityName = "se:cdpVersion";

private const string DefaultRemoteServerUrl = "http://127.0.0.1:4444/wd/hub";

private IWebStorage storage;
private IApplicationCache appCache;
private ILocationContext locationContext;
private DevToolsSession devToolsSession;

/// <summary>
/// Initializes a new instance of the <see cref="RemoteWebDriver"/> class. This constructor defaults proxy to http://127.0.0.1:4444/wd/hub
Expand Down Expand Up @@ -399,6 +401,49 @@ public ReadOnlyCollection<IWebElement> FindElementsByCssSelector(string cssSelec
return this.FindElements("css selector", cssSelector);
}

public DevToolsSession GetDevToolsSession()
{
return GetDevToolsSession(DevToolsSession.AutoDetectDevToolsProtocolVersion);
}

public DevToolsSession GetDevToolsSession(int protocolVersion)
{
if (this.devToolsSession == null)
{
if (!this.Capabilities.HasCapability(RemoteDevToolsEndPointCapabilityName))
{
throw new WebDriverException("Cannot find " + RemoteDevToolsEndPointCapabilityName + " capability for driver");
}

if (!this.Capabilities.HasCapability(RemoteDevToolsVersionCapabilityName))
{
throw new WebDriverException("Cannot find " + RemoteDevToolsVersionCapabilityName + " capability for driver");
}

string debuggerAddress = this.Capabilities.GetCapability(RemoteDevToolsEndPointCapabilityName).ToString();
string version = this.Capabilities.GetCapability(RemoteDevToolsVersionCapabilityName).ToString();

bool versionParsed = int.TryParse(version.Substring(0, version.IndexOf(".")), out int devToolsProtocolVersion);
if (!versionParsed)
{
throw new WebDriverException("Cannot parse protocol version from reported version string: " + version);
}

try
{
DevToolsSession session = new DevToolsSession(debuggerAddress);
session.Start(devToolsProtocolVersion).ConfigureAwait(false).GetAwaiter().GetResult();
this.devToolsSession = session;
}
catch (Exception e)
{
throw new WebDriverException("Unexpected error creating WebSocket DevTools session.", e);
}
}

return this.devToolsSession;
}

private static ICapabilities ConvertOptionsToCapabilities(DriverOptions options)
{
if (options == null)
Expand Down
6 changes: 3 additions & 3 deletions dotnet/test/common/appconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@
},

"Remote": {
"DriverTypeName": "OpenQA.Selenium.Remote.TestInternetExplorerRemoteWebDriver",
"AssemblyName": "WebDriver.Remote.Tests",
"DriverTypeName": "OpenQA.Selenium.Remote.StableChannelRemoteChromeDriver",
"AssemblyName": "WebDriver.Common.Tests",
"BrowserValue": "Remote",
"RemoteCapabilities": "internet explorer",
"RemoteCapabilities": "chrome",
"AutoStartRemoteServer": true
}
}
Expand Down

0 comments on commit f10cb89

Please sign in to comment.