Skip to content

Commit

Permalink
[java] use FileDetector to install Firefox addons
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 28, 2021
1 parent 05a3a5e commit 54a03b9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
27 changes: 27 additions & 0 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.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<HasExtensions>, AdditionalHttpCommands {
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions java/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions java/src/org/openqa/selenium/firefox/HasExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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".
Expand Down
13 changes: 10 additions & 3 deletions java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("[email protected]");
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
Expand Down

0 comments on commit 54a03b9

Please sign in to comment.