-
-
Save pqviet07/3af8bfa959b799d2dc3403323c4795f4 to your computer and use it in GitHub Desktop.
Selenium C# network traffic logging example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task LogNetworkRequests(IWebDriver driver) | |
{ | |
INetwork interceptor = driver.Manage().Network; | |
interceptor.NetworkRequestSent += OnNetworkRequestSent; | |
interceptor.NetworkResponseReceived += OnNetworkResponseReceived; | |
await interceptor.StartMonitoring(); | |
driver.Url = "http://the-internet.herokuapp.com/redirect"; | |
await interceptor.StopMonitoring(); | |
} | |
private void OnNetworkRequestSent(object sender, NetworkRequestSentEventArgs e) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.AppendFormat("Request {0}", e.RequestId).AppendLine(); | |
builder.AppendLine("--------------------------------"); | |
builder.AppendFormat("{0} {1}", e.RequestMethod, e.RequestUrl).AppendLine(); | |
foreach (KeyValuePair<string, string> header in e.RequestHeaders) | |
{ | |
builder.AppendFormat("{0}: {1}", header.Key, header.Value).AppendLine(); | |
} | |
builder.AppendLine("--------------------------------"); | |
builder.AppendLine(); | |
Console.WriteLine(builder.ToString()); | |
} | |
private void OnNetworkResponseReceived(object sender, NetworkResponseReceivedEventArgs e) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
builder.AppendFormat("Response {0}", e.RequestId).AppendLine(); | |
builder.AppendLine("--------------------------------"); | |
builder.AppendFormat("{0} {1}", e.ResponseStatusCode, e.ResponseUrl).AppendLine(); | |
foreach (KeyValuePair<string, string> header in e.ResponseHeaders) | |
{ | |
builder.AppendFormat("{0}: {1}", header.Key, header.Value).AppendLine(); | |
} | |
if (e.ResponseResourceType == "Document") | |
{ | |
builder.AppendLine(e.ResponseBody); | |
} | |
else if (e.ResponseResourceType == "Script") | |
{ | |
builder.AppendLine("<JavaScript content>"); | |
} | |
else if (e.ResponseResourceType == "Stylesheet") | |
{ | |
builder.AppendLine("<stylesheet content>"); | |
} | |
else if (e.ResponseResourceType == "Image") | |
{ | |
builder.AppendLine("<image>"); | |
} | |
else | |
{ | |
builder.AppendFormat("Content type: {0}", e.ResponseResourceType).AppendLine(); | |
} | |
builder.AppendLine("--------------------------------"); | |
Console.WriteLine(builder.ToString()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using OpenQA.Selenium; | |
// Example from selenium | |
namespace SeleniumDocs.ChromeDevTools | |
{ | |
[TestClass] | |
public class NetworkInterceptorTest : BaseChromeTest | |
{ | |
[TestMethod] | |
public void InterceptNetworkForAuthentication() | |
{ | |
var handler = new NetworkAuthenticationHandler() | |
{ | |
UriMatcher = _ => true, | |
Credentials = new PasswordCredentials("admin", "admin") | |
}; | |
INetwork networkInterceptor = driver.Manage().Network; | |
networkInterceptor.AddAuthenticationHandler(handler); | |
networkInterceptor.StartMonitoring().Wait(); | |
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/basic_auth"); | |
networkInterceptor.StopMonitoring().Wait(); | |
Assert.AreEqual("Congratulations! You must have the proper credentials.", driver.FindElement(By.TagName("p")).Text); | |
} | |
[TestMethod] | |
public void InterceptNetworkResponse() | |
{ | |
var handler = new NetworkResponseHandler(); | |
handler.ResponseMatcher = httpresponse => true; | |
handler.ResponseTransformer = http => | |
{ | |
var response = new HttpResponseData(); | |
response.StatusCode = 200; | |
response.Body = "Creamy, delicious cheese!"; | |
return response; | |
}; | |
INetwork networkInterceptor = driver.Manage().Network; | |
networkInterceptor.AddResponseHandler(handler); | |
networkInterceptor.StartMonitoring().Wait(); | |
driver.Navigate().GoToUrl("http://google.com"); | |
networkInterceptor.StopMonitoring().Wait(); | |
Assert.IsTrue(driver.PageSource.Contains("delicious cheese")); | |
} | |
[TestMethod] | |
public void InterceptNetworkRequest() | |
{ | |
var handler = new NetworkRequestHandler(); | |
handler.RequestMatcher = httprequest => true; | |
handler.ResponseSupplier = http => | |
{ | |
var response = new HttpResponseData(); | |
response.StatusCode = 200; | |
response.Body = "Creamy, delicious cheese!"; | |
return response; | |
}; | |
INetwork networkInterceptor = driver.Manage().Network; | |
networkInterceptor.AddRequestHandler(handler); | |
networkInterceptor.StartMonitoring().Wait(); | |
driver.Navigate().GoToUrl("https://google.com"); | |
networkInterceptor.StopMonitoring().Wait(); | |
Assert.IsTrue(driver.PageSource.Contains("delicious cheese")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment