Integrating PHPUnit with Selenium

Sebastian Bergmann » 04 October 2006 » in New Features » 8 Comments

Today I committed the initial work for integrating Selenium RC with PHPUnit.

Selenium RC "is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser.".

In this case it is PHPUnit that drives the execution of the web application UI tests, using PEAR's new Selenium package to communicate with the Selenium RC Server.
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
 
class WebTest extends PHPUnit_Extensions_SeleniumTestCase
{
    protected function setUp()
    {
        $this->setBrowser('*firefox');
        $this->setBrowserUrl('http://www.example.com/');
    }
 
    public function testTitle()
    {
        $this->open('http://www.example.com/');
        $this->assertTitleEquals('Example Web Page');
    }
}
?>
We can now use the TextUI test runner that comes with PHPUnit to run this test:
sb@wopr-mobile ~ % phpunit WebTest
PHPUnit 3.0.0 by Sebastian Bergmann.

.

Time: 00:05


OK (1 test)
Behind the scenes PHPUnit talks to Selenium RC, which in turn opens a new Mozilla Firefox instance. The browser is then instructed to load http://www.example.com/ and to return the title of the loaded page. This string is then passed back to PHPUnit to evaluate the assertion of the unit test.
Defined tags for this entry: , , ,

Trackback specific URI for this entry

8 Comments to "Integrating PHPUnit with Selenium"

Display comments as (Linear | Threaded)
  1. Patrick Lightbody
    09/10/2006 at 07:18 Permalink
    Very cool! As the original creator of Selenium RC (well, specifically Selenium Server) and someone who is just getting started on a PHP project, I'm glad to see this happen.

    But also as the founder of OpenQA, I have to ask: any chance you'd like to bring your work in to OpenQA/Selenium RC so it can sit along side the Java/.NET/perl/python/ruby add-ons?

    Reply

  2. Ichsan
    25/07/2007 at 09:48 Permalink
    I've tried your script, but when I run phpunit WebTest, the firefox did not raise up. Instead, I had to kill invisible firefox 2 program before it finally did. Have you try the script for firefox 2? Neither in windows nor linux, the test failed to be automatic. But for iexplore, it run smoothly.

    Reply

  3. Sebastian Bergmann
    25/07/2007 at 09:50 Permalink
    You currently need a nightly snapshot of Selenium RC for Firefox 2 support.

    Reply

  4. ivy
    22/12/2008 at 09:47 Permalink
    hi,
    i want to konw whether or not Integrating PHPUnit with Selenium can run under command line enviroment.
    if the answer is yes ,how can i get it
    thank you very much

    Reply

  5. Sebastian Bergmann
    22/12/2008 at 09:53 Permalink
    Sure.

    Reply

  6. ivy
    25/12/2008 at 15:24 Permalink
    as you mentioned in the document,seleniumtestcase can generate coverage report.
    I have tried as you direct in the document.
    1. Copy PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php into your webserver's document root directory.
    2. In your webserver's php.ini configuration file, configure PHPUnit/Extensions/SeleniumTestCase/prepend.php and PHPUnit/Extensions/SeleniumTestCase/append.php as the auto_prepend_file and auto_append_file, respectively.
    3. In your test case class that extends PHPUnit_Extensions_SeleniumTestCase, use

    protected $coverageScriptUrl = 'http://host/phpunit_coverage.php';

    to configure the URL for the phpunit_coverage.php script.

    but the coverage report i have has nothing in it.
    is there anything i ignore?

    Reply

  7. Sebastian Bergmann
    25/12/2008 at 16:41 Permalink
    Please do not use comments on this blog to ask questions / and or report bugs.

    Reply

  8. Simon Brüchner
    14/12/2009 at 12:01 Permalink
    Each time I call open() in a test, the browser will be opened again! How can I stay in the already opened browser?

    Reply

2 Trackbacks to "Integrating PHPUnit with Selenium"

  1. Sebastian Bergmann 21/08/2007 at 11:19
    Version 3.2 of PHPUnit is shaping up nicely. In this blog posting I want to highlight the improvements to SeleniumTestCase, PHPUnit's extension for Selenium. Probably the most important improvement to SeleniumTestCase is the fact that you can now run e
  2. Ogito blog 20/10/2007 at 18:02
    Today I learned of Selenium. While PHPUnit is suitable for testing if the back-end functions properly, Selenium can automate testing the front-end, meaning - actual output in the browser. Even better, Sebastian Bergmann seems to be working on integrati...

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Submitted comments will be subject to moderation before being displayed.