diff --git a/dotnet/src/webdriver/Firefox/FirefoxExtension.cs b/dotnet/src/webdriver/Firefox/FirefoxExtension.cs index f146eb27268ce..1126ec952a4a8 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxExtension.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxExtension.cs @@ -25,7 +25,7 @@ using System.Text.Json.Nodes; using System.Xml; - +#nullable enable namespace OpenQA.Selenium.Firefox { @@ -38,8 +38,8 @@ public class FirefoxExtension private const string RdfManifestFileName = "install.rdf"; private const string JsonManifestFileName = "manifest.json"; - private string extensionFileName; - private string extensionResourceId; + private readonly string extensionFileName; + private readonly string extensionResourceId; /// /// Initializes a new instance of the class. @@ -48,6 +48,7 @@ public class FirefoxExtension /// WebDriver attempts to resolve the parameter /// by looking first for the specified file in the directory of the calling assembly, /// then using the full path to the file, if a full path is provided. + /// If is . public FirefoxExtension(string fileName) : this(fileName, string.Empty) { @@ -65,16 +66,18 @@ public FirefoxExtension(string fileName) /// not found in the file system, WebDriver attempts to locate a resource in the /// executing assembly with the name specified by the /// parameter. + /// If or are . internal FirefoxExtension(string fileName, string resourceId) { - this.extensionFileName = fileName; - this.extensionResourceId = resourceId; + this.extensionFileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); + this.extensionResourceId = resourceId ?? throw new ArgumentNullException(nameof(resourceId)); } /// /// Installs the extension into a profile directory. /// /// The Firefox profile directory into which to install the extension. + /// If is . public void Install(string profileDirectory) { DirectoryInfo info = new DirectoryInfo(profileDirectory); @@ -132,7 +135,7 @@ private static string GetExtensionId(string root) private static string ReadIdFromInstallRdf(string root) { - string id = null; + string id; string installRdf = Path.Combine(root, "install.rdf"); try { @@ -143,11 +146,11 @@ private static string ReadIdFromInstallRdf(string root) rdfNamespaceManager.AddNamespace("em", EmNamespaceUri); rdfNamespaceManager.AddNamespace("RDF", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); - XmlNode node = rdfXmlDocument.SelectSingleNode("//em:id", rdfNamespaceManager); + XmlNode? node = rdfXmlDocument.SelectSingleNode("//em:id", rdfNamespaceManager); if (node == null) { - XmlNode descriptionNode = rdfXmlDocument.SelectSingleNode("//RDF:Description", rdfNamespaceManager); - XmlAttribute attribute = descriptionNode.Attributes["id", EmNamespaceUri]; + XmlNode? descriptionNode = rdfXmlDocument.SelectSingleNode("//RDF:Description", rdfNamespaceManager); + XmlAttribute? attribute = descriptionNode?.Attributes?["id", EmNamespaceUri]; if (attribute == null) { throw new WebDriverException("Cannot locate node containing extension id: " + installRdf); @@ -175,26 +178,19 @@ private static string ReadIdFromInstallRdf(string root) private static string ReadIdFromManifestJson(string root) { - string id = null; + string id = string.Empty; string manifestJsonPath = Path.Combine(root, JsonManifestFileName); + var manifestObject = JsonNode.Parse(File.ReadAllText(manifestJsonPath)); - if (manifestObject["applications"] != null) + if (manifestObject!["applications"]?["gecko"]?["id"] is { } idNode) { - var applicationObject = manifestObject["applications"]; - if (applicationObject["gecko"] != null) - { - var geckoObject = applicationObject["gecko"]; - if (geckoObject["id"] != null) - { - id = geckoObject["id"].ToString().Trim(); - } - } + id = idNode.ToString().Trim(); } if (string.IsNullOrEmpty(id)) { - string addInName = manifestObject["name"].ToString().Replace(" ", ""); - string addInVersion = manifestObject["version"].ToString(); + string addInName = manifestObject["name"]!.ToString().Replace(" ", ""); + string addInVersion = manifestObject["version"]!.ToString(); id = string.Format(CultureInfo.InvariantCulture, "{0}@{1}", addInName, addInVersion); } diff --git a/dotnet/src/webdriver/Firefox/FirefoxProfile.cs b/dotnet/src/webdriver/Firefox/FirefoxProfile.cs index cf340826db916..0998cb436ea94 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxProfile.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxProfile.cs @@ -114,8 +114,14 @@ public static FirefoxProfile FromBase64String(string base64) /// Adds a Firefox Extension to this profile /// /// The path to the new extension + /// If is . public void AddExtension(string extensionToInstall) { + if (extensionToInstall is null) + { + throw new ArgumentNullException(nameof(extensionToInstall)); + } + this.extensions.Add(Path.GetFileNameWithoutExtension(extensionToInstall), new FirefoxExtension(extensionToInstall)); }