Skip to content

Commit f10cb89

Browse files
committed
[dotnet] Add proxying of CDP commands via remote/grid
1 parent dca5756 commit f10cb89

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

dotnet/src/webdriver/DevTools/DevToolsSession.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public DevToolsSession(string endpointAddress)
6464

6565
CommandTimeout = TimeSpan.FromSeconds(5);
6666
this.debuggerEndpoint = endpointAddress;
67+
if (endpointAddress.StartsWith("ws:"))
68+
{
69+
this.websocketAddress = endpointAddress;
70+
}
6771

6872
sessionSocket = new ClientWebSocket();
6973
sessionSocket.Options.KeepAliveInterval = TimeSpan.Zero;
@@ -249,23 +253,33 @@ public T GetVersionSpecificDomains<T>() where T: DevToolsSessionDomains
249253
/// <returns>A task that represents the asynchronous operation.</returns>
250254
internal async Task Start(int protocolVersion)
251255
{
252-
string debuggerUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}", this.debuggerEndpoint);
253-
string rawVersionInfo = string.Empty;
254-
using (HttpClient client = new HttpClient())
256+
if (this.websocketAddress == null)
255257
{
256-
client.BaseAddress = new Uri(debuggerUrl);
257-
rawVersionInfo = await client.GetStringAsync("/json/version");
258-
}
258+
string debuggerUrl = string.Format(CultureInfo.InvariantCulture, "http://{0}", this.debuggerEndpoint);
259+
string rawVersionInfo = string.Empty;
260+
using (HttpClient client = new HttpClient())
261+
{
262+
client.BaseAddress = new Uri(debuggerUrl);
263+
rawVersionInfo = await client.GetStringAsync("/json/version");
264+
}
259265

260-
var versionInfo = JsonConvert.DeserializeObject<DevToolsVersionInfo>(rawVersionInfo);
261-
websocketAddress = versionInfo.WebSocketDebuggerUrl;
266+
var versionInfo = JsonConvert.DeserializeObject<DevToolsVersionInfo>(rawVersionInfo);
267+
this.websocketAddress = versionInfo.WebSocketDebuggerUrl;
262268

263-
if (protocolVersion == AutoDetectDevToolsProtocolVersion)
269+
if (protocolVersion == AutoDetectDevToolsProtocolVersion)
270+
{
271+
bool versionParsed = int.TryParse(versionInfo.BrowserMajorVersion, out protocolVersion);
272+
if (!versionParsed)
273+
{
274+
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Unable to parse version number received from browser. Reported browser version string is '{0}'", versionInfo.Browser));
275+
}
276+
}
277+
}
278+
else
264279
{
265-
bool versionParsed = int.TryParse(versionInfo.BrowserMajorVersion, out protocolVersion);
266-
if (!versionParsed)
280+
if (protocolVersion == AutoDetectDevToolsProtocolVersion)
267281
{
268-
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Unable to parse version number received from browser. Reported browser version string is '{0}'", versionInfo.Browser));
282+
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.");
269283
}
270284
}
271285

dotnet/src/webdriver/Remote/RemoteWebDriver.cs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using System.Collections.Generic;
2121
using System.Collections.ObjectModel;
22+
using OpenQA.Selenium.DevTools;
2223
using OpenQA.Selenium.Html5;
2324
using OpenQA.Selenium.Internal;
2425

@@ -57,13 +58,14 @@ namespace OpenQA.Selenium.Remote
5758
/// }
5859
/// </code>
5960
/// </example>
60-
public class RemoteWebDriver : WebDriver, IFindsById, IFindsByClassName, IFindsByLinkText, IFindsByName, IFindsByTagName, IFindsByXPath, IFindsByPartialLinkText, IFindsByCssSelector
61+
public class RemoteWebDriver : WebDriver, IDevTools, IFindsById, IFindsByClassName, IFindsByLinkText, IFindsByName, IFindsByTagName, IFindsByXPath, IFindsByPartialLinkText, IFindsByCssSelector
6162
{
63+
public readonly string RemoteDevToolsEndPointCapabilityName = "se:cdp";
64+
public readonly string RemoteDevToolsVersionCapabilityName = "se:cdpVersion";
65+
6266
private const string DefaultRemoteServerUrl = "http://127.0.0.1:4444/wd/hub";
6367

64-
private IWebStorage storage;
65-
private IApplicationCache appCache;
66-
private ILocationContext locationContext;
68+
private DevToolsSession devToolsSession;
6769

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

404+
public DevToolsSession GetDevToolsSession()
405+
{
406+
return GetDevToolsSession(DevToolsSession.AutoDetectDevToolsProtocolVersion);
407+
}
408+
409+
public DevToolsSession GetDevToolsSession(int protocolVersion)
410+
{
411+
if (this.devToolsSession == null)
412+
{
413+
if (!this.Capabilities.HasCapability(RemoteDevToolsEndPointCapabilityName))
414+
{
415+
throw new WebDriverException("Cannot find " + RemoteDevToolsEndPointCapabilityName + " capability for driver");
416+
}
417+
418+
if (!this.Capabilities.HasCapability(RemoteDevToolsVersionCapabilityName))
419+
{
420+
throw new WebDriverException("Cannot find " + RemoteDevToolsVersionCapabilityName + " capability for driver");
421+
}
422+
423+
string debuggerAddress = this.Capabilities.GetCapability(RemoteDevToolsEndPointCapabilityName).ToString();
424+
string version = this.Capabilities.GetCapability(RemoteDevToolsVersionCapabilityName).ToString();
425+
426+
bool versionParsed = int.TryParse(version.Substring(0, version.IndexOf(".")), out int devToolsProtocolVersion);
427+
if (!versionParsed)
428+
{
429+
throw new WebDriverException("Cannot parse protocol version from reported version string: " + version);
430+
}
431+
432+
try
433+
{
434+
DevToolsSession session = new DevToolsSession(debuggerAddress);
435+
session.Start(devToolsProtocolVersion).ConfigureAwait(false).GetAwaiter().GetResult();
436+
this.devToolsSession = session;
437+
}
438+
catch (Exception e)
439+
{
440+
throw new WebDriverException("Unexpected error creating WebSocket DevTools session.", e);
441+
}
442+
}
443+
444+
return this.devToolsSession;
445+
}
446+
402447
private static ICapabilities ConvertOptionsToCapabilities(DriverOptions options)
403448
{
404449
if (options == null)

dotnet/test/common/appconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@
9595
},
9696

9797
"Remote": {
98-
"DriverTypeName": "OpenQA.Selenium.Remote.TestInternetExplorerRemoteWebDriver",
99-
"AssemblyName": "WebDriver.Remote.Tests",
98+
"DriverTypeName": "OpenQA.Selenium.Remote.StableChannelRemoteChromeDriver",
99+
"AssemblyName": "WebDriver.Common.Tests",
100100
"BrowserValue": "Remote",
101-
"RemoteCapabilities": "internet explorer",
101+
"RemoteCapabilities": "chrome",
102102
"AutoStartRemoteServer": true
103103
}
104104
}

0 commit comments

Comments
 (0)