My C# code looks like this for creating chrome web driver, i wanted to add the custom HTTP headers to all my http requests.
ex: user-agent : Android
var service = ChromeDriverService.CreateDefaultService(@"c:\Chrome\");
var option = new ChromeOptions();
_driver = new ChromeDriver(service, option);
We have the way for firefox, as the link shows, but for chrome it does not work. https://eveningsamurai.wordpress.com/2013/11/21/changing-http-headers-for-a-selenium-webdriver-request/
Any help appreciated
I've been able to manage that using ModHeaders Chrome extension. Download the plugin CRX file and load it in your test Chrome instance.
var options = new ChromeOptions();
options.AddExtension("WebDrivers/modHeader_2_1_1.crx");
var driver = new ChromeDriver(options);
Then you can configure the plugin using the local storage as this is where the plugins stores its config too.
// set the context to access extension local storage
Configuration.driver.Navigate().GoToUrl("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/icon.png");
Configuration.driver.ExecuteScript(
"localStorage.setItem('profiles', JSON.stringify([{ " +
" title: 'Selenium', hideComment: true, appendMode: '', " +
" headers: [ " +
" {enabled: true, name: 'MY_HEADER', value: 'MY_VALUE', comment: ''} " +
" ], " +
" respHeaders: [], " +
" filters: [] " +
"}])); ");
And finally you can navigate to somewhere and check that the headers are loaded
Configuration.driver.Navigate().GoToUrl("http://example.com/");
One way to handle this case is with FiddlerCore proxy, capture all the requests and modify the header as part of request. https://www.nuget.org/packages/FiddlerCore/
Nice blog about fiddler core http://weblog.west-wind.com/posts/2014/Jul/29/Using-FiddlerCore-to-capture-HTTP-Requests-with-NET
public static void Start()
{
FiddlerApplication.RequestHeadersAvailable += FiddlerApplication_RequestHeadersAvailable;
FiddlerApplication.Startup(8888, true, true, true);
}
static void FiddlerApplication_RequestHeadersAvailable(Session oSession)
{
oSession.RequestHeaders.Add("My_Custom_Header", "XXXXXXXXXXXXXXXX");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With