Skip to content

Commit

Permalink
Allow tests using the SynthesizedFirefoxDriver to work from an IDE.
Browse files Browse the repository at this point in the history
We do this by creating a custom firefox profile.
  • Loading branch information
shs96c committed Jun 26, 2016
1 parent 612e2b0 commit 7759ff4
Showing 1 changed file with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

package org.openqa.selenium.testing.drivers;

import static org.junit.Assert.fail;
import static org.openqa.selenium.testing.DevMode.isInDevMode;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.BuckBuild;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
Expand All @@ -31,6 +34,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -89,7 +93,7 @@ private static Capabilities tweakCapabilities(Capabilities desiredCaps) {

private static FirefoxProfile createTemporaryProfile() {
if (!isInDevMode()) {
FirefoxProfile profile = new FirefoxProfile();
FirefoxProfile profile = new CustomProfile();

if (Boolean.getBoolean("webdriver.debug")) {
try {
Expand All @@ -105,7 +109,7 @@ private static FirefoxProfile createTemporaryProfile() {
}

try {
FirefoxProfile profile = new FirefoxProfile();
FirefoxProfile profile = new CustomProfile();
if (Boolean.getBoolean("webdriver.debug")) {

Firebug.addTo(profile);
Expand Down Expand Up @@ -148,5 +152,48 @@ private static FirefoxProfile copyExtensionTo(FirefoxProfile profile) throws IOE
profile.addExtension(ext);
return profile;
}

private static class CustomProfile extends FirefoxProfile {

private static Path prefs;

@Override
protected Reader onlyOverrideThisIfYouKnowWhatYouAreDoing() {
try {
return super.onlyOverrideThisIfYouKnowWhatYouAreDoing();
} catch (RuntimeException e) {
if (!DevMode.isInDevMode()) {
throw e;
}
}

prefs = actuallyGetPrefsPath();

try {
return Files.newBufferedReader(prefs);
} catch (IOException e) {
fail(Throwables.getStackTraceAsString(e));
throw new RuntimeException(e);
}
}

private Path actuallyGetPrefsPath() {
if (prefs != null) {
return prefs;
}

synchronized (CustomProfile.class) {
if (prefs == null) {
try {
prefs = new BuckBuild().of("//javascript/firefox-driver:webdriver_prefs").go();
} catch (IOException ioe) {
throw new WebDriverException(ioe);
}
}
}

return prefs;
}
}
}

0 comments on commit 7759ff4

Please sign in to comment.