Using Selenium RC With Multiple Users

So you may be testing with selenium already, you may even be using selenium RC to automate your testing and integrate it with your other unit tests, and you may already be doing so in a shared development environment, but if you aren’t using selenium RC with multiple users, you may not know that you can start the RC server on any port you wish, allowing multiple clients to connect at the same time.

To start the selenium RC server, you would typically run this command:

java -jar selenium-server.jar

Which will start it on the default port of 4444. If you want to start it on another port, simply add -port <portnumber> to the command like this:

java -jar selenium-server.jar -port 1234

This command would start the server on port 1234.

Great, now I can start the server on multiple ports, so what?

So here is the good news for those of us in shared development environments. Say you have a windows virtual machine running selenium with your test browser installed. Things get complicated when multiple clients are testing at the same time. Tests can fail unexpectedly, developers begin fighting for time on the test server, and all hell breaks loose. By running multiple servers on different ports, we can avoid the third world war. Everyone can connect at the same time to their personal server, and we have peace and harmony in our testing environment.

Wow, setting up a different port everytime must suck.

Not really. There are many ways that you can automate the process so that it’s completely transparent to your development team. A lot will depend on your environment, but in our case, we created a .bat file that starts the servers in our virtual machine and placed it in the start-up folder.

start "Selenium 4444" /min java.exe -jar selenium-server.jar -port 4444
start "Selenium 4445" /min java.exe -jar selenium-server.jar -port 4445
start "Selenium 4446" /min java.exe -jar selenium-server.jar -port 4446
start "Selenium 4447" /min java.exe -jar selenium-server.jar -port 4447

HTML code generated by vim-color-improved v.0.4.0.Download this code: selenium.bat

With this in place, we now have 4 selenium RC servers running at all times. If one or more crashes for any reason, running the batch file again will start any that are missing. The selenium RC server will not start if its port is in use, so only the ones missing will run, and you’ll have them all up and running again.

Ok, what about the client side? I don’t want to have to edit another file in my development environment.

On the client side, we are using phpunit with Testing_Selenium. Of course all of our selenium test classes extend a parent class, so it was easy enough for us to create some dynamic logic there to decide which port to use.

<?php
class SeleniumTestHelper
{
    public function setup()
    {
        $ports = array('user1' => 4444,
                       'user2' => 4445,
                       'user3' => 4446,
                       'user4' => 4447);
        $user = someFunctionThatDeterminesUser();
        define('BROWSER', '*firefox');
        define('URL', 'http://www.example.com');
        define('SERVER', '192.168.0.2');
        define('PORT', $ports[$user]);
        $this->selenium = new Testing_Selenium(BROWSER, URL, SERVER, PORT);
    }
}
?>

HTML code generated by vim-color-improved v.0.4.0.Download this code: SeleniumTestHelper.php

