Skip to content

Commit 5f1a0f8

Browse files
authored
Use FileDetector to install Firefox addons if one is set on the driver (#9872)
1 parent 60b2cff commit 5f1a0f8

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

java/src/org/openqa/selenium/firefox/AddHasExtensions.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@
2020
import com.google.auto.service.AutoService;
2121
import com.google.common.collect.ImmutableMap;
2222
import org.openqa.selenium.Capabilities;
23+
import org.openqa.selenium.WebDriver;
24+
import org.openqa.selenium.WebDriverException;
25+
import org.openqa.selenium.WrapsDriver;
2326
import org.openqa.selenium.internal.Require;
27+
import org.openqa.selenium.io.Zip;
2428
import org.openqa.selenium.remote.AdditionalHttpCommands;
2529
import org.openqa.selenium.remote.AugmenterProvider;
2630
import org.openqa.selenium.remote.CommandInfo;
2731
import org.openqa.selenium.remote.ExecuteMethod;
32+
import org.openqa.selenium.remote.RemoteWebDriver;
2833
import org.openqa.selenium.remote.http.HttpMethod;
2934

35+
import java.io.File;
36+
import java.io.IOException;
3037
import java.nio.file.Path;
3138
import java.util.Map;
3239
import java.util.function.Predicate;
3340

3441
import static org.openqa.selenium.remote.Browser.FIREFOX;
42+
import static org.openqa.selenium.remote.DriverCommand.UPLOAD_FILE;
3543

3644
@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
3745
public class AddHasExtensions implements AugmenterProvider<HasExtensions>, AdditionalHttpCommands {
@@ -65,9 +73,23 @@ public HasExtensions getImplementation(Capabilities capabilities, ExecuteMethod
6573
public String installExtension(Path path) {
6674
Require.nonNull("Extension Path", path);
6775

68-
return (String) executeMethod.execute(
69-
INSTALL_EXTENSION,
70-
ImmutableMap.of("path", path.toAbsolutePath().toString(), "temporary", false));
76+
if (executeMethod instanceof WrapsDriver) {
77+
WebDriver wrapped = ((WrapsDriver) executeMethod).getWrappedDriver();
78+
if (wrapped instanceof RemoteWebDriver) {
79+
File localFile = ((RemoteWebDriver) wrapped).getFileDetector().getLocalFile(path.toString());
80+
if (localFile != null) {
81+
try {
82+
String zip = Zip.zip(localFile);
83+
String newPath = (String) executeMethod.execute(UPLOAD_FILE, ImmutableMap.of("file", zip));
84+
return installExtensionAtPath(newPath);
85+
} catch (IOException e) {
86+
throw new WebDriverException("Cannot upload " + localFile, e);
87+
}
88+
}
89+
}
90+
}
91+
92+
return installExtensionAtPath(path.toString());
7193
}
7294

7395
@Override
@@ -76,6 +98,12 @@ public void uninstallExtension(String extensionId) {
7698

7799
executeMethod.execute(UNINSTALL_EXTENSION, ImmutableMap.of("id", extensionId));
78100
}
101+
102+
private String installExtensionAtPath(String path) {
103+
return (String) executeMethod.execute(
104+
INSTALL_EXTENSION,
105+
ImmutableMap.of("path", path, "temporary", false));
106+
}
79107
};
80108
}
81109
}

java/src/org/openqa/selenium/remote/RemoteExecuteMethod.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
package org.openqa.selenium.remote;
1919

20+
import org.openqa.selenium.WebDriver;
21+
import org.openqa.selenium.WrapsDriver;
2022
import org.openqa.selenium.internal.Require;
2123

2224
import java.util.Map;
2325

24-
public class RemoteExecuteMethod implements ExecuteMethod {
26+
public class RemoteExecuteMethod implements ExecuteMethod, WrapsDriver {
2527
private final RemoteWebDriver driver;
2628

2729
public RemoteExecuteMethod(RemoteWebDriver driver) {
@@ -40,4 +42,8 @@ public Object execute(String commandName, Map<String, ?> parameters) {
4042

4143
return response.getValue();
4244
}
45+
46+
@Override public WebDriver getWrappedDriver() {
47+
return this.driver;
48+
}
4349
}

java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.openqa.selenium.remote.Command;
3838
import org.openqa.selenium.remote.CommandExecutor;
3939
import org.openqa.selenium.remote.DriverCommand;
40+
import org.openqa.selenium.remote.LocalFileDetector;
4041
import org.openqa.selenium.remote.RemoteWebDriver;
4142
import org.openqa.selenium.remote.SessionId;
4243
import org.openqa.selenium.remote.UnreachableBrowserException;
@@ -503,8 +504,12 @@ public void testFirefoxCanNativelyClickOverlappingElements() {
503504
public void canAddRemoveExtensions() {
504505
Path extension = InProject.locate("third_party/firebug/favourite_colour-1.1-an+fx.xpi");
505506

506-
((HasExtensions) driver).installExtension(extension);
507-
((HasExtensions) driver).uninstallExtension("favourite-colour-examples@mozilla.org");
507+
if (driver.getClass().equals(RemoteWebDriver.class)) {
508+
((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
509+
}
510+
511+
String id = ((HasExtensions) driver).installExtension(extension);
512+
((HasExtensions) driver).uninstallExtension(id);
508513
}
509514

510515
@Test

0 commit comments

Comments
 (0)