Skip to content

Commit

Permalink
Adding BeforeRemoteHttpRequest event to .NET RemoteWebDriver
Browse files Browse the repository at this point in the history
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
}
  • Loading branch information
jimevans committed Mar 27, 2018
1 parent 8910104 commit a3a2062
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
38 changes: 38 additions & 0 deletions dotnet/src/webdriver/Remote/BeforeRemoteHttpRequestEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// <copyright file="BeforeRemoteHttpRequestEventArgs.cs" company="WebDriver Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Net;

namespace OpenQA.Selenium.Remote
{
public class BeforeRemoteHttpRequestEventArgs : EventArgs
{
private HttpWebRequest request;

public BeforeRemoteHttpRequestEventArgs(HttpWebRequest request)
{
this.request = request;
}

public HttpWebRequest Request
{
get { return this.request; }
}
}
}
31 changes: 26 additions & 5 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena
{
HttpWebRequest.DefaultMaximumErrorResponseLength = -1;
}
}
}

/// <summary>
/// Gets the repository of objects containin information about commands.
/// </summary>
public CommandInfoRepository CommandInfoRepository
public event EventHandler<BeforeRemoteHttpRequestEventArgs> BeforeRemoteHttpRequest;

/// <summary>
/// Gets the repository of objects containin information about commands.
/// </summary>
public CommandInfoRepository CommandInfoRepository
{
get { return this.commandInfoRepository; }
}
Expand Down Expand Up @@ -128,6 +130,23 @@ public virtual Response Execute(Command commandToExecute)
return toReturn;
}

/// <summary>
/// Raises the <see cref="BeforeRemoteHttpRequest"/> event.
/// </summary>
/// <param name="eventArgs">A <see cref="BeforeRemoteHttpRequestEventArgs"/> that contains the event data.</param>
protected virtual void OnBeforeRemoteHttpRequest(BeforeRemoteHttpRequestEventArgs eventArgs)
{
if (eventArgs == null)
{
throw new ArgumentNullException("eventArgs", "eventArgs must not be null");
}

if (this.BeforeRemoteHttpRequest != null)
{
this.BeforeRemoteHttpRequest(this, eventArgs);
}
}

private static string GetTextOfWebResponse(HttpWebResponse webResponse)
{
// StreamReader.Close also closes the underlying stream.
Expand Down Expand Up @@ -176,6 +195,8 @@ private HttpResponseInfo MakeHttpRequest(HttpRequestInfo requestInfo)
request.Headers.Add("Cache-Control", "no-cache");
}

this.OnBeforeRemoteHttpRequest(new BeforeRemoteHttpRequestEventArgs(request));

HttpResponseInfo responseInfo = new HttpResponseInfo();
HttpWebResponse webResponse = null;
try
Expand Down

0 comments on commit a3a2062

Please sign in to comment.