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.3.2.
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.3.2.
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