9 Responses to “Using Selenium RC With Multiple Users”

  1. João Prado Maia’s Weblog » Using Selenium RC with multiple users Says:

    [...] Fox (from Alert Logic too) wrote a very good tutorial on how to run Selenium RC to execute unit tests in a team [...]

  2. Php Class Tutorial Says:

    Good site I “Stumbledupon” it today and gave it a stumble for you.. looking forward to seeing what else you have..later

  3. anil Says:

    means we can run multiple RC one with each browser at a time? if so then it is great

  4. Young Says:

    Works well with Firefox. Does not seem to work with Internet Explorer.

    Selenium tests always try to re-use the existing IE window (instead of launching a new IE window), causing tests to collide in the same IE window.

  5. holminator Says:

    I am using Selenium RC at work. Up to 4 clients may run tests on a single server including IE. The reason for the IE browsers to interfere is:

    “For firefox, user can create a user profiles, and specify a profile for a particular test, therefore, multiple tests can run concurrently on the same machine. For IE, Selenium RC modify registry settings and configures the LAN connection settings directly, therefore only a single test can be run on a single machine.”
    (from http://khaidoan.wikidot.com/selenium-rc)

    My solution:
    I created 4 windows users each running a selenium rc server (on different ports of course) – so the IE browsers cannot interact with each other. BUT: The 4 users must all be logged on otherwise the IE does not start (they may be inactive or disconnected).
    But be sure to have enough CPU power – IE is 5-10 (!) times slower than firefox, which may cause waitFor-commands to time out.

  6. Raghu Says:

    hi all,
    I am learning the Selenium, but i need the information regarding Selenium, How can i connect the perl client driver with selenium and how can i configure with IDE.
    I was searching in the net but i didn’t get the much information.

    With Regards,
    V Raghu.

  7. Kathy Says:

    I’m running into a strange problem with multiple uses on the same server. I log into one (my development system), and launch the selenium server on port 4444. Then I log into the system as a different user (QA System) from a different computer and launch the selenium server on port 4445. When I try to run a script from the QA System, the script runs, but the browser displays on the development system. All the user shows is the console and status of the test. If I shut down the server on the development system, I will get an error when I try to run a script from the QA system saying the server session should not be null (full text below). Has anyone run into this? (BTW, I’m running Firefox with separate profiles)

    Thanks,

    Kathy

    09:58:14.852 INFO – Java: Sun Microsystems Inc. 19.1-b02
    09:58:14.852 INFO – OS: Windows 2003 5.2 x86
    09:58:14.868 INFO – v1.0.1 [2696], with Core v@VERSION@ [@REVISION@]
    09:58:14.993 INFO – Version Jetty/5.1.x
    09:58:14.993 INFO – Started HttpContext[/selenium-server/driver,/selenium-server
    /driver]
    09:58:14.993 INFO – Started HttpContext[/selenium-server,/selenium-server]
    09:58:14.993 INFO – Started HttpContext[/,/]
    09:58:15.024 INFO – Started SocketListener on 0.0.0.0:4445
    09:58:15.024 INFO – Started org.mortbay.jetty.Server@1960f05
    09:58:26.836 INFO – Checking Resource aliases
    09:58:26.852 INFO – Command request: deleteAllVisibleCookies[, ] on session null

    09:58:26.852 ERROR – Exception running ‘deleteAllVisibleCookies ‘command on sess
    ion null
    java.lang.NullPointerException: sessionId should not be null; has this session b
    een started yet?
    at org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(Fram
    eGroupCommandQueueSet.java:214)
    at org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(Selen
    iumCoreCommand.java:34)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.doCommand(Se
    leniumDriverResourceHandler.java:549)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleComman
    dRequest(SeleniumDriverResourceHandler.java:364)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(Selen
    iumDriverResourceHandler.java:125)
    at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
    at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
    at org.mortbay.http.HttpServer.service(HttpServer.java:909)
    at org.mortbay.http.HttpConnection.service(HttpConnection.java:820)
    at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:986)
    at org.mortbay.http.HttpConnection.handle(HttpConnection.java:837)
    at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:
    245)
    at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
    at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
    09:58:26.852 INFO – Got result: ERROR Server Exception: sessionId should not be
    null; has this session been started yet? on session null
    09:58:26.868 INFO – Command request: getLocation[, ] on session null
    09:58:26.868 ERROR – Exception running ‘getLocation ‘command on session null
    java.lang.NullPointerException: sessionId should not be null; has this session b
    een started yet?
    at org.openqa.selenium.server.FrameGroupCommandQueueSet.getQueueSet(Fram
    eGroupCommandQueueSet.java:214)
    at org.openqa.selenium.server.commands.SeleniumCoreCommand.execute(Selen
    iumCoreCommand.java:34)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.doCommand(Se
    leniumDriverResourceHandler.java:549)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleComman
    dRequest(SeleniumDriverResourceHandler.java:364)
    at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(Selen
    iumDriverResourceHandler.java:125)
    at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
    at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
    at org.mortbay.http.HttpServer.service(HttpServer.java:909)
    at org.mortbay.http.HttpConnection.service(HttpConnection.java:820)
    at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:986)
    at org.mortbay.http.HttpConnection.handle(HttpConnection.java:837)
    at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:
    245)
    at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
    at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
    09:58:26.868 INFO – Got result: ERROR Server Exception: sessionId should not be
    null; has this session been started yet? on session null

  8. Preeti Says:

    Hello,
    I am unable to run .. and got below message.Please help.

    C:\SELENIUM>java -jar selenium-server-standalone-2.5.0.jar
    Sep 1, 2011 12:21:26 PM org.openqa.grid.selenium.GridLauncher main
    INFO: Launching a standalone server
    12:21:27.693 INFO – Java: Sun Microsystems Inc. 20.0-b11
    12:21:27.694 INFO – OS: Windows 7 6.1 x86
    12:21:27.701 INFO – v2.5.0, with Core v2.5.0. Built from revision 13516
    12:21:27.797 INFO – RemoteWebDriver instances should connect to: http://127.0.0.
    1:4444/wd/hub
    12:21:27.798 INFO – Version Jetty/5.1.x
    12:21:27.799 INFO – Started HttpContext[/selenium-server/driver,/selenium-server
    /driver]
    12:21:27.799 INFO – Started HttpContext[/selenium-server,/selenium-server]
    12:21:27.800 INFO – Started HttpContext[/,/]
    12:21:27.819 INFO – Started org.openqa.jetty.jetty.servlet.ServletHandler@f9f9d8

    12:21:27.820 INFO – Started HttpContext[/wd,/wd]
    12:21:27.821 WARN – Failed to start: SocketListener0@0.0.0.0:4444
    Exception in thread “main” java.net.BindException: Selenium is already running o
    n port 4444. Or some other service is.
    at org.openqa.selenium.server.SeleniumServer.start(SeleniumServer.java:4
    10)
    at org.openqa.selenium.server.SeleniumServer.boot(SeleniumServer.java:23
    6)
    at org.openqa.selenium.server.SeleniumServer.main(SeleniumServer.java:20
    1)
    at org.openqa.grid.selenium.GridLauncher.main(GridLauncher.java:40)

  9. ajeyaraj Says:

    Is it a must to run multiple servers in order to run multiple tests. I have a single RC server running on port 4444, and I run two tests both pointing to same server, and the tests seem to run fine in parallel. Am I missing something?