From e07a40f532378fa637f7628d33aa82aaf3d3023d Mon Sep 17 00:00:00 2001 From: titusfortner Date: Tue, 28 Sep 2021 11:53:23 -0500 Subject: [PATCH] [java] use FileDetector to install Firefox addons if path not found --- .../selenium/firefox/AddHasExtensions.java | 28 +++++++++++++++++-- .../selenium/firefox/FirefoxDriverTest.java | 4 +-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/java/src/org/openqa/selenium/firefox/AddHasExtensions.java b/java/src/org/openqa/selenium/firefox/AddHasExtensions.java index 938e5a3b575b2..3528cc50bf3f7 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.LocalFileDetector; 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 { @@ -65,9 +72,24 @@ public HasExtensions getImplementation(Capabilities capabilities, ExecuteMethod public String installExtension(Path path) { Require.nonNull("Extension Path", path); - return (String) executeMethod.execute( - INSTALL_EXTENSION, - ImmutableMap.of("path", path.toAbsolutePath().toString(), "temporary", false)); + try { + return (String) executeMethod.execute( + INSTALL_EXTENSION, + ImmutableMap.of("path", path.toAbsolutePath().toString(), "temporary", false)); + } catch (WebDriverException ex) { + File localFile = new LocalFileDetector().getLocalFile(path.toString()); + if (ex.getMessage().contains("No such file or directory") && localFile != null) { + 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); + } + } else { + throw ex; + } + } } @Override diff --git a/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java b/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java index 1e746f9bd0a56..bf953a870bb83 100644 --- a/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java +++ b/java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java @@ -503,8 +503,8 @@ public void testFirefoxCanNativelyClickOverlappingElements() { public void canAddRemoveExtensions() { Path extension = InProject.locate("third_party/firebug/favourite_colour-1.1-an+fx.xpi"); - ((HasExtensions) driver).installExtension(extension); - ((HasExtensions) driver).uninstallExtension("favourite-colour-examples@mozilla.org"); + String id = ((HasExtensions) driver).installExtension(extension); + ((HasExtensions) driver).uninstallExtension(id); } @Test