My blog has moved!
You should be automatically redirected in 5 seconds. If not, visit and update your bookmarks.

Monday, 16 November 2009

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


Knorr said...


You 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.



leumascom said...

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

Anonymous said...


I followed exactly what you have told and it throws exception

Parameter 'port' is required by @Configuration on method startSeleniumServer
but 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 org.testng.SuiteRunnerWorker.runSuite(
at org.testng.TestNG.runSuitesSequentially(
at org.testng.TestNG.runSuitesLocally(
at org.testng.remote.RemoteTestNG.initAndRun(
at org.testng.remote.RemoteTestNG.main(

What could be the problem? You help is appreciated


leumascom said...

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