Start the Selenium Server dynamically

>I have been working on a test suite in java using testng as the testing framework. I could not have suggested any other test frame work as it allows me to do a lot of configurable setups and teardowns. Yeah am not gonna promote testng anymore, lol.

I could have started using selenium server using a usual batch file that maven could call in one of its targets but i think doing it this way is cleaner.

public class SeleniumManager {
private SeleniumServer seleniumServer;

private static Selenium selenium;

private RemoteControlConfiguration rcc;

@Parameters( { “selenium.port” })
public void startSeleniumServer(String port) {

rcc = new RemoteControlConfiguration();

try {
seleniumServer = new SeleniumServer(false, rcc);
// seleniumServer= new SeleniumServer();

} catch (Exception e) {
throw new IllegalStateException(“Can’t start selenium server”, e);

@AfterSuite(alwaysRun = true)
public void stopSeleniumServer() {
if (seleniumServer != null) {

public static Selenium startSelenium(String host, String port, String browser, String url) {
selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);
return selenium;

public static void stopSelenium(Selenium selenium) {

As you can see i have a seleniumManager class which would dynamically start the selenium server before my testSuite starts and kill the SeleniumServer after the test suite is completed.

Please try this out and leave comments

This entry was posted in Software Testing and tagged , , , , . Bookmark the permalink.

4 Responses to Start the Selenium Server dynamically

  1. Knorr says:

    >HelloYou are using some annotations that are not valid for me. I'm using JUnit 4.5 and @BeforeSuite and @AfterSuite do not exist.From what I gathered, they are from TestNG. Is this correct?I'm stuck with JUnit 4.5 for corporate reasons and I'm trying to get a Selenium Manager working.Thanks!FK

  2. leumascom says:

    >Sorry, i think that was an omission by me. I am using TestNG.

  3. Anonymous says:

    >Hello, I followed exactly what you have told and it throws exceptionorg.testng.TestNGException: Parameter 'port' is required by @Configuration on method startSeleniumServerbut has not been marked @Optional or defined in C:\Documents and Settings\XPMUser\Local Settings\Temp\testng-eclipse-1172490322\testng-customsuite.xml at org.testng.internal.Parameters.createParameters( at org.testng.internal.Parameters.createParameters( at org.testng.internal.Parameters.createConfigurationParameters( at org.testng.internal.Invoker.invokeConfigurations( at org.testng.internal.Invoker.invokeConfigurations( at org.testng.SuiteRunner.privateRun( at at org.testng.SuiteRunnerWorker.runSuite( at at org.testng.TestNG.runSuitesSequentially( at org.testng.TestNG.runSuitesLocally( at at at org.testng.remote.RemoteTestNG.initAndRun( at org.testng.remote.RemoteTestNG.main( could be the problem? You help is appreciatedThanksShashi

  4. leumascom says:

    >The problem is that you have not passed the port number on which you want to run your test on the testng xml file. the @port variable is mandatory for your tests to run

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s