"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/dotnet/src/webdriver/Chromium/ChromiumDriver.cs" (17 Feb 2023, 19528 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "ChromiumDriver.cs" see the
Fossies "Dox" file reference documentation.
1 // <copyright file="ChromiumDriver.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.Collections.ObjectModel;
22 using OpenQA.Selenium.DevTools;
23 using OpenQA.Selenium.Remote;
24
25 namespace OpenQA.Selenium.Chromium
26 {
27 /// <summary>
28 /// Provides an abstract way to access Chromium-based browsers to run tests.
29 /// </summary>
30 public class ChromiumDriver : WebDriver, ISupportsLogs, IDevTools
31 {
32 /// <summary>
33 /// Accept untrusted SSL Certificates
34 /// </summary>
35 public static readonly bool AcceptUntrustedCertificates = true;
36
37 /// <summary>
38 /// Command for executing a Chrome DevTools Protocol command in a driver for a Chromium-based browser.
39 /// </summary>
40 public static readonly string ExecuteCdp = "executeCdpCommand";
41
42 /// <summary>
43 /// Command for getting cast sinks in a driver for a Chromium-based browser.
44 /// </summary>
45 public static readonly string GetCastSinksCommand = "getCastSinks";
46
47 /// <summary>
48 /// Command for selecting a cast sink in a driver for a Chromium-based browser.
49 /// </summary>
50 public static readonly string SelectCastSinkCommand = "selectCastSink";
51
52 /// <summary>
53 /// Command for starting cast tab mirroring in a driver for a Chromium-based browser.
54 /// </summary>
55 public static readonly string StartCastTabMirroringCommand = "startCastTabMirroring";
56
57 /// <summary>
58 /// Command for starting cast desktop mirroring in a driver for a Chromium-based browser.
59 /// </summary>
60 public static readonly string StartCastDesktopMirroringCommand = "startCastDesktopMirroring";
61
62 /// <summary>
63 /// Command for getting a cast issued message in a driver for a Chromium-based browser.
64 /// </summary>
65 public static readonly string GetCastIssueMessageCommand = "getCastIssueMessage";
66
67 /// <summary>
68 /// Command for stopping casting in a driver for a Chromium-based browser.
69 /// </summary>
70 public static readonly string StopCastingCommand = "stopCasting";
71
72 /// <summary>
73 /// Command for getting the simulated network conditions in a driver for a Chromium-based browser.
74 /// </summary>
75 public static readonly string GetNetworkConditionsCommand = "getNetworkConditions";
76
77 /// <summary>
78 /// Command for setting the simulated network conditions in a driver for a Chromium-based browser.
79 /// </summary>
80 public static readonly string SetNetworkConditionsCommand = "setNetworkConditions";
81
82 /// <summary>
83 /// Command for deleting the simulated network conditions in a driver for a Chromium-based browser.
84 /// </summary>
85 public static readonly string DeleteNetworkConditionsCommand = "deleteNetworkConditions";
86
87 /// <summary>
88 /// Command for executing a Chrome DevTools Protocol command in a driver for a Chromium-based browser.
89 /// </summary>
90 public static readonly string SendChromeCommand = "sendChromeCommand";
91
92 /// <summary>
93 /// Command for executing a Chrome DevTools Protocol command that returns a result in a driver for a Chromium-based browser.
94 /// </summary>
95 public static readonly string SendChromeCommandWithResult = "sendChromeCommandWithResult";
96
97 /// <summary>
98 /// Command for launching an app in a driver for a Chromium-based browser.
99 /// </summary>
100 public static readonly string LaunchAppCommand = "launchAppCommand";
101
102 /// <summary>
103 /// Command for setting permissions in a driver for a Chromium-based browser.
104 /// </summary>
105 public static readonly string SetPermissionCommand = "setPermission";
106
107 private readonly string optionsCapabilityName;
108 private DevToolsSession devToolsSession;
109
110 private static Dictionary<string, CommandInfo> chromiumCustomCommands = new Dictionary<string, CommandInfo>()
111 {
112 { GetNetworkConditionsCommand, new HttpCommandInfo(HttpCommandInfo.GetCommand, "/session/{sessionId}/chromium/network_conditions") },
113 { SetNetworkConditionsCommand, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/chromium/network_conditions") },
114 { DeleteNetworkConditionsCommand, new HttpCommandInfo(HttpCommandInfo.DeleteCommand, "/session/{sessionId}/chromium/network_conditions") },
115 { SendChromeCommand, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/chromium/send_command") },
116 { SendChromeCommandWithResult, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/chromium/send_command_and_get_result") },
117 { LaunchAppCommand, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/chromium/launch_app") },
118 { SetPermissionCommand, new HttpCommandInfo(HttpCommandInfo.PostCommand, "/session/{sessionId}/permissions") }
119 };
120
121 /// <summary>
122 /// Initializes a new instance of the <see cref="ChromiumDriver"/> class using the specified <see cref="ChromiumDriverService"/>.
123 /// </summary>
124 /// <param name="service">The <see cref="ChromiumDriverService"/> to use.</param>
125 /// <param name="options">The <see cref="ChromiumOptions"/> to be used with the ChromiumDriver.</param>
126 /// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
127 protected ChromiumDriver(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
128 : base(new DriverServiceCommandExecutor(service, commandTimeout), ConvertOptionsToCapabilities(options))
129 {
130 this.optionsCapabilityName = options.CapabilityName;
131 }
132
133 protected static IReadOnlyDictionary<string, CommandInfo> ChromiumCustomCommands
134 {
135 get { return new ReadOnlyDictionary<string, CommandInfo>(chromiumCustomCommands); }
136 }
137
138 /// <summary>
139 /// Gets or sets the <see cref="IFileDetector"/> responsible for detecting
140 /// sequences of keystrokes representing file paths and names.
141 /// </summary>
142 /// <remarks>The Chromium driver does not allow a file detector to be set,
143 /// as the server component of the Chromium driver only
144 /// allows uploads from the local computer environment. Attempting to set
145 /// this property has no effect, but does not throw an exception. If you
146 /// are attempting to run the Chromium driver remotely, use <see cref="RemoteWebDriver"/>
147 /// in conjunction with a standalone WebDriver server.</remarks>
148 public override IFileDetector FileDetector
149 {
150 get { return base.FileDetector; }
151 set { }
152 }
153
154 /// <summary>
155 /// Gets a value indicating whether a DevTools session is active.
156 /// </summary>
157 public bool HasActiveDevToolsSession
158 {
159 get { return this.devToolsSession != null; }
160 }
161
162 /// <summary>
163 /// Gets or sets the network condition emulation for Chromium.
164 /// </summary>
165 public ChromiumNetworkConditions NetworkConditions
166 {
167 get
168 {
169 Response response = this.Execute(GetNetworkConditionsCommand, null);
170 return ChromiumNetworkConditions.FromDictionary(response.Value as Dictionary<string, object>);
171 }
172
173 set
174 {
175 if (value == null)
176 {
177 throw new ArgumentNullException(nameof(value), "value must not be null");
178 }
179
180 Dictionary<string, object> parameters = new Dictionary<string, object>();
181 parameters["network_conditions"] = value;
182 this.Execute(SetNetworkConditionsCommand, parameters);
183 }
184 }
185
186 /// <summary>
187 /// Launches a Chromium based application.
188 /// </summary>
189 /// <param name="id">ID of the chromium app to launch.</param>
190 public void LaunchApp(string id)
191 {
192 if (id == null)
193 {
194 throw new ArgumentNullException(nameof(id), "id must not be null");
195 }
196
197 Dictionary<string, object> parameters = new Dictionary<string, object>();
198 parameters["id"] = id;
199 this.Execute(LaunchAppCommand, parameters);
200 }
201
202 /// <summary>
203 /// Set supported permission on browser.
204 /// </summary>
205 /// <param name="permissionName">Name of item to set the permission on.</param>
206 /// <param name="permissionValue">Value to set the permission to.</param>
207 public void SetPermission(string permissionName, string permissionValue)
208 {
209 if (permissionName == null)
210 {
211 throw new ArgumentNullException(nameof(permissionName), "name must not be null");
212 }
213
214 if (permissionValue == null)
215 {
216 throw new ArgumentNullException(nameof(permissionValue), "value must not be null");
217 }
218
219 Dictionary<string, object> nameParameter = new Dictionary<string, object>();
220 nameParameter["name"] = permissionName;
221 Dictionary<string, object> parameters = new Dictionary<string, object>();
222 parameters["descriptor"] = nameParameter;
223 parameters["state"] = permissionValue;
224 this.Execute(SetPermissionCommand, parameters);
225 }
226
227 /// <summary>
228 /// Executes a custom Chrome Dev Tools Protocol Command.
229 /// </summary>
230 /// <param name="commandName">Name of the command to execute.</param>
231 /// <param name="commandParameters">Parameters of the command to execute.</param>
232 /// <returns>An object representing the result of the command, if applicable.</returns>
233 public object ExecuteCdpCommand(string commandName, Dictionary<string, object> commandParameters)
234 {
235 if (commandName == null)
236 {
237 throw new ArgumentNullException(nameof(commandName), "commandName must not be null");
238 }
239
240 Dictionary<string, object> parameters = new Dictionary<string, object>();
241 parameters["cmd"] = commandName;
242 parameters["params"] = commandParameters;
243 Response response = this.Execute(ExecuteCdp, parameters);
244 return response.Value;
245 }
246
247 /// <summary>
248 /// Creates a session to communicate with a browser using the Chromium Developer Tools debugging protocol.
249 /// </summary>
250 /// <param name="devToolsProtocolVersion">The version of the Chromium Developer Tools protocol to use. Defaults to autodetect the protocol version.</param>
251 /// <returns>The active session to use to communicate with the Chromium Developer Tools debugging protocol.</returns>
252 public DevToolsSession GetDevToolsSession()
253 {
254 return GetDevToolsSession(DevToolsSession.AutoDetectDevToolsProtocolVersion);
255 }
256
257 /// <summary>
258 /// Creates a session to communicate with a browser using the Chromium Developer Tools debugging protocol.
259 /// </summary>
260 /// <param name="devToolsProtocolVersion">The version of the Chromium Developer Tools protocol to use. Defaults to autodetect the protocol version.</param>
261 /// <returns>The active session to use to communicate with the Chromium Developer Tools debugging protocol.</returns>
262 public DevToolsSession GetDevToolsSession(int devToolsProtocolVersion)
263 {
264 if (this.devToolsSession == null)
265 {
266 if (!this.Capabilities.HasCapability(this.optionsCapabilityName))
267 {
268 throw new WebDriverException("Cannot find " + this.optionsCapabilityName + " capability for driver");
269 }
270
271 Dictionary<string, object> options = this.Capabilities.GetCapability(this.optionsCapabilityName) as Dictionary<string, object>;
272 if (options == null)
273 {
274 throw new WebDriverException("Found " + this.optionsCapabilityName + " capability, but is not an object");
275 }
276
277 if (!options.ContainsKey("debuggerAddress"))
278 {
279 throw new WebDriverException("Did not find debuggerAddress capability in " + this.optionsCapabilityName);
280 }
281
282 string debuggerAddress = options["debuggerAddress"].ToString();
283 try
284 {
285 DevToolsSession session = new DevToolsSession(debuggerAddress);
286 session.StartSession(devToolsProtocolVersion).ConfigureAwait(false).GetAwaiter().GetResult();
287 this.devToolsSession = session;
288 }
289 catch (Exception e)
290 {
291 throw new WebDriverException("Unexpected error creating WebSocket DevTools session.", e);
292 }
293 }
294
295 return this.devToolsSession;
296 }
297
298 /// <summary>
299 /// Closes a DevTools session.
300 /// </summary>
301 public void CloseDevToolsSession()
302 {
303 if (this.devToolsSession != null)
304 {
305 this.devToolsSession.StopSession(true).ConfigureAwait(false).GetAwaiter().GetResult();
306 }
307 }
308
309 /// <summary>
310 /// Clears simulated network conditions.
311 /// </summary>
312 public void ClearNetworkConditions()
313 {
314 this.Execute(DeleteNetworkConditionsCommand, null);
315 }
316
317 /// <summary>
318 /// Returns the list of cast sinks (Cast devices) available to the Chrome media router.
319 /// </summary>
320 /// <returns>The list of available sinks.</returns>
321 public List<Dictionary<string, string>> GetCastSinks()
322 {
323 List<Dictionary<string, string>> returnValue = new List<Dictionary<string, string>>();
324 Response response = this.Execute(GetCastSinksCommand, null);
325 object[] responseValue = response.Value as object[];
326 if (responseValue != null)
327 {
328 foreach (object entry in responseValue)
329 {
330 Dictionary<string, object> entryValue = entry as Dictionary<string, object>;
331 if (entryValue != null)
332 {
333 Dictionary<string, string> sink = new Dictionary<string, string>();
334 foreach (KeyValuePair<string, object> pair in entryValue)
335 {
336 sink[pair.Key] = pair.Value.ToString();
337 }
338
339 returnValue.Add(sink);
340 }
341 }
342 }
343 return returnValue;
344 }
345
346 /// <summary>
347 /// Selects a cast sink (Cast device) as the recipient of media router intents (connect or play).
348 /// </summary>
349 /// <param name="deviceName">Name of the target sink (device).</param>
350 public void SelectCastSink(string deviceName)
351 {
352 if (deviceName == null)
353 {
354 throw new ArgumentNullException(nameof(deviceName), "deviceName must not be null");
355 }
356
357 Dictionary<string, object> parameters = new Dictionary<string, object>();
358 parameters["sinkName"] = deviceName;
359 this.Execute(SelectCastSinkCommand, parameters);
360 }
361
362 /// <summary>
363 /// Initiates tab mirroring for the current browser tab on the specified device.
364 /// </summary>
365 /// <param name="deviceName">Name of the target sink (device).</param>
366 public void StartTabMirroring(string deviceName)
367 {
368 if (deviceName == null)
369 {
370 throw new ArgumentNullException(nameof(deviceName), "deviceName must not be null");
371 }
372
373 Dictionary<string, object> parameters = new Dictionary<string, object>();
374 parameters["sinkName"] = deviceName;
375 this.Execute(StartCastTabMirroringCommand, parameters);
376 }
377
378 /// <summary>
379 /// Initiates mirroring of the desktop on the specified device.
380 /// </summary>
381 /// <param name="deviceName">Name of the target sink (device).</param>
382 public void StartDesktopMirroring(string deviceName)
383 {
384 if (deviceName == null)
385 {
386 throw new ArgumentNullException(nameof(deviceName), "deviceName must not be null");
387 }
388
389 Dictionary<string, object> parameters = new Dictionary<string, object>();
390 parameters["sinkName"] = deviceName;
391 this.Execute(StartCastDesktopMirroringCommand, parameters);
392 }
393
394 /// <summary>
395 /// Returns the error message if there is any issue in a Cast session.
396 /// </summary>
397 /// <returns>An error message.</returns>
398 public String GetCastIssueMessage()
399 {
400 Response response = this.Execute(GetCastIssueMessageCommand, null);
401 return (string)response.Value;
402 }
403
404 /// <summary>
405 /// Stops casting from media router to the specified device, if connected.
406 /// </summary>
407 /// <param name="deviceName">Name of the target sink (device).</param>
408 public void StopCasting(string deviceName)
409 {
410 if (deviceName == null)
411 {
412 throw new ArgumentNullException(nameof(deviceName), "deviceName must not be null");
413 }
414
415 Dictionary<string, object> parameters = new Dictionary<string, object>();
416 parameters["sinkName"] = deviceName;
417 this.Execute(StopCastingCommand, parameters);
418 }
419
420 protected override void Dispose(bool disposing)
421 {
422 if (disposing)
423 {
424 if (this.devToolsSession != null)
425 {
426 this.devToolsSession.Dispose();
427 this.devToolsSession = null;
428 }
429 }
430
431 base.Dispose(disposing);
432 }
433
434 private static ICapabilities ConvertOptionsToCapabilities(ChromiumOptions options)
435 {
436 if (options == null)
437 {
438 throw new ArgumentNullException(nameof(options), "options must not be null");
439 }
440
441 return options.ToCapabilities();
442 }
443 }
444 }