2

How would you run the Selenium process (thread) from a Java process so I don't have to start Selenium by hand?

2 Answers 2

9

The server:

import org.openqa.selenium.server.SeleniumServer;
public class SeleniumServerControl {
  private static final SeleniumServerControl instance = new SeleniumServerControl();
  public static SeleniumServerControl getInstance() {
    return instance;
  }
  private SeleniumServer server = null;
  protected SeleniumServerControl() {
  }
  public void startSeleniumServer() {
    if (server == null) {
      try {
        server = new SeleniumServer(SeleniumServer.DEFAULT_PORT);
        System.out.println(" selenium server " + server.toString());
      } catch (Exception e) {
        System.err.println("Could not create Selenium Server because of: "
            + e.getMessage());
        e.printStackTrace();
      }
    }
    try {
      server.start();
    } catch (Exception e) {
      System.err.println("Could not start Selenium Server because of: "
          + e.getMessage());
      e.printStackTrace();
    }
  }
  public void stopSeleniumServer() {
    if (server != null) {
      try {
        server.stop();
        server = null;
      } catch (Exception e) {
        System.err.println("Could not stop Selenium Server because of: "
            + e.getMessage());
        e.printStackTrace();
      }
    }
  }
}

The client:

browser = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com");
browser.start();
Sign up to request clarification or add additional context in comments.

Comments

3

Also there are some additional settings you can use:

    RemoteControlConfiguration settings = new RemoteControlConfiguration();
    File f = new File("/home/user/.mozilla/firefox/default");
    settings.setFirefoxProfileTemplate(f);
    settings.setReuseBrowserSessions(true);
    settings.setSingleWindow(true);
    if (this.ServerWorks == false)
    {
        try
        {
            server = new SeleniumServer(settings);
            server.start();
            this.ServerWorks = true;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.