18
18
19
19
using System ;
20
20
using OpenQA . Selenium . Remote ;
21
+ using System . Collections . Generic ;
21
22
22
23
namespace OpenQA . Selenium . Chrome
23
24
{
@@ -61,10 +62,15 @@ public class ChromeDriver : RemoteWebDriver
61
62
/// </summary>
62
63
public static readonly bool AcceptUntrustedCertificates = true ;
63
64
64
- /// <summary>
65
- /// Initializes a new instance of the <see cref="ChromeDriver"/> class.
66
- /// </summary>
67
- public ChromeDriver ( )
65
+ private const string GetNetworkConditionsCommand = "getNetworkConditions" ;
66
+ private const string SetNetworkConditionsCommand = "setNetworkConditions" ;
67
+ private const string DeleteNetworkConditionsCommand = "deleteNetworkConditions" ;
68
+ private const string SendChromeCommand = "sendChromeCommand" ;
69
+
70
+ /// <summary>
71
+ /// Initializes a new instance of the <see cref="ChromeDriver"/> class.
72
+ /// </summary>
73
+ public ChromeDriver ( )
68
74
: this ( new ChromeOptions ( ) )
69
75
{
70
76
}
@@ -140,6 +146,11 @@ public ChromeDriver(ChromeDriverService service, ChromeOptions options)
140
146
public ChromeDriver ( ChromeDriverService service , ChromeOptions options , TimeSpan commandTimeout )
141
147
: base ( new DriverServiceCommandExecutor ( service , commandTimeout ) , ConvertOptionsToCapabilities ( options ) )
142
148
{
149
+ // Add the custom commands unique to Chrome
150
+ AddCustomChromeCommand ( GetNetworkConditionsCommand , CommandInfo . GetCommand , "/session/{sessionId}/chromium/network_connection" ) ;
151
+ AddCustomChromeCommand ( SetNetworkConditionsCommand , CommandInfo . PostCommand , "/session/{sessionId}/chromium/network_connection" ) ;
152
+ AddCustomChromeCommand ( DeleteNetworkConditionsCommand , CommandInfo . DeleteCommand , "/session/{sessionId}/chromium/network_connection" ) ;
153
+ AddCustomChromeCommand ( SendChromeCommand , CommandInfo . PostCommand , "/session/{sessionId}/chromium/send_command" ) ;
143
154
}
144
155
145
156
/// <summary>
@@ -158,6 +169,46 @@ public override IFileDetector FileDetector
158
169
set { }
159
170
}
160
171
172
+ /// <summary>
173
+ /// Gets or sets the network condition emulation for Chrome.
174
+ /// </summary>
175
+ public ChromeNetworkConditions NetworkConditions
176
+ {
177
+ get
178
+ {
179
+ Response response = this . Execute ( GetNetworkConditionsCommand , null ) ;
180
+ return ChromeNetworkConditions . FromDictionary ( response . Value as Dictionary < string , object > ) ;
181
+ }
182
+
183
+ set
184
+ {
185
+ if ( value == null )
186
+ {
187
+ throw new ArgumentNullException ( "value" , "value must not be null" ) ;
188
+ }
189
+
190
+ this . Execute ( SetNetworkConditionsCommand , value . ToDictionary ( ) ) ;
191
+ }
192
+ }
193
+
194
+ /// <summary>
195
+ /// Executes a custom Chrome command.
196
+ /// </summary>
197
+ /// <param name="commandName">Name of the command to execute.</param>
198
+ /// <param name="commandParameters">Parameters of the command to execute.</param>
199
+ public void ExecuteChromeCommand ( string commandName , Dictionary < string , object > commandParameters )
200
+ {
201
+ if ( commandName == null )
202
+ {
203
+ throw new ArgumentNullException ( "commandName" , "commandName must not be null" ) ;
204
+ }
205
+
206
+ Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
207
+ parameters [ "cmd" ] = commandName ;
208
+ parameters [ "params" ] = commandParameters ;
209
+ this . Execute ( SendChromeCommand , parameters ) ;
210
+ }
211
+
161
212
private static ICapabilities ConvertOptionsToCapabilities ( ChromeOptions options )
162
213
{
163
214
if ( options == null )
@@ -167,5 +218,11 @@ private static ICapabilities ConvertOptionsToCapabilities(ChromeOptions options)
167
218
168
219
return options . ToCapabilities ( ) ;
169
220
}
221
+
222
+ private void AddCustomChromeCommand ( string commandName , string method , string resourcePath )
223
+ {
224
+ CommandInfo commandInfoToAdd = new CommandInfo ( method , resourcePath ) ;
225
+ this . CommandExecutor . CommandInfoRepository . TryAddCommand ( commandName , commandInfoToAdd ) ;
226
+ }
170
227
}
171
228
}
0 commit comments