Skip to content

Commit

Permalink
Allow remote webdrivers that are firefox to install extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Sep 16, 2021
1 parent ca58c3b commit 0a5b2d8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
61 changes: 61 additions & 0 deletions java/src/org/openqa/selenium/firefox/AddHasExtensions.java
@@ -0,0 +1,61 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.firefox;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.AugmenterProvider;
import org.openqa.selenium.remote.ExecuteMethod;

import java.nio.file.Path;
import java.util.function.Predicate;

import static java.util.Collections.singletonMap;
import static org.openqa.selenium.remote.BrowserType.FIREFOX;

@AutoService(AugmenterProvider.class)
public class AddHasExtensions implements AugmenterProvider<HasExtensions> {
@Override
public Predicate<Capabilities> isApplicable() {
return caps -> FIREFOX.equals(caps.getBrowserName());
}

@Override
public Class<HasExtensions> getDescribedInterface() {
return HasExtensions.class;
}

@Override
public HasExtensions getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
return new HasExtensions() {
@Override
public String installExtension(Path path) {
return (String) executeMethod.execute(FirefoxDriver.ExtraCommands.INSTALL_EXTENSION,
ImmutableMap.of("path", path.toAbsolutePath().toString(),
"temporary", false));

}

@Override
public void uninstallExtension(String extensionId) {
executeMethod.execute(FirefoxDriver.ExtraCommands.UNINSTALL_EXTENSION, singletonMap("id", extensionId));
}
};
}
}
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/firefox/FirefoxDriver.java
Expand Up @@ -140,7 +140,7 @@ public static final class Capability {
public static final String MARIONETTE = "marionette";
}

private static class ExtraCommands {
static class ExtraCommands {
static String INSTALL_EXTENSION = "installExtension";
static String UNINSTALL_EXTENSION = "uninstallExtension";
static String FULL_PAGE_SCREENSHOT = "fullPageScreenshot";
Expand Down
17 changes: 17 additions & 0 deletions java/test/org/openqa/selenium/firefox/FirefoxDriverTest.java
Expand Up @@ -35,6 +35,7 @@
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebDriverBuilder;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.openqa.selenium.support.ui.ExpectedCondition;
Expand Down Expand Up @@ -519,6 +520,22 @@ public void testFirefoxCanNativelyClickOverlappingElements() {
+ "click in over (handled by body)");
}

@Test
public void shouldAllowRemoteWebDriversToBeAugmentedWithHasExtensions() {
WebDriver driver = RemoteWebDriver.builder()
.oneOf(new FirefoxOptions())
.address("http://localhost:4444/")
.build();

try {
assertThat(driver).isInstanceOf(HasExtensions.class);
fail(driver.getClass().getName());
} finally {
driver.quit();
}
}


private static class CustomFirefoxProfile extends FirefoxProfile {}

}

0 comments on commit 0a5b2d8

Please sign in to comment.