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

You Might Also Like
Comments
comments powered by Disqus