Skip to content

Commit 7c9d268

Browse files
committed
Add network interception to .NET
This commit adds the ability of a user to intercept and manipulate network traffic between the browser and the web site being automated. To use this feature at present, the browser being automated must support the Chromium DevTools Protocol, and the driver instance must implement the IDevTools interface. Attempting to use these features will cause an exception to be thrown at runtime if the driver is not capable of this communication. To access this feature, use the following code: // Assume "driver" is a valid IWebDriver instance INetwork network = driver.Manage().Network; // Set up monitoring information by adding handlers // and subscribing to events. // Begin monitoring by starting the monitor. await network.StartMonitoring(); With this new functionality, users can do the following: * Monitor HTTP requests and responses between browser and web server * Add handlers to modify requests that meet user-defined criteria * Add handlers to supply responses for requests that meet user-defined criteria * Add handlers to supply authentication credentials for sites using basic and digest authentication
1 parent a420091 commit 7c9d268

14 files changed

+610
-4
lines changed

dotnet/src/support/Events/EventFiringWebDriver.cs

+5
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,11 @@ public ILogs Logs
884884
get { return this.wrappedOptions.Logs; }
885885
}
886886

887+
public INetwork Network
888+
{
889+
get { return this.wrappedOptions.Network; }
890+
}
891+
887892
/// <summary>
888893
/// Provides access to the timeouts defined for this driver.
889894
/// </summary>

dotnet/src/webdriver/DevTools/HttpRequestData.cs renamed to dotnet/src/webdriver/HttpRequestData.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
using System.Collections.Generic;
2121
using System.Text;
2222

