Zend framework is my PHP framework of choice, and I like Zend Studio 7 for coding. I try to unit test as much as possible. But when in comes to User Interface testing, plain phpUnit cannot do the job. Then Selenium comes in play. So with’s the big deal? It isn’t working out of the box…
My setup is a Mac running 10.6.1 Snow Leopard running al the latest patches. I’m using Firefox 3.5.3 and my IDE is Zend Studio 7.0.2.
Start with installing Selenium. You can install it as a pear package. On snow leopard apple included the latest versions of php (5.3) and PEAR, so installation is easy. But make sure you have the latest version of PEAR, just to be sure….
sudo pear upgrade
Now install the Selenium PEAR package. As of now the package is still in beta, so we cannot do a pear install Testing_Selenium. We need to specify the version.
pear install channel://pear.php.net/Testing_Selenium-0.4.3
SeleniumRC is now installed. But we need to fix it. Selenium has some hardcoded files with references to Firefox 2. We need to change it to work with Firefox 3.5. Why do we need to change this? When selenium fire’s up the browser in a test case it will install a plug-in for the remote control. But the plug-in will not load because there is a browser version restriction. It will not load in version higher than 2.
First go to the folder where the selenium-server.jar is installed
cd /usr/lib/php/data/Testing_Selenium
There should be an selenium-server.jar in that folder. You can check with a list statement
ls
Now extract the files we need to change
jar xf selenium-server.jar customProfileDirCUSTFFCHROME/extensions/readystate@openqa.org/install.rdf
jar xf selenium-server.jar customProfileDirCUSTFFCHROME/extensions/{538F0036-F358-4f84-A764-89FB437166B4}/install.rdf
jar xf selenium-server.jar customProfileDirCUSTFFCHROME/extensions/\{503A0CD4-EDC8-489b-853B-19E0BAA8F0A4\}/install.rdf
jar xf selenium-server.jar customProfileDirCUSTFF/extensions/readystate\@openqa.org/install.rdf
jar xf selenium-server.jar customProfileDirCUSTFF/extensions/\{538F0036-F358-4f84-A764-89FB437166B4\}/install.rdf
Now replace all references to firefox 2.0.0.* to 3.5.*
sudo find . -name "*.rdf" -exec sed -i -e 's/<em:maxVersion>2.0<\/em:maxVersion>/<em:maxVersion>3.5.*<\/em:maxVersion>/' '{}' \;
This line will search all extracted rdf files and replaces the version numbers.
Now put the modified files back into the jar
jar uf selenium-server.jar customProfileDirCUSTFFCHROME/extensions/readystate@openqa.org/install.rdf
jar uf selenium-server.jar customProfileDirCUSTFFCHROME/extensions/{538F0036-F358-4f84-A764-89FB437166B4}/install.rdf
jar uf selenium-server.jar customProfileDirCUSTFFCHROME/extensions/\{503A0CD4-EDC8-489b-853B-19E0BAA8F0A4\}/install.rdf
jar uf selenium-server.jar customProfileDirCUSTFF/extensions/readystate\@openqa.org/install.rdf
jar uf selenium-server.jar customProfileDirCUSTFF/extensions/\{538F0036-F358-4f84-A764-89FB437166B4\}/install.rdf
OK, that’s selenium taking care of. Now start the server . Be sure you’re in the /usr/lib/php/data/Testing_Selenium directory
java -jar ./selenium-server.jar
It should display some INFO messages saying org.mortbay.util.Container start
Selenium is running but need to fix a firefox bug before we can do an actual test. Without this fix Firefox will crash when selenium is trying to start it. We need to remove a sqlite file. As a precaution we don’t remove it but rename it
sudo mv /Applications/Firefox.app/Contents/MacOS/libsqlite3.dylib /Applications/Firefox.app/Contents/MacOS/_libsqlite3.dylib
I don’t experience bugs in firefox after this renaming. But if you think firefox is unstable, in normal use, after renaming this file rename it back to it’s original name after you’re done testing.
Now we have to generate some tests. You can build you own tests using firefox and the SeleniumIDE, but that is pretty straight forward. In the IDE you can copy the PHP code to your phpUnit tests.
For now I prepared a simple test see the code. Make sure you have phpUnit in your library path. In Zend Studio you can set this by altering the PHP include path in your project. If you don’t use Zend Studio you have to make sure you have phpUnit installed.
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
function setUp()
{
$this->setBrowser("*chrome");
$this->setBrowserUrl("http://ikregeer.nl/");
}
function testMyTestCase()
{
$this->open("http://ikregeer.nl/");
$this->click("link=Kamerstukken");
$this->waitForPageToLoad("30000");
$this->click("link=Antwoorden");
$this->waitForPageToLoad("30000");
}
}
?>
ikregeer.nl is a website I developed, and the tests just clicks on a few menu’s. The code is actualy copy-pasted from the SeleniumIDE, but with one minor but realy important fix.
For some reason the setBrowserUrl isn’t working correctly. To fix this you have to make sure the first call in the testMyTestCase function is $this->open(“http://yourwebsite.com/”); If you don’t do this the test will fail with a strange selenium proxy error. I have to check if this is due to the phpUnit implementation in Zend Studio or it’s a bug in the selenium-server.
You can now run your tests from Zend Studio and it will open a new firefox browser window and do the interface test. I made a small video to see it working
Tags: php, phpUnit, Selenium, Snow Leopard, zend studio
[...] There’s already a fantastic article about running Selenium tests in Zend Studio and on the MAC. Which could be deployed a testing framework like PHP Under Control. http://www.god-object.com/2009/10/16/user-interface-testing-on-mac-os-10-6-snow-leopard/ [...]