Tips & Tricks

Selenium/ WebDriver Tips and Tricks

WebDriver

How to increase 45 sec waiting period to bind locking port 7054?

During parallel execution of tests on FireFox you may encounter this error –
[sourcecode language=”java”]unable to bind to locking port 7054 within 45 seconds [/sourcecode]
[sourcecode language=”java”]FirefoxBinary fb = new FirefoxBinary();
fb.setTimeout(java.util.concurrent.TimeUnit.SECONDS.toMillis(90));

WebDriver driver = new FirefoxDriver(fb, null); // or pass in a FirefoxProfile you want instead of the default webdriver creates.[/sourcecode]





Selenium RC

How to handle Selenium is already running on port 4444 ?

This is one of the common problems that we face while automate webapps using Selenium RC(Selenium 1.0).
[sourcecode language=”java”]
Failed to start: SocketListener0@0.0.0.0:4444
Exception in thread "main" java.net.BindException: Selenium is already running on port 4444. Or some other service is.
at org.openqa.selenium.server.SeleniumServer.start(SeleniumServer.java:399)
at org.openqa.selenium.server.SeleniumServer.boot(SeleniumServer.java:234)
at org.openqa.selenium.server.SeleniumServer.main(SeleniumServer.java:198)
[/sourcecode]

You can very well kill this session and start freshly by using the command below in the browser.
http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

After you load this url the selenium sever that is running on port 4444 will be killed and you will notice a OKOK message in the browser.

How to start Selenium Server Automatically using Java code ?

Starting selenium sever manually in a Terminal(mac) or command prompt (windows) is a must do task before you kickoff your tests and we some times forget to do that and feel embarrassed (mainly in demos :P)

This is method of automatically starting and shutting down of selenium server through Java code comes handy when the selenium RC scripts needs to be integrated with CI like Jenkins or Travis.

Starting selenium server

[sourcecode language=”java”]
public static void startSeleniumServer(SeleniumServer server) throws Exception {

try {
ServerSocket serverSocket = new ServerSocket(RemoteControlConfiguration.DEFAULT_PORT);
serverSocket.close();

try {
RemoteControlConfiguration rcc = new RemoteControlConfiguration();
rcc.setPort(RemoteControlConfiguration.DEFAULT_PORT);
server = new SeleniumServer(false, rcc);

} catch (Exception e) {
System.err.println("Could not create Selenium Server because of: "
+ e.getMessage());
e.printStackTrace();
}
try {
server.start();
System.out.println("Server started");
} catch (Exception e) {
System.err.println("Could not start Selenium Server because of: "
+ e.getMessage());
e.printStackTrace();
}
} catch (BindException e) {
System.out.println("Selenium server already up, will reuse…");
}
}
[/sourcecode]

Stopping Selenium Server

[sourcecode language=”java”]
public static void stopSeleniumServer(SeleniumServer server, DefaultSelenium selenium){
selenium.stop();
if (server != null)
{
try
{
selenium.shutDownSeleniumServer();
server.stop();

server = null;
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
[/sourcecode]

Hope that helps!!

Leave a Comment