diff --git a/dotnet/src/webdriver/Chromium/ChromiumOptions.cs b/dotnet/src/webdriver/Chromium/ChromiumOptions.cs index 63251e12dbc11..c5992ebfb8f9c 100644 --- a/dotnet/src/webdriver/Chromium/ChromiumOptions.cs +++ b/dotnet/src/webdriver/Chromium/ChromiumOptions.cs @@ -22,6 +22,8 @@ using System.Collections.ObjectModel; using System.IO; +#nullable enable + namespace OpenQA.Selenium.Chromium { /// @@ -42,25 +44,18 @@ public abstract class ChromiumOptions : DriverOptions private const string PerformanceLoggingPreferencesChromeOption = "perfLoggingPrefs"; private const string WindowTypesChromeOption = "windowTypes"; private const string UseSpecCompliantProtocolOption = "w3c"; - - private bool leaveBrowserRunning; private bool useSpecCompliantProtocol = true; - private string binaryLocation; - private string debuggerAddress; - private string minidumpPath; - private List arguments = new List(); - private List extensionFiles = new List(); - private List encodedExtensions = new List(); - private List excludedSwitches = new List(); - private List windowTypes = new List(); - private Dictionary additionalChromeOptions = new Dictionary(); - private Dictionary userProfilePreferences; - private Dictionary localStatePreferences; - - private string mobileEmulationDeviceName; - private ChromiumMobileEmulationDeviceSettings mobileEmulationDeviceSettings; - private ChromiumPerformanceLoggingPreferences perfLoggingPreferences; - private ChromiumAndroidOptions androidOptions; + private readonly List arguments = new List(); + private readonly List extensionFiles = new List(); + private readonly List encodedExtensions = new List(); + private readonly List excludedSwitches = new List(); + private readonly List windowTypes = new List(); + private readonly Dictionary additionalChromeOptions = new Dictionary(); + private Dictionary? userProfilePreferences; + private Dictionary? localStatePreferences; + + private string? mobileEmulationDeviceName; + private ChromiumMobileEmulationDeviceSettings? mobileEmulationDeviceSettings; /// /// Initializes a new instance of the class. @@ -90,10 +85,7 @@ public ChromiumOptions() : base() /// protected abstract string VendorPrefix { get; } - private string LoggingPreferencesChromeOption - { - get { return this.VendorPrefix + ":loggingPrefs"; } - } + private string LoggingPreferencesChromeOption => this.VendorPrefix + ":loggingPrefs"; /// /// Gets the name of the capability used to store Chromium options in @@ -104,29 +96,18 @@ private string LoggingPreferencesChromeOption /// /// Gets or sets the location of the Chromium browser's binary executable file. /// - public override string BinaryLocation - { - get { return this.binaryLocation; } - set { this.binaryLocation = value; } - } + public override string? BinaryLocation { get; set; } /// /// Gets or sets a value indicating whether Chromium should be left running after the /// ChromeDriver instance is exited. Defaults to . /// - public bool LeaveBrowserRunning - { - get { return this.leaveBrowserRunning; } - set { this.leaveBrowserRunning = value; } - } + public bool LeaveBrowserRunning { get; set; } /// /// Gets the list of arguments appended to the Chromium command line as a string array. /// - public ReadOnlyCollection Arguments - { - get { return this.arguments.AsReadOnly(); } - } + public ReadOnlyCollection Arguments => this.arguments.AsReadOnly(); /// /// Gets the list of extensions to be installed as an array of base64-encoded strings. @@ -151,43 +132,28 @@ public ReadOnlyCollection Extensions /// Gets or sets the address of a Chromium debugger server to connect to. /// Should be of the form "{hostname|IP address}:port". /// - public string DebuggerAddress - { - get { return this.debuggerAddress; } - set { this.debuggerAddress = value; } - } + public string? DebuggerAddress { get; set; } /// /// Gets or sets the directory in which to store minidump files. /// - public string MinidumpPath - { - get { return this.minidumpPath; } - set { this.minidumpPath = value; } - } + public string? MinidumpPath { get; set; } /// /// Gets or sets the performance logging preferences for the driver. /// - public ChromiumPerformanceLoggingPreferences PerformanceLoggingPreferences - { - get { return this.perfLoggingPreferences; } - set { this.perfLoggingPreferences = value; } - } + public ChromiumPerformanceLoggingPreferences? PerformanceLoggingPreferences { get; set; } /// /// Gets or sets the options for automating Chromium applications on Android. /// - public ChromiumAndroidOptions AndroidOptions - { - get { return this.androidOptions; } - set { this.androidOptions = value; } - } + public ChromiumAndroidOptions? AndroidOptions { get; set; } /// /// Adds a single argument to the list of arguments to be appended to the browser executable command line. /// /// The argument to add. + /// If is or . public void AddArgument(string argument) { if (string.IsNullOrEmpty(argument)) @@ -202,15 +168,17 @@ public void AddArgument(string argument) /// Adds arguments to be appended to the browser executable command line. /// /// An array of arguments to add. + /// If is . public void AddArguments(params string[] argumentsToAdd) { - this.AddArguments(new List(argumentsToAdd)); + this.AddArguments((IEnumerable)argumentsToAdd); } /// /// Adds arguments to be appended to the browser executable command line. /// /// An object of arguments to add. + /// If is . public void AddArguments(IEnumerable argumentsToAdd) { if (argumentsToAdd == null) @@ -226,6 +194,7 @@ public void AddArguments(IEnumerable argumentsToAdd) /// to the browser executable command line by chromedriver.exe. /// /// The argument to exclude. + /// If is or . public void AddExcludedArgument(string argument) { if (string.IsNullOrEmpty(argument)) @@ -241,9 +210,10 @@ public void AddExcludedArgument(string argument) /// to the browser executable command line by chromedriver.exe. /// /// An array of arguments to exclude. + /// If is . public void AddExcludedArguments(params string[] argumentsToExclude) { - this.AddExcludedArguments(new List(argumentsToExclude)); + this.AddExcludedArguments((IEnumerable)argumentsToExclude); } /// @@ -251,6 +221,7 @@ public void AddExcludedArguments(params string[] argumentsToExclude) /// to the browser executable command line by chromedriver.exe. /// /// An object of arguments to exclude. + /// If is . public void AddExcludedArguments(IEnumerable argumentsToExclude) { if (argumentsToExclude == null) @@ -266,6 +237,7 @@ public void AddExcludedArguments(IEnumerable argumentsToExclude) /// to be installed in the instance of Chrome. /// /// The full path to the extension to add. + /// If is or . public void AddExtension(string pathToExtension) { if (string.IsNullOrEmpty(pathToExtension)) @@ -281,9 +253,11 @@ public void AddExtension(string pathToExtension) /// in the instance of Chrome. /// /// An array of full paths to the extensions to add. + /// If is . + /// If any extension file path does not point to a file. public void AddExtensions(params string[] extensions) { - this.AddExtensions(new List(extensions)); + this.AddExtensions((IEnumerable)extensions); } /// @@ -291,6 +265,8 @@ public void AddExtensions(params string[] extensions) /// in the instance of Chrome. /// /// An of full paths to the extensions to add. + /// If is . + /// If any extension file path does not point to a file. public void AddExtensions(IEnumerable extensions) { if (extensions == null) @@ -314,6 +290,8 @@ public void AddExtensions(IEnumerable extensions) /// to be installed in the instance of Chrome. /// /// A base64-encoded string representing the extension to add. + /// If is or . + /// If the extension string is not valid base-64. public void AddEncodedExtension(string extension) { if (string.IsNullOrEmpty(extension)) @@ -329,9 +307,11 @@ public void AddEncodedExtension(string extension) /// to be installed in the instance of Chrome. /// /// An array of base64-encoded strings representing the extensions to add. + /// If is . + /// If an extension string is not valid base-64. public void AddEncodedExtensions(params string[] extensions) { - this.AddEncodedExtensions(new List(extensions)); + this.AddEncodedExtensions((IEnumerable)extensions); } /// @@ -340,6 +320,8 @@ public void AddEncodedExtensions(params string[] extensions) /// /// An of base64-encoded strings /// representing the extensions to add. + /// If is . + /// If an extension string is not valid base-64. public void AddEncodedExtensions(IEnumerable extensions) { if (extensions == null) @@ -370,6 +352,7 @@ public void AddEncodedExtensions(IEnumerable extensions) /// /// The name of the preference to set. /// The value of the preference to set. + /// If is . public void AddUserProfilePreference(string preferenceName, object preferenceValue) { if (this.userProfilePreferences == null) @@ -386,6 +369,7 @@ public void AddUserProfilePreference(string preferenceName, object preferenceVal /// /// The name of the preference to set. /// The value of the preference to set. + /// If is . public void AddLocalStatePreference(string preferenceName, object preferenceValue) { if (this.localStatePreferences == null) @@ -404,7 +388,7 @@ public void AddLocalStatePreference(string preferenceName, object preferenceValu /// 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 as the argument. - public void EnableMobileEmulation(string deviceName) + public void EnableMobileEmulation(string? deviceName) { this.mobileEmulationDeviceSettings = null; this.mobileEmulationDeviceName = deviceName; @@ -420,7 +404,7 @@ public void EnableMobileEmulation(string deviceName) /// 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 as the argument. - public void EnableMobileEmulation(ChromiumMobileEmulationDeviceSettings deviceSettings) + public void EnableMobileEmulation(ChromiumMobileEmulationDeviceSettings? deviceSettings) { this.mobileEmulationDeviceName = null; if (deviceSettings != null && string.IsNullOrEmpty(deviceSettings.UserAgent)) @@ -438,6 +422,7 @@ public void EnableMobileEmulation(ChromiumMobileEmulationDeviceSettings deviceSe /// The name of the window type to add. /// This method can be used to allow the driver to access {webview} /// elements by adding "webview" as a window type. + /// If is or . public void AddWindowType(string windowType) { if (string.IsNullOrEmpty(windowType)) @@ -453,9 +438,10 @@ public void AddWindowType(string windowType) /// returned by the Chromium driver. /// /// An array of window types to add. + /// If is . public void AddWindowTypes(params string[] windowTypesToAdd) { - this.AddWindowTypes(new List(windowTypesToAdd)); + this.AddWindowTypes((IEnumerable)windowTypesToAdd); } /// @@ -463,6 +449,7 @@ public void AddWindowTypes(params string[] windowTypesToAdd) /// returned by the Chromium driver. /// /// An of window types to add. + /// If is . public void AddWindowTypes(IEnumerable windowTypesToAdd) { if (windowTypesToAdd == null) @@ -488,6 +475,10 @@ public void AddWindowTypes(IEnumerable windowTypesToAdd) /// existing value with the new value in . /// Calling this method adds capabilities to the Chromium-specific options object passed to /// webdriver executable (e.g. property name 'goog:chromeOptions'). + /// + /// thrown when attempting to add a capability for which there is already a type safe option, or + /// when is or the empty string. + /// protected void AddAdditionalChromiumOption(string optionName, object optionValue) { this.ValidateCapabilityName(optionName); @@ -509,7 +500,7 @@ public override ICapabilities ToCapabilities() AddVendorSpecificChromiumCapabilities(capabilities); - Dictionary loggingPreferences = this.GenerateLoggingPreferencesDictionary(); + Dictionary? loggingPreferences = this.GenerateLoggingPreferencesDictionary(); if (loggingPreferences != null) { capabilities.SetCapability(LoggingPreferencesChromeOption, loggingPreferences); @@ -534,9 +525,9 @@ private Dictionary BuildChromeOptionsDictionary() chromeOptions[ArgumentsChromeOption] = this.Arguments; } - if (!string.IsNullOrEmpty(this.binaryLocation)) + if (!string.IsNullOrEmpty(this.BinaryLocation)) { - chromeOptions[BinaryChromeOption] = this.binaryLocation; + chromeOptions[BinaryChromeOption] = this.BinaryLocation!; } ReadOnlyCollection extensions = this.Extensions; @@ -555,9 +546,9 @@ private Dictionary BuildChromeOptionsDictionary() chromeOptions[PreferencesChromeOption] = this.userProfilePreferences; } - if (this.leaveBrowserRunning) + if (this.LeaveBrowserRunning) { - chromeOptions[DetachChromeOption] = this.leaveBrowserRunning; + chromeOptions[DetachChromeOption] = this.LeaveBrowserRunning; } if (!this.useSpecCompliantProtocol) @@ -565,9 +556,9 @@ private Dictionary BuildChromeOptionsDictionary() chromeOptions[UseSpecCompliantProtocolOption] = this.useSpecCompliantProtocol; } - if (!string.IsNullOrEmpty(this.debuggerAddress)) + if (!string.IsNullOrEmpty(this.DebuggerAddress)) { - chromeOptions[DebuggerAddressChromeOption] = this.debuggerAddress; + chromeOptions[DebuggerAddressChromeOption] = this.DebuggerAddress!; } if (this.excludedSwitches.Count > 0) @@ -575,24 +566,24 @@ private Dictionary BuildChromeOptionsDictionary() chromeOptions[ExcludeSwitchesChromeOption] = this.excludedSwitches; } - if (!string.IsNullOrEmpty(this.minidumpPath)) + if (!string.IsNullOrEmpty(this.MinidumpPath)) { - chromeOptions[MinidumpPathChromeOption] = this.minidumpPath; + chromeOptions[MinidumpPathChromeOption] = this.MinidumpPath!; } if (!string.IsNullOrEmpty(this.mobileEmulationDeviceName) || this.mobileEmulationDeviceSettings != null) { - chromeOptions[MobileEmulationChromeOption] = this.GenerateMobileEmulationSettingsDictionary(); + chromeOptions[MobileEmulationChromeOption] = GenerateMobileEmulationSettingsDictionary(this.mobileEmulationDeviceSettings, this.mobileEmulationDeviceName); } - if (this.perfLoggingPreferences != null) + if (this.PerformanceLoggingPreferences != null) { - chromeOptions[PerformanceLoggingPreferencesChromeOption] = this.GeneratePerformanceLoggingPreferencesDictionary(); + chromeOptions[PerformanceLoggingPreferencesChromeOption] = GeneratePerformanceLoggingPreferencesDictionary(this.PerformanceLoggingPreferences); } - if (this.androidOptions != null) + if (this.AndroidOptions != null) { - this.AddAndroidOptions(chromeOptions); + AddAndroidOptions(chromeOptions, this.AndroidOptions); } if (this.windowTypes.Count > 0) @@ -608,66 +599,66 @@ private Dictionary BuildChromeOptionsDictionary() return chromeOptions; } - private void AddAndroidOptions(Dictionary chromeOptions) + private static void AddAndroidOptions(Dictionary chromeOptions, ChromiumAndroidOptions androidOptions) { - chromeOptions["androidPackage"] = this.androidOptions.AndroidPackage; + chromeOptions["androidPackage"] = androidOptions.AndroidPackage; - if (!string.IsNullOrEmpty(this.androidOptions.AndroidDeviceSerial)) + if (!string.IsNullOrEmpty(androidOptions.AndroidDeviceSerial)) { - chromeOptions["androidDeviceSerial"] = this.androidOptions.AndroidDeviceSerial; + chromeOptions["androidDeviceSerial"] = androidOptions.AndroidDeviceSerial!; } - if (!string.IsNullOrEmpty(this.androidOptions.AndroidActivity)) + if (!string.IsNullOrEmpty(androidOptions.AndroidActivity)) { - chromeOptions["androidActivity"] = this.androidOptions.AndroidActivity; + chromeOptions["androidActivity"] = androidOptions.AndroidActivity!; } - if (!string.IsNullOrEmpty(this.androidOptions.AndroidProcess)) + if (!string.IsNullOrEmpty(androidOptions.AndroidProcess)) { - chromeOptions["androidProcess"] = this.androidOptions.AndroidProcess; + chromeOptions["androidProcess"] = androidOptions.AndroidProcess; } - if (this.androidOptions.UseRunningApp) + if (androidOptions.UseRunningApp) { - chromeOptions["androidUseRunningApp"] = this.androidOptions.UseRunningApp; + chromeOptions["androidUseRunningApp"] = androidOptions.UseRunningApp; } } - private Dictionary GeneratePerformanceLoggingPreferencesDictionary() + private static Dictionary GeneratePerformanceLoggingPreferencesDictionary(ChromiumPerformanceLoggingPreferences prefs) { Dictionary perfLoggingPrefsDictionary = new Dictionary(); - perfLoggingPrefsDictionary["enableNetwork"] = this.perfLoggingPreferences.IsCollectingNetworkEvents; - perfLoggingPrefsDictionary["enablePage"] = this.perfLoggingPreferences.IsCollectingPageEvents; + perfLoggingPrefsDictionary["enableNetwork"] = prefs.IsCollectingNetworkEvents; + perfLoggingPrefsDictionary["enablePage"] = prefs.IsCollectingPageEvents; - string tracingCategories = this.perfLoggingPreferences.TracingCategories; + string tracingCategories = prefs.TracingCategories; if (!string.IsNullOrEmpty(tracingCategories)) { perfLoggingPrefsDictionary["traceCategories"] = tracingCategories; } - perfLoggingPrefsDictionary["bufferUsageReportingInterval"] = Convert.ToInt64(this.perfLoggingPreferences.BufferUsageReportingInterval.TotalMilliseconds); + perfLoggingPrefsDictionary["bufferUsageReportingInterval"] = Convert.ToInt64(prefs.BufferUsageReportingInterval.TotalMilliseconds); return perfLoggingPrefsDictionary; } - private Dictionary GenerateMobileEmulationSettingsDictionary() + private static Dictionary GenerateMobileEmulationSettingsDictionary(ChromiumMobileEmulationDeviceSettings? settings, string? deviceName) { Dictionary mobileEmulationSettings = new Dictionary(); - if (!string.IsNullOrEmpty(this.mobileEmulationDeviceName)) + if (!string.IsNullOrEmpty(deviceName)) { - mobileEmulationSettings["deviceName"] = this.mobileEmulationDeviceName; + mobileEmulationSettings["deviceName"] = deviceName!; } - else if (this.mobileEmulationDeviceSettings != null) + else if (settings != null) { - mobileEmulationSettings["userAgent"] = this.mobileEmulationDeviceSettings.UserAgent; + mobileEmulationSettings["userAgent"] = settings.UserAgent; Dictionary deviceMetrics = new Dictionary(); - deviceMetrics["width"] = this.mobileEmulationDeviceSettings.Width; - deviceMetrics["height"] = this.mobileEmulationDeviceSettings.Height; - deviceMetrics["pixelRatio"] = this.mobileEmulationDeviceSettings.PixelRatio; - if (!this.mobileEmulationDeviceSettings.EnableTouchEvents) + deviceMetrics["width"] = settings.Width; + deviceMetrics["height"] = settings.Height; + deviceMetrics["pixelRatio"] = settings.PixelRatio; + if (!settings.EnableTouchEvents) { - deviceMetrics["touch"] = this.mobileEmulationDeviceSettings.EnableTouchEvents; + deviceMetrics["touch"] = settings.EnableTouchEvents; } mobileEmulationSettings["deviceMetrics"] = deviceMetrics;