Skip to content

Instantly share code, notes, and snippets.

@jimevans
Created September 5, 2019 19:11
Show Gist options
  • Save jimevans/52b9673340ce7561574b9021a535cc54 to your computer and use it in GitHub Desktop.
Save jimevans/52b9673340ce7561574b9021a535cc54 to your computer and use it in GitHub Desktop.
public class ExtendedFirefoxDriver : FirefoxDriver
{
// NOTE: Add constructor overloads to support the instantiations your tests require.
public ExtendedFirefoxDriver(string servicePath, FirefoxOptions options) : base(servicePath, options)
{
this.CommandExecutor.CommandInfoRepository.TryAddCommand("installAddOn", new CommandInfo(CommandInfo.PostCommand, "/session/{sessionId}/moz/addon/install"));
}
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");
}
byte[] addOnBytes = File.ReadAllBytes(addOnFileToInstall);
string base64AddOn = Convert.ToBase64String(addOnBytes);
this.InstallAddOn(base64AddOn);
}
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("installAddOn", parameters);
}
}
class Program
{
static void Main(string[] args)
{
string geckodriverDirectory = @"C:\path\to\directory\containing\geckodriver";
string addIn = @"C:\path\to\downloaded\extension\adblock_plus-3.6.3-an+fx.xpi";
ExtendedFirefoxDriver ffDriver = new ExtendedFirefoxDriver(geckodriverDirectory, new FirefoxOptions());
ffDriver.InstallAddOnFromFile(addIn);
Console.ReadLine();
ffDriver.Quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment