Created
June 30, 2011 15:25
-
-
Save freynaud/1056466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ebay.spine.selenium; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.openqa.selenium.WebDriverException; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.firefox.FirefoxProfile; | |
import org.openqa.selenium.io.TemporaryFilesystem; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
public class FirefoxeBayProfile { | |
private static DesiredCapabilities firefox = null; | |
static { | |
// Make a copy of the profile to use | |
File tempDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("userprofile", "copy"); | |
try { | |
File certdb = new File(tempDir, "cert8.db"); | |
writeResourceToFile(certdb, "eBayProfile/cert8.db", FirefoxeBayProfile.class); | |
// Delete the old compreg.dat file so that our new extension is | |
// registered | |
File compreg = new File(tempDir, "compreg.dat"); | |
if (compreg.exists()) { | |
if (!compreg.delete()) { | |
throw new WebDriverException("Cannot delete file from copy of profile "); | |
} | |
} | |
} catch (IOException e) { | |
throw new WebDriverException(e); | |
} | |
FirefoxProfile profile = new ProfileFromDirectory(tempDir); | |
DesiredCapabilities cap = DesiredCapabilities.firefox(); | |
cap.setCapability(FirefoxDriver.PROFILE, profile); | |
firefox = cap; | |
} | |
public static void writeResourceToFile(File file, String resourceName, Class<?> clasz) throws IOException { | |
InputStream inputStream = clasz.getResourceAsStream("/" + resourceName); | |
if (inputStream == null) { | |
System.err.println("Couldn't find resource on the class path: " + resourceName); | |
} | |
else { | |
try { | |
FileOutputStream outputStream = new FileOutputStream(file); | |
try { | |
int nread; | |
byte[] buffer = new byte[4096]; | |
while (0 < (nread = inputStream.read(buffer))) { | |
outputStream.write(buffer, 0, nread); | |
} | |
} finally { | |
outputStream.close(); | |
} | |
} finally { | |
inputStream.close(); | |
} | |
} | |
} | |
public static DesiredCapabilities eBayProfile() { | |
return firefox; | |
} | |
private static class ProfileFromDirectory extends FirefoxProfile { | |
public ProfileFromDirectory(File dir) { | |
super(dir); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment