Skip to content

Commit a3a2062

Browse files
committed
Adding BeforeRemoteHttpRequest event to .NET RemoteWebDriver
This commit adds the ability for advanced users of the .NET bindings RemoteWebDriver class to modify the HTTP command sent from the .NET WebDriver code to the remote WebDriver server. Note carefully that this **DOES NOT** modify the HTTP request between the browser and the web server, and is not a mechanism for getting access to that HTTP request. Usage of this new event would look something like this: public void StartDriver() { HttpCommandExecutor executor = new HttpCommandExecutor( new Url("http://localhost:4444/wd/hub"), TimeSpan.FromSeconds(60)); executor.BeforeRemoteHttpRequest += BeforeRemoteHttpRequestHandler; ICapabilities capabilities = new ChromeOptions().ToCapabilities(); IWebDriver driver = new RemoteWebDriver(executor, capabilities); } public void BeforeRemoteHttpRequestHandler( object sender, BeforeRemoteHttpRequestEventArgs e) { // Note: WebRequest.DefaultWebProxy is an IWebProxy implementation. // This could be anything, from adding additional headers, to modifying // the content of the request. Use with extreme caution. e.Request.Proxy = WebRequest.DefaultWebProxy; e.Request.AutomaticDecompression = DecompressionMethods.GZip }
1 parent 8910104 commit a3a2062

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// <copyright file="BeforeRemoteHttpRequestEventArgs.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
using System.Net;
21+
22+
namespace OpenQA.Selenium.Remote
23+
{
24+
public class BeforeRemoteHttpRequestEventArgs : EventArgs
25+
{
26+
private HttpWebRequest request;
27+
28+
public BeforeRemoteHttpRequestEventArgs(HttpWebRequest request)
29+
{
30+
this.request = request;
31+
}
32+
33+
public HttpWebRequest Request
34+
{
35+
get { return this.request; }
36+
}
37+
}
38+
}

dotnet/src/webdriver/Remote/HttpCommandExecutor.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena
8484
{
8585
HttpWebRequest.DefaultMaximumErrorResponseLength = -1;
8686
}
87-
}
87+
}
8888

89-
/// <summary>
90-
/// Gets the repository of objects containin information about commands.
91-
/// </summary>
92-
public CommandInfoRepository CommandInfoRepository
89+
public event EventHandler<BeforeRemoteHttpRequestEventArgs> BeforeRemoteHttpRequest;
90+
91+
/// <summary>
92+
/// Gets the repository of objects containin information about commands.
93+
/// </summary>
94+
public CommandInfoRepository CommandInfoRepository
9395
{
9496
get { return this.commandInfoRepository; }
9597
}
@@ -128,6 +130,23 @@ public virtual Response Execute(Command commandToExecute)
128130
return toReturn;
129131
}
130132

133+
/// <summary>
134+
/// Raises the <see cref="BeforeRemoteHttpRequest"/> event.
135+
/// </summary>
136+
/// <param name="eventArgs">A <see cref="BeforeRemoteHttpRequestEventArgs"/> that contains the event data.</param>
137+
protected virtual void OnBeforeRemoteHttpRequest(BeforeRemoteHttpRequestEventArgs eventArgs)
138+
{
139+
if (eventArgs == null)
140+
{
141+
throw new ArgumentNullException("eventArgs", "eventArgs must not be null");
142+
}
143+
144+
if (this.BeforeRemoteHttpRequest != null)
145+
{
146+
this.BeforeRemoteHttpRequest(this, eventArgs);
147+
}
148+
}
149+
131150
private static string GetTextOfWebResponse(HttpWebResponse webResponse)
132151
{
133152
// StreamReader.Close also closes the underlying stream.
@@ -176,6 +195,8 @@ private HttpResponseInfo MakeHttpRequest(HttpRequestInfo requestInfo)
176195
request.Headers.Add("Cache-Control", "no-cache");
177196
}
178197

198+
this.OnBeforeRemoteHttpRequest(new BeforeRemoteHttpRequestEventArgs(request));
199+
179200
HttpResponseInfo responseInfo = new HttpResponseInfo();
180201
HttpWebResponse webResponse = null;
181202
try

0 commit comments

Comments
 (0)