In the last days I experimented with Selenium (thanks to knoebi for the hint). What is Selenium? Here the description from the website:
Selenium is a test tool for web applications. Selenium tests run directly in a browser, just as real users do.
I have to admit that I like Selenium, and so I will describe in this post how to use it with CakePHP.
First the installation:
- Download Selenium from http://www.openqa.org/selenium-core/download.action (I recommend to download the newest version)
- If you downloaded version 0.6 of Selenium: Copy the “selenium” folder from the zip to your “app/webroot” folder
- If you downloaded version 0.7.0 of Selenium: Copy the content of the “core” folder from the zip to “app/webroot/selenium”
- Download the code snippets from the download section and put the files in the correct folders
So, with that we are ready to write our first, very simple Selenium test case.
Test cases are organized in test suites, so we create first such a test suite in “app/views/pages/tests/”. You may wonder why we create the test suite in a subfolder of “views”. The reason is simple: Selenium tests are written in plain html. Here is our test suite:
<?php
// testsuite-login.thtml
$selenium->suiteTitle('My first testsuite');
$selenium->addTestCase('Login', 'test_login');
?>
Let us write our test case. We want to test if we can login into our application and if we are redirected to the correct page. The code should be self-explanatory (if not, please write a comment or have a look in the Selenium reference)
<?php
// test_login.thtml
$selenium->caseTitle('Login');
$selenium->open('/login');
$selenium->type('name=data[User][username]', 'dho');
$selenium->type('name=data[User][password]', 'test');
$selenium->clickAndWait('submit');
$selenium->assertLocation('/users/dho');
$selenium->assertTextPresent('Daniel Hofstetter');
?>
That’s it, we are ready to run our test suite. For doing that, we have to switch to our browser and type http://localhost/selenium/TestRunner.html?test=/pages/tests/testsuite-login (if you have several test suites and you do not want to remember such long urls you can edit “app/webroot/selenium/index.html” and add the urls there. The only url you have to remember in this case is http://localhost/selenium). On the right side we have to click on the “All” button and the test starts. Yeah, everything is green, at least here ;-)
Update (2006-05-26): Download link for Selenium modified, added instructions for Selenium 0.7.
Update (2006-11-06): For Selenium 0.8 you can find instructions at http://cakebaker.42dh.com/2006/10/04/new-selenium-helper/.

Great tip – thanks!
Nice! But as far as I know does SimpleTest allow you to do exactly the same with their WebTestsuite class. In fact I’ve played with it before and it looks good as well.
But thx for the code and sharing! ; )
@Felix: Well, with the WebTestSuite of SimpleTest you can do the same as with Selenium as long as your application you want to test does not use JavaScript. SimpleTest does not support JavaScript.
Selenium has a JS interpretor??? That sounds incredible. If it’s any good I’ll definitly have a look at the project. I mean I’d be fun to just take the JS interpreting code and play around with it … ; )
[...] cake baker » Selenium cake baker has posted a simple tutorial about using Selenium to test CakePHP apps. (tags: cakephp test unittest Selenium tutorial) [...]
@Felix: No, Selenium does not have a JS interpreter, it simply uses the JS engine of the browser in which you run the tests.
Ah I see … ^^. I should have RTFM I guess ; ). But that sounds interesting. I’ll definitly try it out now. Thx for your time …
[...] The update process is simple (if you are new to Selenium, see also http://cakebaker.wordpress.com/2006/03/22/selenium/): [...]
Good afternoon everyone.
I don’t understand one thing in this article. First I should “Copy the “selenium” folder from the zip to your “app/webroot” folder”, however, afterwards, the url for testing is http://localhost/selenium/TestRunner.html?test..
, that means the directory “selenium” is placed directly under web server root.
Besides, I tried to access another directory like this:
http://127.0.0.1/Larbaletier/Gauthier/app/webroot/selenium/core/TestRunner.html?test=/Larbaletier/Gauthier/app/views/pages/tests/testsuite-login
But I was then told to supply a Cakephp controller.
Would anyone please explain what the problem is?
Thanks very much.
@Belle fille: Hm, I am not sure if I understand you. I assume in the article that your document root points to app/webroot. So maybe you have to change the url a little bit. If your application runs under http://localhost/yourapp, you have to call selenium with http://localhost/yourapp/selenium...
Hope that helps.
Hi. Thanks for the heads up on selenium, will use it.
I’m following your instructions and I believe the selenium team has updated (May 14) the location of the TestRunner.html to the selenium/core directory that’s causing some hiccups. I’m getting the following errors:
Notice: Undefined variable: selenium in C:\www\app\views\pages\tests\testsuite-login.thtml on line 3
Fatal error: Call to a member function suiteTitle() on a non-object in C:\www\app\views\pages\tests\testsuite-login.thtml on line 3
In my opinion, localhost/selenium in the tutorial means selenium is under htdocs of Apache server.
On my Apache server that’s
localhost/Larbaletier/Gauthier/app where app is
the directory app of cakephp.
@JF: With the 0.7 release of Selenium you simply have to copy the content of the core folder to app/webroot/selenium. I will update the article. Thanks for the hint!
@Belle fille: Hm, I still do not see what the problem is… Maybe you can explain it to me via IRC?
Great work ! congratulations !
This helper is really useful for me.
hi can we perform parameterization function with selenium?
@Mayank: I don’t understand you, what do you mean with “parameterization function”?
[...] http://cakebaker.wordpress.com/2006/03/22/selenium [...]
Are you going to update the docs for Selenium 0.8 soon? I was using the sampe for ver 0.8 and test failed saying that it was incomplete.
@e12win: There is a new Selenium helper for Selenium 0.8: http://cakebaker.42dh.com/2006/10/04/new-selenium-helper/
HTH
Hi Everyone,
Parameterization is possible with Selenium IDE ?
Regards,
Mallik
@Mallik: What do you mean with “parameterization”?
How to do parameterization with selenium IDE
@Rohit: What do you mean with “parameterization with selenium IDE”?
Parameterization is passing values to a function. so I can call the the function where ever it required with different set of values…
@Raju: Hm, I still don’t understand what exactly you mean with parameterization in the context of Selenium IDE. Can you give an example of what you try accomplish?