Created
September 13, 2011 03:39
-
-
Save freynaud/1213073 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 org.openqa.selenium.safari.org.openqa.selenium.safari; | |
import org.openqa.selenium.WebDriverException; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.Map; | |
import java.util.List; | |
public class SafariBinary { | |
private static final String SAFARI_BINARY_LOCATION = "/Applications/Safari.app/Contents/MacOS/Safari"; | |
//private static final String SAFARI_BINARY_LOCATION = "/Applications/TextEdit.app/Contents/MacOS/TextEdit"; | |
//private static final String SAFARI_BINARY_LOCATION = "/Applications/firefox.app/Contents/MacOS/firefox-bin"; | |
private static final int MAX_LOAD_WAIT = 10000; | |
private ProcessWrapper safariProcess; | |
private ProcessBuilder processBuilder; | |
interface ProcessListener { | |
void onProcessFinished(); | |
} | |
private class ProcessWrapper implements Runnable { | |
private final Process process; | |
private final List<ProcessListener> listeners; | |
ProcessWrapper(Process process) { | |
this.process = process; | |
this.listeners = new ArrayList<ProcessListener>(); | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public void run() { | |
quit(); | |
} | |
}); | |
new Thread(this).start(); | |
} | |
void addProcessListener(ProcessListener listener) { | |
listeners.add(listener); | |
} | |
void quit() { | |
process.destroy(); | |
} | |
public void run() { | |
int in = 0; | |
while (in != -1) { | |
try { | |
in = process.getInputStream().read(); | |
System.out.print((char) in); | |
} catch (IOException e) { | |
System.err.println("Safari terminated. (" + e + ")"); | |
break; | |
} | |
} | |
for (ProcessListener listener : listeners) { | |
listener.onProcessFinished(); | |
} | |
} | |
} | |
public SafariBinary() { | |
processBuilder = new ProcessBuilder(SAFARI_BINARY_LOCATION).redirectErrorStream(true); | |
} | |
private void setupBuilderEnvironments() throws IOException { | |
Map<String, String> envs = new HashMap<String, String>(); | |
envs.put("DYLD_INSERT_LIBRARIES","/Users/freynaud/Library/Developer/Xcode/DerivedData/SafariDriver-dxvlxtdwivgyildircyshujpzooy/Build/Products/Debug/BundleInjector.dylib"); | |
envs.put("XCInjectBundle", "/Users/freynaud/Library/Developer/Xcode/DerivedData/SafariBundle-bhgwntykayxjgagkkuusqppavwce/Build/Products/Debug/SafariBundle.bundle"); | |
envs.put("XCInjectBundleInto", SAFARI_BINARY_LOCATION); | |
processBuilder.environment().putAll(envs); | |
} | |
public void startSafari() { | |
if (safariProcess == null) { | |
try { | |
setupBuilderEnvironments(); | |
safariProcess = new ProcessWrapper(processBuilder.start()); | |
safariProcess.addProcessListener(new ProcessListener() { | |
public void onProcessFinished() { | |
safariProcess = null; | |
} | |
}); | |
} catch (Exception e) { | |
quit(); | |
throw new WebDriverException("Failed to launch Safari.", e); | |
} | |
} else { | |
throw new WebDriverException("Safari is already running."); | |
} | |
} | |
public boolean isRunning() { | |
return safariProcess == null; | |
} | |
public void quit() { | |
if (safariProcess != null) { | |
safariProcess.quit(); | |
safariProcess = null; | |
} | |
} | |
public static void main(String[] args) { | |
SafariBinary safariBinary = new SafariBinary(); | |
safariBinary.startSafari(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment