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
June 26th, 2008 at 1:17 pm
[...] Fox (from Alert Logic too) wrote a very good tutorial on how to run Selenium RC to execute unit tests in a team [...]
June 28th, 2008 at 9:47 am
Good site I “Stumbledupon” it today and gave it a stumble for you.. looking forward to seeing what else you have..later
July 16th, 2009 at 5:27 am
means we can run multiple RC one with each browser at a time? if so then it is great
October 19th, 2009 at 10:34 pm
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.
November 27th, 2009 at 7:04 am
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.