forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromiumOptions.cs
670 lines (583 loc) · 31.2 KB
/
ChromiumOptions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// <copyright file="ChromiumOptions.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
#nullable enable
namespace OpenQA.Selenium.Chromium
{
/// <summary>
/// Abstract class to manage options specific to Chromium-based browsers.
/// </summary>
public abstract class ChromiumOptions : DriverOptions
{
private const string ArgumentsChromeOption = "args";
private const string BinaryChromeOption = "binary";
private const string ExtensionsChromeOption = "extensions";
private const string LocalStateChromeOption = "localState";
private const string PreferencesChromeOption = "prefs";
private const string DetachChromeOption = "detach";
private const string DebuggerAddressChromeOption = "debuggerAddress";
private const string ExcludeSwitchesChromeOption = "excludeSwitches";
private const string MinidumpPathChromeOption = "minidumpPath";
private const string MobileEmulationChromeOption = "mobileEmulation";
private const string PerformanceLoggingPreferencesChromeOption = "perfLoggingPrefs";
private const string WindowTypesChromeOption = "windowTypes";
private const string UseSpecCompliantProtocolOption = "w3c";
private bool useSpecCompliantProtocol = true;
private readonly List<string> arguments = new List<string>();
private readonly List<string> extensionFiles = new List<string>();
private readonly List<string> encodedExtensions = new List<string>();
private readonly List<string> excludedSwitches = new List<string>();
private readonly List<string> windowTypes = new List<string>();
private readonly Dictionary<string, object> additionalChromeOptions = new Dictionary<string, object>();
private Dictionary<string, object>? userProfilePreferences;
private Dictionary<string, object>? localStatePreferences;
private string? mobileEmulationDeviceName;
private ChromiumMobileEmulationDeviceSettings? mobileEmulationDeviceSettings;
/// <summary>
/// Initializes a new instance of the <see cref="ChromiumOptions"/> class.
/// </summary>
public ChromiumOptions() : base()
{
this.AddKnownCapabilityName(this.CapabilityName, "current ChromiumOptions class instance");
this.AddKnownCapabilityName(CapabilityType.LoggingPreferences, "SetLoggingPreference method");
this.AddKnownCapabilityName(this.LoggingPreferencesChromeOption, "SetLoggingPreference method");
this.AddKnownCapabilityName(ChromiumOptions.ArgumentsChromeOption, "AddArguments method");
this.AddKnownCapabilityName(ChromiumOptions.BinaryChromeOption, "BinaryLocation property");
this.AddKnownCapabilityName(ChromiumOptions.ExtensionsChromeOption, "AddExtensions method");
this.AddKnownCapabilityName(ChromiumOptions.LocalStateChromeOption, "AddLocalStatePreference method");
this.AddKnownCapabilityName(ChromiumOptions.PreferencesChromeOption, "AddUserProfilePreference method");
this.AddKnownCapabilityName(ChromiumOptions.DetachChromeOption, "LeaveBrowserRunning property");
this.AddKnownCapabilityName(ChromiumOptions.DebuggerAddressChromeOption, "DebuggerAddress property");
this.AddKnownCapabilityName(ChromiumOptions.ExcludeSwitchesChromeOption, "AddExcludedArgument property");
this.AddKnownCapabilityName(ChromiumOptions.MinidumpPathChromeOption, "MinidumpPath property");
this.AddKnownCapabilityName(ChromiumOptions.MobileEmulationChromeOption, "EnableMobileEmulation method");
this.AddKnownCapabilityName(ChromiumOptions.PerformanceLoggingPreferencesChromeOption, "PerformanceLoggingPreferences property");
this.AddKnownCapabilityName(ChromiumOptions.WindowTypesChromeOption, "AddWindowTypes method");
this.AddKnownCapabilityName(ChromiumOptions.UseSpecCompliantProtocolOption, "UseSpecCompliantProtocol property");
}
/// <summary>
/// Gets the vendor prefix to apply to Chromium-specific capability names.
/// </summary>
protected abstract string VendorPrefix { get; }
private string LoggingPreferencesChromeOption => this.VendorPrefix + ":loggingPrefs";
/// <summary>
/// Gets the name of the capability used to store Chromium options in
/// an <see cref="ICapabilities"/> object.
/// </summary>
public abstract string CapabilityName { get; }
/// <summary>
/// Gets or sets the location of the Chromium browser's binary executable file.
/// </summary>
public override string? BinaryLocation { get; set; }
/// <summary>
/// Gets or sets a value indicating whether Chromium should be left running after the
/// ChromeDriver instance is exited. Defaults to <see langword="false"/>.
/// </summary>
public bool LeaveBrowserRunning { get; set; }
/// <summary>
/// Gets the list of arguments appended to the Chromium command line as a string array.
/// </summary>
public ReadOnlyCollection<string> Arguments => this.arguments.AsReadOnly();
/// <summary>
/// Gets the list of extensions to be installed as an array of base64-encoded strings.
/// </summary>
public ReadOnlyCollection<string> Extensions
{
get
{
List<string> allExtensions = new List<string>(this.encodedExtensions);
foreach (string extensionFile in this.extensionFiles)
{
byte[] extensionByteArray = File.ReadAllBytes(extensionFile);
string encodedExtension = Convert.ToBase64String(extensionByteArray);
allExtensions.Add(encodedExtension);
}
return allExtensions.AsReadOnly();
}
}
/// <summary>
/// Gets or sets the address of a Chromium debugger server to connect to.
/// Should be of the form "{hostname|IP address}:port".
/// </summary>
public string? DebuggerAddress { get; set; }
/// <summary>
/// Gets or sets the directory in which to store minidump files.
/// </summary>
public string? MinidumpPath { get; set; }
/// <summary>
/// Gets or sets the performance logging preferences for the driver.
/// </summary>
public ChromiumPerformanceLoggingPreferences? PerformanceLoggingPreferences { get; set; }
/// <summary>
/// Gets or sets the options for automating Chromium applications on Android.
/// </summary>
public ChromiumAndroidOptions? AndroidOptions { get; set; }
/// <summary>
/// Adds a single argument to the list of arguments to be appended to the browser executable command line.
/// </summary>
/// <param name="argument">The argument to add.</param>
/// <exception cref="ArgumentException">If <paramref name="argument"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public void AddArgument(string argument)
{
if (string.IsNullOrEmpty(argument))
{
throw new ArgumentException("argument must not be null or empty", nameof(argument));
}
this.AddArguments(argument);
}
/// <summary>
/// Adds arguments to be appended to the browser executable command line.
/// </summary>
/// <param name="argumentsToAdd">An array of arguments to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="argumentsToAdd"/> is <see langword="null"/>.</exception>
public void AddArguments(params string[] argumentsToAdd)
{
this.AddArguments((IEnumerable<string>)argumentsToAdd);
}
/// <summary>
/// Adds arguments to be appended to the browser executable command line.
/// </summary>
/// <param name="argumentsToAdd">An <see cref="IEnumerable{T}"/> object of arguments to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="argumentsToAdd"/> is <see langword="null"/>.</exception>
public void AddArguments(IEnumerable<string> argumentsToAdd)
{
if (argumentsToAdd == null)
{
throw new ArgumentNullException(nameof(argumentsToAdd), "argumentsToAdd must not be null");
}
this.arguments.AddRange(argumentsToAdd);
}
/// <summary>
/// Adds a single argument to be excluded from the list of arguments passed by default
/// to the browser executable command line by chromedriver.exe.
/// </summary>
/// <param name="argument">The argument to exclude.</param>
/// <exception cref="ArgumentException">If <paramref name="argument"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public void AddExcludedArgument(string argument)
{
if (string.IsNullOrEmpty(argument))
{
throw new ArgumentException("argument must not be null or empty", nameof(argument));
}
this.AddExcludedArguments(argument);
}
/// <summary>
/// Adds arguments to be excluded from the list of arguments passed by default
/// to the browser executable command line by chromedriver.exe.
/// </summary>
/// <param name="argumentsToExclude">An array of arguments to exclude.</param>
/// <exception cref="ArgumentNullException">If <paramref name="argumentsToExclude"/> is <see langword="null"/>.</exception>
public void AddExcludedArguments(params string[] argumentsToExclude)
{
this.AddExcludedArguments((IEnumerable<string>)argumentsToExclude);
}
/// <summary>
/// Adds arguments to be excluded from the list of arguments passed by default
/// to the browser executable command line by chromedriver.exe.
/// </summary>
/// <param name="argumentsToExclude">An <see cref="IEnumerable{T}"/> object of arguments to exclude.</param>
/// <exception cref="ArgumentNullException">If <paramref name="argumentsToExclude"/> is <see langword="null"/>.</exception>
public void AddExcludedArguments(IEnumerable<string> argumentsToExclude)
{
if (argumentsToExclude == null)
{
throw new ArgumentNullException(nameof(argumentsToExclude), "argumentsToExclude must not be null");
}
this.excludedSwitches.AddRange(argumentsToExclude);
}
/// <summary>
/// Adds a path to a packed Chrome extension (.crx file) to the list of extensions
/// to be installed in the instance of Chrome.
/// </summary>
/// <param name="pathToExtension">The full path to the extension to add.</param>
/// <exception cref="ArgumentException">If <paramref name="pathToExtension"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public void AddExtension(string pathToExtension)
{
if (string.IsNullOrEmpty(pathToExtension))
{
throw new ArgumentException("pathToExtension must not be null or empty", nameof(pathToExtension));
}
this.AddExtensions(pathToExtension);
}
/// <summary>
/// Adds a list of paths to packed Chrome extensions (.crx files) to be installed
/// in the instance of Chrome.
/// </summary>
/// <param name="extensions">An array of full paths to the extensions to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensions"/> is <see langword="null"/>.</exception>
/// <exception cref="FileNotFoundException">If any extension file path does not point to a file.</exception>
public void AddExtensions(params string[] extensions)
{
this.AddExtensions((IEnumerable<string>)extensions);
}
/// <summary>
/// Adds a list of paths to packed Chrome extensions (.crx files) to be installed
/// in the instance of Chrome.
/// </summary>
/// <param name="extensions">An <see cref="IEnumerable{T}"/> of full paths to the extensions to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensions"/> is <see langword="null"/>.</exception>
/// <exception cref="FileNotFoundException">If any extension file path does not point to a file.</exception>
public void AddExtensions(IEnumerable<string> extensions)
{
if (extensions == null)
{
throw new ArgumentNullException(nameof(extensions), "extensions must not be null");
}
foreach (string extension in extensions)
{
if (!File.Exists(extension))
{
throw new FileNotFoundException("No extension found at the specified path", extension);
}
this.extensionFiles.Add(extension);
}
}
/// <summary>
/// Adds a base64-encoded string representing a Chrome extension to the list of extensions
/// to be installed in the instance of Chrome.
/// </summary>
/// <param name="extension">A base64-encoded string representing the extension to add.</param>
/// <exception cref="ArgumentException">If <paramref name="extension"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
/// <exception cref="WebDriverException">If the extension string is not valid base-64.</exception>
public void AddEncodedExtension(string extension)
{
if (string.IsNullOrEmpty(extension))
{
throw new ArgumentException("extension must not be null or empty", nameof(extension));
}
this.AddEncodedExtensions(extension);
}
/// <summary>
/// Adds a list of base64-encoded strings representing Chrome extensions to the list of extensions
/// to be installed in the instance of Chrome.
/// </summary>
/// <param name="extensions">An array of base64-encoded strings representing the extensions to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensions"/> is <see langword="null"/>.</exception>
/// <exception cref="WebDriverException">If an extension string is not valid base-64.</exception>
public void AddEncodedExtensions(params string[] extensions)
{
this.AddEncodedExtensions((IEnumerable<string>)extensions);
}
/// <summary>
/// Adds a list of base64-encoded strings representing Chrome extensions to be installed
/// in the instance of Chrome.
/// </summary>
/// <param name="extensions">An <see cref="IEnumerable{T}"/> of base64-encoded strings
/// representing the extensions to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensions"/> is <see langword="null"/>.</exception>
/// <exception cref="WebDriverException">If an extension string is not valid base-64.</exception>
public void AddEncodedExtensions(IEnumerable<string> extensions)
{
if (extensions == null)
{
throw new ArgumentNullException(nameof(extensions), "extensions must not be null");
}
foreach (string extension in extensions)
{
// Run the extension through the base64 converter to test that the
// string is not malformed.
try
{
Convert.FromBase64String(extension);
}
catch (FormatException ex)
{
throw new WebDriverException("Could not properly decode the base64 string", ex);
}
this.encodedExtensions.Add(extension);
}
}
/// <summary>
/// Adds a preference for the user-specific profile or "user data directory."
/// If the specified preference already exists, it will be overwritten.
/// </summary>
/// <param name="preferenceName">The name of the preference to set.</param>
/// <param name="preferenceValue">The value of the preference to set.</param>
/// <exception cref="ArgumentNullException">If <paramref name="preferenceName"/> is <see langword="null"/>.</exception>
public void AddUserProfilePreference(string preferenceName, object preferenceValue)
{
if (this.userProfilePreferences == null)
{
this.userProfilePreferences = new Dictionary<string, object>();
}
this.userProfilePreferences[preferenceName] = preferenceValue;
}
/// <summary>
/// Adds a preference for the local state file in the user's data directory for Chromium.
/// If the specified preference already exists, it will be overwritten.
/// </summary>
/// <param name="preferenceName">The name of the preference to set.</param>
/// <param name="preferenceValue">The value of the preference to set.</param>
/// <exception cref="ArgumentNullException">If <paramref name="preferenceName"/> is <see langword="null"/>.</exception>
public void AddLocalStatePreference(string preferenceName, object preferenceValue)
{
if (this.localStatePreferences == null)
{
this.localStatePreferences = new Dictionary<string, object>();
}
this.localStatePreferences[preferenceName] = preferenceValue;
}
/// <summary>
/// Allows the Chromium browser to emulate a mobile device.
/// </summary>
/// <param name="deviceName">The name of the device to emulate. The device name must be a
/// valid device name from the Chrome DevTools Emulation panel.</param>
/// <remarks>Specifying an invalid device name will not throw an exeption, but
/// will generate an error in Chrome when the driver starts. To unset mobile
/// emulation, call this method with <see langword="null"/> as the argument.</remarks>
public void EnableMobileEmulation(string? deviceName)
{
this.mobileEmulationDeviceSettings = null;
this.mobileEmulationDeviceName = deviceName;
}
/// <summary>
/// Allows the Chromium browser to emulate a mobile device.
/// </summary>
/// <param name="deviceSettings">The <see cref="ChromiumMobileEmulationDeviceSettings"/>
/// object containing the settings of the device to emulate.</param>
/// <exception cref="ArgumentException">Thrown if the device settings option does
/// not have a user agent string set.</exception>
/// <remarks>Specifying an invalid device name will not throw an exeption, but
/// will generate an error in Chrome when the driver starts. To unset mobile
/// emulation, call this method with <see langword="null"/> as the argument.</remarks>
public void EnableMobileEmulation(ChromiumMobileEmulationDeviceSettings? deviceSettings)
{
this.mobileEmulationDeviceName = null;
if (deviceSettings != null && string.IsNullOrEmpty(deviceSettings.UserAgent))
{
throw new ArgumentException("Device settings must include a user agent string.", nameof(deviceSettings));
}
this.mobileEmulationDeviceSettings = deviceSettings;
}
/// <summary>
/// Adds a type of window that will be listed in the list of window handles
/// returned by the Chrome driver.
/// </summary>
/// <param name="windowType">The name of the window type to add.</param>
/// <remarks>This method can be used to allow the driver to access {webview}
/// elements by adding "webview" as a window type.</remarks>
/// <exception cref="ArgumentException">If <paramref name="windowType"/> is <see langword="null"/> or <see cref="string.Empty"/>.</exception>
public void AddWindowType(string windowType)
{
if (string.IsNullOrEmpty(windowType))
{
throw new ArgumentException("windowType must not be null or empty", nameof(windowType));
}
this.AddWindowTypes(windowType);
}
/// <summary>
/// Adds a list of window types that will be listed in the list of window handles
/// returned by the Chromium driver.
/// </summary>
/// <param name="windowTypesToAdd">An array of window types to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="windowTypesToAdd"/> is <see langword="null"/>.</exception>
public void AddWindowTypes(params string[] windowTypesToAdd)
{
this.AddWindowTypes((IEnumerable<string>)windowTypesToAdd);
}
/// <summary>
/// Adds a list of window types that will be listed in the list of window handles
/// returned by the Chromium driver.
/// </summary>
/// <param name="windowTypesToAdd">An <see cref="IEnumerable{T}"/> of window types to add.</param>
/// <exception cref="ArgumentNullException">If <paramref name="windowTypesToAdd"/> is <see langword="null"/>.</exception>
public void AddWindowTypes(IEnumerable<string> windowTypesToAdd)
{
if (windowTypesToAdd == null)
{
throw new ArgumentNullException(nameof(windowTypesToAdd), "windowTypesToAdd must not be null");
}
this.windowTypes.AddRange(windowTypesToAdd);
}
/// <summary>
/// Provides a means to add additional capabilities not yet added as type safe options
/// for the Chromium driver.
/// </summary>
/// <param name="optionName">The name of the capability to add.</param>
/// <param name="optionValue">The value of the capability to add.</param>
/// <exception cref="ArgumentException">
/// thrown when attempting to add a capability for which there is already a type safe option, or
/// when <paramref name="optionName"/> is <see langword="null"/> or the empty string.
/// </exception>
/// <remarks>Calling <see cref="AddAdditionalChromiumOption(string, object)"/>
/// where <paramref name="optionName"/> has already been added will overwrite the
/// existing value with the new value in <paramref name="optionValue"/>.
/// Calling this method adds capabilities to the Chromium-specific options object passed to
/// webdriver executable (e.g. property name 'goog:chromeOptions').</remarks>
/// <exception cref="ArgumentException">
/// thrown when attempting to add a capability for which there is already a type safe option, or
/// when <paramref name="optionName"/> is <see langword="null"/> or the empty string.
/// </exception>
protected void AddAdditionalChromiumOption(string optionName, object optionValue)
{
this.ValidateCapabilityName(optionName);
this.additionalChromeOptions[optionName] = optionValue;
}
/// <summary>
/// Returns DesiredCapabilities for Chromium with these options included as
/// capabilities. This does not copy the options. Further changes will be
/// reflected in the returned capabilities.
/// </summary>
/// <returns>The DesiredCapabilities for Chrome with these options.</returns>
public override ICapabilities ToCapabilities()
{
Dictionary<string, object> chromeOptions = this.BuildChromeOptionsDictionary();
IWritableCapabilities capabilities = this.GenerateDesiredCapabilities(false);
capabilities.SetCapability(this.CapabilityName, chromeOptions);
AddVendorSpecificChromiumCapabilities(capabilities);
Dictionary<string, object>? loggingPreferences = this.GenerateLoggingPreferencesDictionary();
if (loggingPreferences != null)
{
capabilities.SetCapability(LoggingPreferencesChromeOption, loggingPreferences);
}
return capabilities.AsReadOnly();
}
/// <summary>
/// Adds vendor-specific capabilities for Chromium-based browsers.
/// </summary>
/// <param name="capabilities">The capabilities to add.</param>
protected virtual void AddVendorSpecificChromiumCapabilities(IWritableCapabilities capabilities)
{
}
private Dictionary<string, object> BuildChromeOptionsDictionary()
{
Dictionary<string, object> chromeOptions = new Dictionary<string, object>();
if (this.Arguments.Count > 0)
{
chromeOptions[ArgumentsChromeOption] = this.Arguments;
}
if (!string.IsNullOrEmpty(this.BinaryLocation))
{
chromeOptions[BinaryChromeOption] = this.BinaryLocation!;
}
ReadOnlyCollection<string> extensions = this.Extensions;
if (extensions.Count > 0)
{
chromeOptions[ExtensionsChromeOption] = extensions;
}
if (this.localStatePreferences != null && this.localStatePreferences.Count > 0)
{
chromeOptions[LocalStateChromeOption] = this.localStatePreferences;
}
if (this.userProfilePreferences != null && this.userProfilePreferences.Count > 0)
{
chromeOptions[PreferencesChromeOption] = this.userProfilePreferences;
}
if (this.LeaveBrowserRunning)
{
chromeOptions[DetachChromeOption] = this.LeaveBrowserRunning;
}
if (!this.useSpecCompliantProtocol)
{
chromeOptions[UseSpecCompliantProtocolOption] = this.useSpecCompliantProtocol;
}
if (!string.IsNullOrEmpty(this.DebuggerAddress))
{
chromeOptions[DebuggerAddressChromeOption] = this.DebuggerAddress!;
}
if (this.excludedSwitches.Count > 0)
{
chromeOptions[ExcludeSwitchesChromeOption] = this.excludedSwitches;
}
if (!string.IsNullOrEmpty(this.MinidumpPath))
{
chromeOptions[MinidumpPathChromeOption] = this.MinidumpPath!;
}
if (!string.IsNullOrEmpty(this.mobileEmulationDeviceName) || this.mobileEmulationDeviceSettings != null)
{
chromeOptions[MobileEmulationChromeOption] = GenerateMobileEmulationSettingsDictionary(this.mobileEmulationDeviceSettings, this.mobileEmulationDeviceName);
}
if (this.PerformanceLoggingPreferences != null)
{
chromeOptions[PerformanceLoggingPreferencesChromeOption] = GeneratePerformanceLoggingPreferencesDictionary(this.PerformanceLoggingPreferences);
}
if (this.AndroidOptions != null)
{
AddAndroidOptions(chromeOptions, this.AndroidOptions);
}
if (this.windowTypes.Count > 0)
{
chromeOptions[WindowTypesChromeOption] = this.windowTypes;
}
foreach (KeyValuePair<string, object> pair in this.additionalChromeOptions)
{
chromeOptions.Add(pair.Key, pair.Value);
}
return chromeOptions;
}
private static void AddAndroidOptions(Dictionary<string, object> chromeOptions, ChromiumAndroidOptions androidOptions)
{
chromeOptions["androidPackage"] = androidOptions.AndroidPackage;
if (!string.IsNullOrEmpty(androidOptions.AndroidDeviceSerial))
{
chromeOptions["androidDeviceSerial"] = androidOptions.AndroidDeviceSerial!;
}
if (!string.IsNullOrEmpty(androidOptions.AndroidActivity))
{
chromeOptions["androidActivity"] = androidOptions.AndroidActivity!;
}
if (!string.IsNullOrEmpty(androidOptions.AndroidProcess))
{
chromeOptions["androidProcess"] = androidOptions.AndroidProcess;
}
if (androidOptions.UseRunningApp)
{
chromeOptions["androidUseRunningApp"] = androidOptions.UseRunningApp;
}
}
private static Dictionary<string, object> GeneratePerformanceLoggingPreferencesDictionary(ChromiumPerformanceLoggingPreferences prefs)
{
Dictionary<string, object> perfLoggingPrefsDictionary = new Dictionary<string, object>();
perfLoggingPrefsDictionary["enableNetwork"] = prefs.IsCollectingNetworkEvents;
perfLoggingPrefsDictionary["enablePage"] = prefs.IsCollectingPageEvents;
string tracingCategories = prefs.TracingCategories;
if (!string.IsNullOrEmpty(tracingCategories))
{
perfLoggingPrefsDictionary["traceCategories"] = tracingCategories;
}
perfLoggingPrefsDictionary["bufferUsageReportingInterval"] = Convert.ToInt64(prefs.BufferUsageReportingInterval.TotalMilliseconds);
return perfLoggingPrefsDictionary;
}
private static Dictionary<string, object?> GenerateMobileEmulationSettingsDictionary(ChromiumMobileEmulationDeviceSettings? settings, string? deviceName)
{
Dictionary<string, object?> mobileEmulationSettings = new Dictionary<string, object?>();
if (!string.IsNullOrEmpty(deviceName))
{
mobileEmulationSettings["deviceName"] = deviceName!;
}
else if (settings != null)
{
mobileEmulationSettings["userAgent"] = settings.UserAgent;
Dictionary<string, object> deviceMetrics = new Dictionary<string, object>();
deviceMetrics["width"] = settings.Width;
deviceMetrics["height"] = settings.Height;
deviceMetrics["pixelRatio"] = settings.PixelRatio;
if (!settings.EnableTouchEvents)
{
deviceMetrics["touch"] = settings.EnableTouchEvents;
}
mobileEmulationSettings["deviceMetrics"] = deviceMetrics;
}
return mobileEmulationSettings;
}
}
}