Skip to content

Commit

Permalink
[java] use FileDetector to install Firefox addons if path not found
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 28, 2021
1 parent 05a3a5e commit e07a40f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 25 additions & 3 deletions java/src/org/openqa/selenium/firefox/AddHasExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<HasExtensions>, AdditionalHttpCommands {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]");
String id = ((HasExtensions) driver).installExtension(extension);
((HasExtensions) driver).uninstallExtension(id);
}

@Test
Expand Down

0 comments on commit e07a40f

Please sign in to comment.