23-
namespace OpenQA.Selenium.DevTools
23+
namespace OpenQA.Selenium
2424
{
2525
/// <summary>
2626
/// Represents the response data for an intercepted HTTP call.

dotnet/src/webdriver/DevTools/HttpResponseData.cs renamed to dotnet/src/webdriver/HttpResponseData.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
using System.Data.Common;
2222
using System.Text;
2323

24-
namespace OpenQA.Selenium.DevTools
24+
namespace OpenQA.Selenium
2525
{
2626
/// <summary>
2727
/// Represents the response data for an intercepted HTTP call.

dotnet/src/webdriver/ICredentials.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// <copyright file="ICredentials.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
namespace OpenQA.Selenium
20+
{
21+
/// <summary>
22+
/// Marker interface describing a set of credentials to be used with network requests.
23+
/// This allows the ability to expand beyond simple user name/password security mechanisms.
24+
/// </summary>
25+
public interface ICredentials
26+
{
27+
}
28+
}

dotnet/src/webdriver/INetwork.cs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// <copyright file="INetwork.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq;
22+
using System.Text;
23+
using System.Threading.Tasks;
24+
25+
namespace OpenQA.Selenium
26+
{
27+
/// <summary>
28+
/// Defines an interface allowing the user to manage network communication by the browser.
29+
/// </summary>
30+
public interface INetwork
31+
{
32+
/// <summary>
33+
/// Occurs when a browser sends a network request.
34+
/// </summary>
35+
event EventHandler<NetworkRequestSentEventArgs> NetworkRequestSent;
36+
37+
/// <summary>
38+
/// Occurs when a browser receives a network response.
39+
/// </summary>
40+
event EventHandler<NetworkResponseRecievedEventArgs> NetworkResponseReceived;
41+
42+
/// <summary>
43+
/// Adds a <see cref="NetworkRequestHandler"/> to examine incoming network requests,
44+
/// and optionally modify the request or provide a response.
45+
/// </summary>
46+
/// <param name="handler">The <see cref="NetworkRequestHandler"/> to add.</param>
47+
void AddRequestHandler(NetworkRequestHandler handler);
48+
49+
/// <summary>
50+
/// Clears all added <see cref="NetworkRequestHandler"/> instances.
51+
/// </summary>
52+
void ClearRequestHandlers();
53+
54+
/// <summary>
55+
/// Adds a <see cref="NetworkAuthenticationHandler"/> to supply authentication
56+
/// credentials for network requests.
57+
/// </summary>
58+
/// <param name="handler">The <see cref="NetworkAuthenticationHandler"/> to add.</param>
59+
void AddAuthenticationHandler(NetworkAuthenticationHandler handler);
60+
61+
/// <summary>
62+
/// Clears all added <see cref="NetworkAuthenticationHandler"/> instances.
63+
/// </summary>
64+
void ClearAuthenticationHandlers();
65+
66+
/// <summary>
67+
/// Asynchronously starts monitoring for network traffic.
68+
/// </summary>
69+
/// <returns>A task that represents the asynchronous operation.</returns>
70+
Task StartMonitoring();
71+
72+
/// <summary>
73+
/// Asynchronously stops monitoring for network traffic.
74+
/// </summary>
75+
/// <returns>A task that represents the asynchronous operation.</returns>
76+
Task StopMonitoring();
77+
}
78+
}

dotnet/src/webdriver/IOptions.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ public interface IOptions
3636
IWindow Window { get; }
3737

3838
/// <summary>
39-
/// Gets an object allowing the user to examing the logs for this driver instance.
39+
/// Gets an object allowing the user to examine the logs for this driver instance.
4040
/// </summary>
4141
ILogs Logs { get; }
4242

43+
/// <summary>
44+
/// Gets an object allowing the user to manage network communication by the browser.
45+
/// </summary>
46+
INetwork Network { get; }
47+
4348
/// <summary>
4449
/// Provides access to the timeouts defined for this driver.
4550
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <copyright file="NetworkAuthenticationHandler.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Allows a user to supply authentication information for network requests.
25+
/// </summary>
26+
public class NetworkAuthenticationHandler
27+
{
28+
/// <summary>
29+
/// Gets or sets a function that takes a <see cref="Uri"/> object, and returns a
30+
/// value indicating whether the supplied URI matches the specified criteria.
31+
/// </summary>
32+
public Func<Uri, bool> UriMatcher { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the credentials to use when responding to an authentication request
36+
/// that matches the specified criteria.
37+
/// </summary>
38+
public ICredentials Credentials { get; set; }
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// <copyright file="NetworkRequestHandler.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Allows a user to handle a network request, potentially modifying the request or providing a known response.
25+
/// </summary>
26+
public class NetworkRequestHandler
27+
{
28+
/// <summary>
29+
/// Gets or sets a function that evaluates request data in an <see cref="HttpRequestData"/> object,
30+
/// and returns a value indicating whether the data matches the specified criteria.
31+
/// </summary>
32+
public Func<HttpRequestData, bool> RequestMatcher { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets a function that accepts an <see cref="HttpRequestData"/> object describing a network
36+
/// request to be sent, and returns a modified <see cref="HttpRequestData"/> object to use in the actual
37+
/// network request.
38+
/// </summary>
39+
public Func<HttpRequestData, HttpRequestData> RequestTransformer { get; set; }
40+
41+
/// <summary>
42+
/// Gets or sets a function that accepts an <see cref="HttpRequestData"/> object describing a network
43+
/// request to be sent, and returns an <see cref="HttpResponseData"/> object as the response for the
44+
/// request, bypassing the actual network request.
45+
/// </summary>
46+
public Func<HttpRequestData, HttpResponseData> ResponseSupplier { get; set; }
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// <copyright file="NetworkRequestSentEventArgs.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
using System.Collections.Generic;
21+
22+
namespace OpenQA.Selenium
23+
{
24+
/// <summary>
25+
/// Provides data for the NetworkRequestSent event of an object implementing the <see cref="INetwork"/> interface.
26+
/// </summary>
27+
public class NetworkRequestSentEventArgs : EventArgs
28+
{
29+
private readonly string requestId;
30+
private readonly string requestUrl;
31+
private readonly string requestMethod;
32+
private readonly string requestPostData;
33+
private readonly Dictionary<string, string> requestHeaders = new Dictionary<string, string>();
34+
35+
/// <summary>
36+
/// Initializes a new instance of the <see cref="NetworkRequestSentEventArgs"/> class.
37+
/// </summary>
38+
/// <param name="requestData">The <see cref="HttpRequestData"/> that describes the network request.</param>
39+
public NetworkRequestSentEventArgs(HttpRequestData requestData)
40+
{
41+
this.requestId = requestData.RequestId;
42+
this.requestUrl = requestData.Url;
43+
this.requestMethod = requestData.Method;
44+
this.requestPostData = requestData.PostData;
45+
foreach (KeyValuePair<string, string> header in requestData.Headers)
46+
{
47+
this.requestHeaders[header.Key] = header.Value;
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Gets the internal request ID of the network request.
53+
/// </summary>
54+
public string RequestId => requestId;
55+
56+
/// <summary>
57+
/// Gets the URL of the network request.
58+
/// </summary>
59+
public string RequestUrl => requestUrl;
60+
61+
/// <summary>
62+
/// Gets the HTTP method of the network request.
63+
/// </summary>
64+
public string RequestMethod => requestMethod;
65+
66+
/// <summary>
67+
/// Gets the post data of the network request, if any.
68+
/// </summary>
69+
public string RequestPostData => requestPostData;
70+
71+
/// <summary>
72+
/// Gets the collection of headers associated with the network request.
73+
/// </summary>
74+
public IReadOnlyDictionary<string, string> RequestHeaders => requestHeaders;
75+
}
76+
}

0 commit comments

Comments
 (0)