Skip to content

Commit

Permalink
[java] Restoring ability to add chrome extension from base64-encoded …
Browse files Browse the repository at this point in the history
…string. Fixes #5183
  • Loading branch information
barancev committed Dec 10, 2017
1 parent 73aa9e5 commit a69c508
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
21 changes: 12 additions & 9 deletions java/client/src/org/openqa/selenium/chrome/ChromeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Stream;

/**
* Class to manage options specific to {@link ChromeDriver}.
Expand Down Expand Up @@ -287,15 +288,17 @@ public Map<String, Object> asMap() {

options.put(
"extensions",
extensionFiles.stream()
.map(file -> {
try {
return Base64.getEncoder().encodeToString(Files.toByteArray(file));
} catch (IOException e) {
throw new SessionNotCreatedException(e.getMessage(), e);
}
})
.collect(ImmutableList.toImmutableList()));
Stream.concat(
extensionFiles.stream()
.map(file -> {
try {
return Base64.getEncoder().encodeToString(Files.toByteArray(file));
} catch (IOException e) {
throw new SessionNotCreatedException(e.getMessage(), e);
}
}),
extensions.stream()
).collect(ImmutableList.toImmutableList()));

toReturn.put(CAPABILITY, options);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,30 @@
package org.openqa.selenium.chrome;

import static org.junit.Assert.assertEquals;
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;

import com.google.common.io.Files;

import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.testing.InProject;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NeedsLocalEnvironment;

import java.io.File;
import java.io.IOException;
import java.util.Base64;

/**
* Functional tests for {@link ChromeOptions}.
*/
public class ChromeOptionsFunctionalTest extends JUnit4TestBase {

private static final String EXT_PATH = "third_party/chrome_ext/backspace.crx";

private ChromeDriver driver = null;

@After
Expand Down Expand Up @@ -59,4 +73,38 @@ public void optionsStayEqualAfterSerialization() throws Exception {
assertEquals("empty chrome options after one is .toJson() should be equal",
options1, options2);
}

@NeedsLocalEnvironment
@Test
public void canAddExtensionFromFile() {
ChromeOptions options = new ChromeOptions();
options.addExtensions(InProject.locate(EXT_PATH).toFile());
driver = new ChromeDriver(options);

driver.get(pages.clicksPage);

driver.findElement(By.id("normal")).click();
new WebDriverWait(driver, 10).until(titleIs("XHTML Test Page"));

driver.findElement(By.tagName("body")).sendKeys(Keys.BACK_SPACE);
new WebDriverWait(driver, 10).until(titleIs("clicks"));
}

@NeedsLocalEnvironment
@Test
public void canAddExtensionFromStringEncodedInBase64() throws IOException {
ChromeOptions options = new ChromeOptions();
options.addEncodedExtensions(Base64.getEncoder().encodeToString(
Files.toByteArray(InProject.locate(EXT_PATH).toFile())));
driver = new ChromeDriver(options);

driver.get(pages.clicksPage);

driver.findElement(By.id("normal")).click();
new WebDriverWait(driver, 10).until(titleIs("XHTML Test Page"));

driver.findElement(By.tagName("body")).sendKeys(Keys.BACK_SPACE);
new WebDriverWait(driver, 10).until(titleIs("clicks"));
}

}
Binary file added third_party/chrome_ext/backspace.crx
Binary file not shown.

0 comments on commit a69c508

Please sign in to comment.