My blog has moved!
You should be automatically redirected in 5 seconds. If not, visit http://samueladesoga.wordpress.com 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;

@BeforeSuite
@Parameters( { "selenium.port" })
public void startSeleniumServer(String port) {

rcc = new RemoteControlConfiguration();
rcc.setPort(Integer.parseInt(port));

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

} catch (Exception e) {
throw new IllegalStateException("Can't start selenium server", e);
}
}

@AfterSuite(alwaysRun = true)
public void stopSeleniumServer() {
if (seleniumServer != null) {
seleniumServer.stop();
}
}

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

public static void stopSelenium(Selenium selenium) {
selenium.stop();
}

}
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

4 comments:

Knorr said...

Hello

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.

Thanks!

FK

leumascom said...

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

Anonymous said...

Hello,

I followed exactly what you have told and it throws exception

org.testng.TestNGException:
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(Parameters.java:143)
at org.testng.internal.Parameters.createParameters(Parameters.java:321)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:77)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:190)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:130)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:260)
at org.testng.SuiteRunner.run(SuiteRunner.java:223)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:954)
at org.testng.TestNG.runSuitesLocally(TestNG.java:883)
at org.testng.TestNG.run(TestNG.java:817)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)

What could be the problem? You help is appreciated

Thanks
Shashi

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