I needed to send an Http request with a few modified headers. After several hours trying to find an equivalent method to that of Selenium RC Selenium.addCustomRequestH
I encountered the same issue 2 weeks ago. I tried the approach suggested by Carl but it seemed a bit to much "overhead" to realize this task.
In the end I used the fiddlerCore library in order to host a proxy server in my code and just use the build in proxy feature of web driver 2. It works pretty good and is much more intuitive/stable in my opinion. Furthermore it works for all browsers and you don't depend on binary files which you need to maintain inside your code repository.
Here an example made in C# for Chrome (Firefox and IE are very similar)
// Check if the server is already running
if (!FiddlerApplication.IsStarted())
{
// Append your delegate to the BeforeRequest event
FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)
{
// Make the modifications needed - change header, logging, ...
oS.oRequest["SM_USER"] = SeleniumSettings.TestUser;
};
// Startup the proxy server
FiddlerApplication.Startup(SeleniumSettings.ProxyPort, false, false);
}
// Create the proxy setting for the browser
var proxy = new Proxy();
proxy.HttpProxy = string.Format("localhost:{0}", SeleniumSettings.ProxyPort);
// Create ChromeOptions object and add the setting
var chromeOptions = new ChromeOptions();
chromeOptions.Proxy = proxy;
// Create the driver
var Driver = new ChromeDriver(path, chromeOptions);
// Afterwards shutdown the proxy server if it's running
if (FiddlerApplication.IsStarted())
{
FiddlerApplication.Shutdown();
}