Skip to content

Commit

Permalink
Adding ability to install/uninstall Firefox add-ons to .NET
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Sep 4, 2019
1 parent d3ac5e0 commit 1f614ef
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion dotnet/src/webdriver/Firefox/FirefoxDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ namespace OpenQA.Selenium.Firefox
public class FirefoxDriver : RemoteWebDriver
{
private const string SetContextCommand = "setContext";
private const string InstallAddOnCommand = "installAddOn";
private const string UninstallAddOnCommand = "uninstallAddOn";

/// <summary>
/// Initializes a new instance of the <see cref="FirefoxDriver"/> class.
Expand Down Expand Up @@ -148,7 +150,9 @@ public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options, TimeS
: base(new DriverServiceCommandExecutor(service, commandTimeout), ConvertOptionsToCapabilities(options))
{
// Add the custom commands unique to Firefox
this.CommandExecutor.CommandInfoRepository.TryAddCommand(SetContextCommand, new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/moz/context"));
this.AddCustomFirefoxCommand(SetContextCommand, CommandInfo.PostCommand, "/session/{sessionId}/moz/context");
this.AddCustomFirefoxCommand(InstallAddOnCommand, CommandInfo.PostCommand, "/session/{sessionId}/moz/addon/install");
this.AddCustomFirefoxCommand(UninstallAddOnCommand, CommandInfo.PostCommand, "/session/{sessionId}/moz/addon/uninstall");
}

/// <summary>
Expand Down Expand Up @@ -179,6 +183,65 @@ public void SetContext(FirefoxCommandContext context)
Response response = this.Execute(SetContextCommand, parameters);
}

/// <summary>
/// Installs a Firefox add-on from a file, typically a .xpi file.
/// </summary>
/// <param name="addOnFileToInstall">Full path and file name of the add-on to install.</param>
public void InstallAddOnFromFile(string addOnFileToInstall)
{
if (string.IsNullOrEmpty(addOnFileToInstall))
{
throw new ArgumentNullException("addOnFileToInstall", "Add-on file name must not be null or the empty string");
}

if (!File.Exists(addOnFileToInstall))
{
throw new ArgumentException("File " + addOnFileToInstall + " does not exist", "addOnFileToInstall");
}

// Implementation note: There is a version of the install add-on
// command that can be used with a file name directly, by passing
// a "path" property in the parameters object of the command. If
// delegating to the "use the base64-encoded blob" version causes
// issues, we can change this method to use the file name directly
// instead.
byte[] addOnBytes = File.ReadAllBytes(addOnFileToInstall);
string base64AddOn = Convert.ToBase64String(addOnBytes);
this.InstallAddOn(base64AddOn);
}

/// <summary>
/// Installs a Firefox add-on.
/// </summary>
/// <param name="base64EncodedAddOn">The base64-encoded string representation of the add-on binary.</param>
public void InstallAddOn(string base64EncodedAddOn)
{
if (string.IsNullOrEmpty(base64EncodedAddOn))
{
throw new ArgumentNullException("base64EncodedAddOn", "Base64 encoded add-on must not be null or the empty string");
}

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters["addon"] = base64EncodedAddOn;
this.Execute(InstallAddOnCommand, parameters);
}

/// <summary>
/// Uninstalls a Firefox add-on.
/// </summary>
/// <param name="addOnId">The ID of the add-on to uninstall.</param>
public void UninstallAddOn(string addOnId)
{
if (string.IsNullOrEmpty(addOnId))
{
throw new ArgumentNullException("addOnId", "Base64 encoded add-on must not be null or the empty string");
}

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters["id"] = addOnId;
this.Execute(UninstallAddOnCommand, parameters);
}

/// <summary>
/// In derived classes, the <see cref="PrepareEnvironment"/> method prepares the environment for test execution.
/// </summary>
Expand All @@ -201,5 +264,11 @@ private static FirefoxDriverService CreateService(FirefoxOptions options)
{
return FirefoxDriverService.CreateDefaultService();
}

private void AddCustomFirefoxCommand(string commandName, string method, string resourcePath)
{
CommandInfo commandInfoToAdd = new CommandInfo(method, resourcePath);
this.CommandExecutor.CommandInfoRepository.TryAddCommand(commandName, commandInfoToAdd);
}
}
}

0 comments on commit 1f614ef

Please sign in to comment.