Testing php sites with The Play framework's Selenium testing.
I have been using
the Play framework recently - a Java MVC web application framework - and have been pretty impressed with it, especially
its Selenium testing.
I like the Play framework's Selenium test runner over Selenium on its own because of two things: firstly the tests are created programmatically, rather than writing HTML directly (you write a script whose output Play turns into Selenium HTML); secondly I like the way it displays all your tests together, lets you choose which ones to run - then displays the results on a single page, highlighting the errors.
I just joined a project working on a php-based website, which did not have automated testing. As I believe automated testing is a brilliant, even essential, practice I suggested we start doing so, and the team are up for the idea. Having been really impressed with Play's Selenium testing, I thought it would be good to be able to use that to test our php site. And with an hour and a half of playing around (and refreshing my memory of Apache httpd configuration directives) I managed to get it working. This is how I did it:
Create project & test
Firstly, created a new play project, eclipsified it (as I like working in Eclipse), and started it in test mode. It started on http://localhost:9000 as it its default.
I then opened the project in Eclipse, and modified the Application.test.html Selenium test to read the following:
#{selenium}
// Open the home page, and check that no error occured
open('http://127.0.0.1:9001/')
assertNotTitle('Application error')
#{/selenium}
(My php project is at http://127.0.0.1:9001/. And of course, 'Application error' won't actually be the title if there is an error, as that is a Play framework thing, but it'll do for now. I also deleted the unit and functional tests as I won't be using them.)
Fail to run...
My attempts to run this directly (loading http://localhost:9000/@tests into my browser & running the test) didn't work - it gave the following error:
Permission denied for <http://localhost:9000> to get property Location.href from <http://127.0.0.1:9001>.
which makes sense, as they are being accessed on two different domain names & ports. (Accessing play using the IP address didn't work either - the different ports still caused the error.)
Run tests from the same port
So, I then updated my Apache httpd server's configuration file to contain the following:
Listen 9001
NameVirtualHost 127.0.0.1:9001
<VirtualHost 127.0.0.1:9001>
DocumentRoot /Users/mbread/Sites/php_project
ServerName 127.0.0.1:9001
RewriteEngine On
RewriteRule ^/@tests(.*) http://localhost:9000/@tests$1 [P]
RewriteRule ^/public(.*) http://localhost:9000/public$1 [P]
</VirtualHost>
Succeed!
Now, going to http://127.0.0.1:9001/ loads the php project. Going to http://127.0.0.1:9001/@tests loads the Play testing framework (the "[P]" in the rewrite rules means it uses Apache's proxy, rather than redirecting the browser, so it still appears at the port 9001 URL). And selecting the test and clicking 'Start!' works! Brilliant!
Next, to see if I can hook Play's fixtures up to the same database as the php project! That should be pretty trivial... (I hope!)
Note: this is a shortened version of the process I went through. I originally had my php site at http://127.0.0.1/~mbread/php_project, but even getting the tests to load at http://127.0.0.1/~mbread/php_project/@tests didn't work, as it was looking for files in the /public directory, which stripped the /~mbread/php_project part out of the URL. I did think about doing something to check for a referrer containing @tests when there was a request for /public, but I thought the approach detailed above would be a better course of action.
There may well be a simpler and better way to achieve this that I haven't seen (and I'd be pleased to hear it - please comment!), and there may be other issues in this set-up that I haven't hit yet, but this is what I've got working for now. I hope it's useful to someone!