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

Advertisements
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(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 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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s