From b624d1457379e01a1ed7fe58b016814cd64a955d Mon Sep 17 00:00:00 2001 From: Michael Render Date: Fri, 27 Dec 2024 18:41:13 -0500 Subject: [PATCH 1/2] [dotnet] Add nullability to `FirefoxExtension` --- .../src/webdriver/Firefox/FirefoxExtension.cs | 43 ++++++++----------- .../src/webdriver/Firefox/FirefoxProfile.cs | 6 +++ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/dotnet/src/webdriver/Firefox/FirefoxExtension.cs b/dotnet/src/webdriver/Firefox/FirefoxExtension.cs index f146eb27268ce..969def81d9232 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,18 @@ private static string ReadIdFromInstallRdf(string root) private static string ReadIdFromManifestJson(string root) { - string id = null; + string id; 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)) + else { - 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)); } From 9e0a26b226a4eb7aba29a4f9b575a74da3f4b672 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Sat, 28 Dec 2024 12:28:27 -0500 Subject: [PATCH 2/2] Handle when applications/gecko.id is all-whitespace --- dotnet/src/webdriver/Firefox/FirefoxExtension.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dotnet/src/webdriver/Firefox/FirefoxExtension.cs b/dotnet/src/webdriver/Firefox/FirefoxExtension.cs index 969def81d9232..1126ec952a4a8 100644 --- a/dotnet/src/webdriver/Firefox/FirefoxExtension.cs +++ b/dotnet/src/webdriver/Firefox/FirefoxExtension.cs @@ -178,7 +178,7 @@ private static string ReadIdFromInstallRdf(string root) private static string ReadIdFromManifestJson(string root) { - string id; + string id = string.Empty; string manifestJsonPath = Path.Combine(root, JsonManifestFileName); var manifestObject = JsonNode.Parse(File.ReadAllText(manifestJsonPath)); @@ -186,7 +186,8 @@ private static string ReadIdFromManifestJson(string root) { id = idNode.ToString().Trim(); } - else + + if (string.IsNullOrEmpty(id)) { string addInName = manifestObject["name"]!.ToString().Replace(" ", ""); string addInVersion = manifestObject["version"]!.ToString();