3

Can anyone provide a proper example on how to configure BrowserMobProxy with Selenium Webdriver in C#? Everywhere i search, it mostly provides samples using Java whereas our framework is in C# and on top i want to use BrowserMobPRoxy.This is so I can capture network traffic for my web application inorder to measure performance

Aarthy
  • 71
  • 1
  • 5

1 Answers1

1

Been trying to solve this as well, the nuget didnt work with .net core. But the api is still an API so its reachable from c# code as well. So with this you set up the proxy yourself. I did the proxy start in powershell but it can be done in c# code as well.

Download BrowserMobProxy at github https://github.com/lightbody/browsermob-proxy

Start the bat file like this, powershell e.g: .\browsermob -port 8080

Do some api calls. I used restsharp:

Create the proxy instance

var client = new RestClient("http://localhost:8080/proxy");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "text/plain");
var body = @" {""port"":8081}";
request.AddParameter("text/plain", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Create Har File so it starts saving information:

var client = new RestClient("http://localhost:8080/proxy/8081/har");
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
IRestResponse response = client.Execute(request);

Use the following code for setting up the proxy with selenium:

ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.SslProxy = "http://localhost:8081";
options.Proxy = proxy;
options.AddArgument("ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(options);

Start the test

driver.Navigate().GoToUrl("https://www.selenium.dev/");

Get the har file

var client = new RestClient("http://localhost:8080/proxy/8081/har");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);