From 54a03b9d64a232f8764f58f7f03ac298433c1ea1 Mon Sep 17 00:00:00 2001 From: titusfortner Date: Tue, 28 Sep 2021 10:48:32 -0500 Subject: [PATCH] [java] use FileDetector to install Firefox addons --- .../selenium/firefox/AddHasExtensions.java | 27 +++++++++++++++++++ .../selenium/firefox/FirefoxDriver.java | 7 +++++ .../selenium/firefox/HasExtensions.java | 10 +++++++ .../selenium/firefox/FirefoxDriverTest.java | 13 ++++++--- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/java/src/org/openqa/selenium/firefox/AddHasExtensions.java b/java/src/org/openqa/selenium/firefox/AddHasExtensions.java index 938e5a3b575b2..07e81677efc9f 100644 --- a/java/src/org/openqa/selenium/firefox/AddHasExtensions.java +++ b/java/src/org/openqa/selenium/firefox/AddHasExtensions.java @@ -20,18 +20,25 @@ import com.google.auto.service.AutoService; import com.google.common.collect.ImmutableMap; import org.openqa.selenium.Capabilities; +import org.openqa.selenium.WebDriverException; import org.openqa.selenium.internal.Require; +import org.openqa.selenium.io.Zip; import org.openqa.selenium.remote.AdditionalHttpCommands; import org.openqa.selenium.remote.AugmenterProvider; import org.openqa.selenium.remote.CommandInfo; import org.openqa.selenium.remote.ExecuteMethod; +import org.openqa.selenium.remote.FileDetector; import org.openqa.selenium.remote.http.HttpMethod; +import java.io.File; +import java.io.IOException; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Map; import java.util.function.Predicate; import static org.openqa.selenium.remote.BrowserType.FIREFOX; +import static org.openqa.selenium.remote.DriverCommand.UPLOAD_FILE; @AutoService({AdditionalHttpCommands.class, AugmenterProvider.class}) public class AddHasExtensions implements AugmenterProvider, AdditionalHttpCommands { @@ -70,6 +77,26 @@ public String installExtension(Path path) { ImmutableMap.of("path", path.toAbsolutePath().toString(), "temporary", false)); } + @Override + public String installExtension(Path path, FileDetector fileDetector) { + Require.nonNull("Extension Path", path); + Require.nonNull("Extension Path", fileDetector); + + File localFile = fileDetector.getLocalFile(path.toString()); + + if (!localFile.isFile()) { + throw new WebDriverException("You may only upload files: " + localFile); + } + + try { + String zip = Zip.zip(localFile); + String newPath = (String) executeMethod.execute(UPLOAD_FILE, ImmutableMap.of("file", zip)); + return installExtension(Paths.get(newPath)); + } catch (IOException e) { + throw new WebDriverException("Cannot upload " + localFile, e); + } + } + @Override public void uninstallExtension(String extensionId) { Require.nonNull("Extension ID", extensionId); diff --git a/java/src/org/openqa/selenium/firefox/FirefoxDriver.java b/java/src/org/openqa/selenium/firefox/FirefoxDriver.java index b1459ffebda5c..118a67efe3818 100644 --- a/java/src/org/openqa/selenium/firefox/FirefoxDriver.java +++ b/java/src/org/openqa/selenium/firefox/FirefoxDriver.java @@ -269,6 +269,13 @@ public String installExtension(Path path) { return extensions.installExtension(path); } + @Override + public String installExtension(Path path, FileDetector detector) { + throw new WebDriverException( + "File detector only works on remote webdriver instances obtained " + + "via RemoteWebDriver"); + } + @Override public void uninstallExtension(String extensionId) { Require.nonNull("Extension ID", extensionId); diff --git a/java/src/org/openqa/selenium/firefox/HasExtensions.java b/java/src/org/openqa/selenium/firefox/HasExtensions.java index 31a534855b8fa..ca8481a73fbd4 100644 --- a/java/src/org/openqa/selenium/firefox/HasExtensions.java +++ b/java/src/org/openqa/selenium/firefox/HasExtensions.java @@ -18,6 +18,7 @@ package org.openqa.selenium.firefox; import org.openqa.selenium.Beta; +import org.openqa.selenium.remote.FileDetector; import java.nio.file.Path; @@ -35,6 +36,15 @@ public interface HasExtensions { */ String installExtension(Path path); + /** + * Installs an extension. + * + * @param path absolute path to the extension file that should be installed. + * @param detector detector for use with Remote webdriver + * @return the unique identifier of the installed extension. + */ + String installExtension(Path path, FileDetector detector); + /** * Uninstall the extension by the given identifier. * This value can be found in the extension's manifest, and typically ends with "@mozilla.org". diff --git a/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java b/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java index 1e746f9bd0a56..2018fab81ebdf 100644 --- a/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java +++ b/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java @@ -40,6 +40,7 @@ import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.SessionId; import org.openqa.selenium.remote.UnreachableBrowserException; +import org.openqa.selenium.remote.UselessFileDetector; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.testing.Ignore; @@ -501,10 +502,16 @@ public void testFirefoxCanNativelyClickOverlappingElements() { @Test public void canAddRemoveExtensions() { - Path extension = InProject.locate("third_party/firebug/favourite_colour-1.1-an+fx.xpi"); + HasExtensions extensions = (HasExtensions) driver; + String extensionID; - ((HasExtensions) driver).installExtension(extension); - ((HasExtensions) driver).uninstallExtension("favourite-colour-examples@mozilla.org"); + Path extension = InProject.locate("third_party/firebug/favourite_colour-1.1-an+fx.xpi"); + if (((RemoteWebDriver) driver).getFileDetector().getClass() != UselessFileDetector.class) { + extensionID = extensions.installExtension(extension, ((RemoteWebDriver) driver).getFileDetector()); + } else { + extensionID = extensions.installExtension(extension); + } + extensions.uninstallExtension(extensionID); } @Test