Selenium

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

19 Comments

  1. Posted March 23, 2006 at 12:12 am | Permalink

    Great tip – thanks!

  2. Posted March 23, 2006 at 6:46 pm | Permalink

    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! ; )

  3. Posted March 23, 2006 at 7:28 pm | Permalink

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

  4. Posted March 23, 2006 at 10:42 pm | Permalink

    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 … ; )

  5. Posted March 24, 2006 at 8:23 am | Permalink

    @Felix: No, Selenium does not have a JS interpreter, it simply uses the JS engine of the browser in which you run the tests.

  6. Posted March 24, 2006 at 3:39 pm | Permalink

    Ah I see … ^^. I should have RTFM I guess ; ). But that sounds interesting. I’ll definitly try it out now. Thx for your time …

  7. Belle fille
    Posted May 24, 2006 at 4:56 pm | Permalink

    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.

  8. Posted May 24, 2006 at 6:27 pm | Permalink

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

  9. JF
    Posted May 25, 2006 at 8:34 pm | Permalink

    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

  10. Belle fille
    Posted May 26, 2006 at 7:46 am | Permalink

    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.

  11. Posted May 26, 2006 at 9:50 am | Permalink

    @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!

  12. Posted May 26, 2006 at 10:08 am | Permalink

    @Belle fille: Hm, I still do not see what the problem is… Maybe you can explain it to me via IRC?

  13. Posted May 26, 2006 at 5:56 pm | Permalink

    Great work ! congratulations !
    This helper is really useful for me.

  14. Posted June 14, 2006 at 11:33 am | Permalink

    hi can we perform parameterization function with selenium?

  15. Posted June 14, 2006 at 12:55 pm | Permalink

    @Mayank: I don’t understand you, what do you mean with “parameterization function”?

  16. Posted September 15, 2006 at 1:42 am | Permalink

    If the power of selenese is not enough for you, i.e. if you want to use features of a real programming language in your tests, have a look at Selenium Remote Control. I’ve written a tutorial on how to use it: Functional unit testing for web applications. It is the least invasive and the most powerful way to do functional testing with just about any web application.

  17. Posted September 18, 2006 at 9:37 am | Permalink

    @Markus Bertheau: Thanks for your tutorial.

  18. ilana
    Posted October 22, 2006 at 4:55 pm | Permalink

    hi all

    i follow the instruction above but the url http://localhost/selenium/TestRunner.html?test=/pages/tests/testsuite-login is given me the js error
    this.gettesttable().rows in null or not an object its seems that php not running but another simple not ivnolved with selenium php script run ok
    can anyone help me
    i download selenuim 8.0.0 the last one
    and i have php 5
    thank in advanced

  19. Posted October 22, 2006 at 5:47 pm | Permalink

    @ilana: Hm, I don’t understand what your problem is. Can you reword it?


3 Trackbacks/Pingbacks

  1. […] cake baker » Selenium cake baker has posted a simple tutorial about using Selenium to test CakePHP apps. (tags: cakephp test unittest Selenium tutorial) […]

  2. […] The update process is simple (if you are new to Selenium, see also https://cakebaker.wordpress.com/2006/03/22/selenium/): […]

  3. […] https://cakebaker.wordpress.com/2006/03/22/selenium […]

%d bloggers like this: