Skip to content

Commit

Permalink
SimonStewart: Moving closer to allowing multiple instances of firefox…
Browse files Browse the repository at this point in the history
… to start up at the same time. Applying patch from Mirko Nasato

r4918
  • Loading branch information
shs96c committed Mar 7, 2008
1 parent 28c5666 commit 503c6b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions CREDITS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Carlos Villela
Michael Tamm
James Cooper
Malcolm Rowe
Mirko Nasato
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
* This allows multiple instances of firefox to be started.
*/
public class FirefoxDriver implements WebDriver, FindsById, FindsByLinkText, FindsByXPath {

public static final String DEFAULT_PROFILE = "WebDriver";
public static final int DEFAULT_PORT = 7055;

private final ExtensionConnection extension;
protected Context context;

Expand All @@ -50,9 +54,13 @@ public FirefoxDriver() {
}

public FirefoxDriver(String profileName) {
this(profileName, DEFAULT_PORT);
}

public FirefoxDriver(String profileName, int port) {
prepareEnvironment();

extension = connectTo(profileName, "localhost", 7055);
extension = connectTo(profileName, "localhost", port);

if (!extension.isConnected()) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,32 @@ public static void main(String[] args) {

if (args.length == 0)
launcher.createBaseWebDriverProfile();
else
else if (args.length == 1)
launcher.createBaseWebDriverProfile(args[0]);
else
launcher.createBaseWebDriverProfile(args[0], Integer.parseInt(args[1]));
}

public void createBaseWebDriverProfile() {
createBaseWebDriverProfile("WebDriver");
createBaseWebDriverProfile(FirefoxDriver.DEFAULT_PROFILE);
}

public void createBaseWebDriverProfile(String profileName) {
createBaseWebDriverProfile(profileName, FirefoxDriver.DEFAULT_PORT);
}

public void createBaseWebDriverProfile(String profileName, int port) {
// If there's a browser already running
connectAndKill();
connectAndKill(port);

File firefox = locateFirefoxBinary(null);

System.out.println(MessageFormat.format("Creating {0}", profileName));
Process process;
try {
process = new ProcessBuilder(firefox.getAbsolutePath(), "-CreateProfile", profileName).redirectErrorStream(true).start();
ProcessBuilder builder = new ProcessBuilder(firefox.getAbsolutePath(), "-CreateProfile", profileName).redirectErrorStream(true);
builder.environment().put("MOZ_NO_REMOTE", "1");
process = builder.start();

process.waitFor();

Expand All @@ -62,22 +70,22 @@ public void createBaseWebDriverProfile(String profileName) {
installExtensionInto(extensionsDir);

System.out.println("Updating user preferences with common, useful settings");
updateUserPrefsFor(extensionsDir);
updateUserPrefsFor(extensionsDir, port);

System.out.println("Deleting existing extensions cache (if it already exists)");
deleteExtensionsCacheIfItExists(extensionsDir);

System.out.println("Firefox should now start and quit");

startFirefox(firefox, profileName);
connectAndWaitForConnectionToDrop();
connectAndKill();
connectAndWaitForConnectionToDrop(port);
connectAndKill(port);
}

private void connectAndWaitForConnectionToDrop() {
private void connectAndWaitForConnectionToDrop(int port) {
while (true) {
try {
new RunningInstanceConnection("localhost", 7055, 1000);
new RunningInstanceConnection("localhost", port, 1000);
sleep(2000);
} catch (ConnectException e) {
// Expected
Expand All @@ -96,9 +104,9 @@ private void sleep(long timeInMillis) {
}
}

protected void connectAndKill() {
protected void connectAndKill(int port) {
try {
ExtensionConnection connection = new RunningInstanceConnection("localhost", 7055, 5000);
ExtensionConnection connection = new RunningInstanceConnection("localhost", port, 5000);
connection.sendMessageAndWaitForResponse(RuntimeException.class, new Command(null, "quit"));
} catch (NullPointerException e) {
// Expected. Swallow it.
Expand All @@ -119,7 +127,7 @@ private void startFirefox(File firefox, String profileName) {
}
}

protected void updateUserPrefsFor(File extensionsDir) {
protected void updateUserPrefsFor(File extensionsDir, int port) {
File userPrefs = new File(extensionsDir, "user.js");

Map<String, String> prefs = new HashMap<String, String>();
Expand Down Expand Up @@ -155,7 +163,7 @@ protected void updateUserPrefsFor(File extensionsDir) {
prefs.put("signon.rememberSignons", "false");

// Which port should we listen on?
prefs.put("webdriver_firefox_port", "7055");
prefs.put("webdriver_firefox_port", Integer.toString(port));

// Settings to facilitate debugging the driver
prefs.put("javascript.options.showInConsole", "true"); // Logs errors in chrome files to the Error Console.
Expand Down Expand Up @@ -257,7 +265,7 @@ public Process startProfile(String profileName, File firefoxBinary) {
profileName = profileName == null ? System.getProperty("webdriver.firefox.profile") : profileName;

if (profileName == null) {
profileName = "WebDriver";
profileName = FirefoxDriver.DEFAULT_PROFILE;
}

try {
Expand Down

0 comments on commit 503c6b1

Please sign in to comment.