Skip to content

Instantly share code, notes, and snippets.

@jimevans
Created April 2, 2021 14:24
Show Gist options
  • Save jimevans/c5ee39b81248f0717e1996741bfc6a19 to your computer and use it in GitHub Desktop.
Save jimevans/c5ee39b81248f0717e1996741bfc6a19 to your computer and use it in GitHub Desktop.
public async Task TestNetworkInterceptor(IWebDriver driver)
{
NetworkRequestHandler handler = new NetworkRequestHandler()
{
RequestMatcher = (request) =>
{
return request.Url.Contains("the-internet.herokuapp.com");
},
ResponseSupplier = (request) =>
{
var response = new HttpResponseData();
response.StatusCode = 200;
foreach (var header in request.Headers)
{
response.Headers.Add(header.Key, header.Value);
}
response.Body = "<p>Intercepted from The Internet</p>";
return response;
}
};
INetwork interceptor = driver.Manage().Network;
await interceptor.StartMonitoring();
driver.Url = "http://the-internet.herokuapp.com/";
string bodyText = driver.FindElement(By.CssSelector("h1")).Text;
if (bodyText.Contains("Welcome to the-internet"))
{
Console.WriteLine("Interception without handler PASSED");
}
else
{
Console.WriteLine("Interception without handler FAILED");
}
interceptor.AddRequestHandler(handler);
driver.Url = "http://the-internet.herokuapp.com/checkboxes";
string text = driver.FindElement(By.CssSelector("p")).Text;
if (text == "Intercepted from The Internet")
{
Console.WriteLine("Interception with handler PASSED");
}
else
{
Console.WriteLine("Interception with handler FAILED");
}
await interceptor.StopMonitoring();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment