log

taiar

Acceptance Tests on Codeigniter With PHPUnit and Selenium

In my first Codeigniter testing article, we take a look in how should we make unit testing with PHPUnit. PHPUnit has an extension that integrates with Selenium RC, a Java written server able to interact with the browser and run tests like an user would do. My point here, like in the first article is not to explore the testing issues but explain how to integrate Codeigniter + PHPUnit + Selenium so we can do acceptance tests. For more information about the tests, check the references in the end of the article.

As a base for this integration, we’ll use the first article’s application so you can start over there. Considering that you have a system with the same conditions described there, lets include in the project the PHPUnit extension to work with Selenium. In the composer.json file, insert the key to download the component:

{
  ...
  "require-dev": {
    ...
    "phpunit/phpunit": "4.1.*",
    "phpunit/phpunit-selenium": ">=1.2"
    ...
  }
  ...
}

and install with:

composer.phar update

Then, download the Selenium RC Server. It is a .jar file that can be obtained here. The Selenium Webdriver uses Firefox for the default browser so you must have it installed. You can use other browser but here we’ll use the default. To run the webdriver, in the file folder:

java -jar selenium-server-standalone-2.*.*.jar

Be sure that the Codeigniter application runs anywhere.

TIP

After some PHP version, it comes with a pragmatical and simple built-in webserver for development environments (avoiding to configure a Apache or Nginx just for local dev). I’ll use it here going on the Codeigniter main folder and inserting the command:

php -S localhost:8888

You can check in your browser that the application runs perfectly. Even .htaccess Apache files are supported what makes this functionality compatible with the most of the projects.

Returning…

Now we’ll write a sample test that’ll use Selenium Webdriver. In this test we’ll:

  1. Access the application using Firefox;
  2. Check if the title of the page is “Welcome to CodeIgniter”, that means, the default Codeigniter’s start page.

Here is the code of the /tests/SeleniumTest.php file:

<?php
class SeleniumTest extends PHPUnit_Extensions_Selenium2TestCase
{
    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost:8888/');
    }

    public function testTitle()
    {
        $this->url('http://localhost:8888/');
        $this->assertEquals('Welcome to CodeIgniter', $this->title());
    }

}

After the file is created, run PHPUnit as we did before:

./vendor/bin/phpunit

The result should be something like:

taiar@guestxor:~/dev/ci$ ./vendor/bin/phpunit
PHPUnit 4.1.3 by Sebastian Bergmann.

Configuration read from /home/taiar/dev/ci/phpunit.xml.dist

..

Time: 6.33 seconds, Memory: 4.00Mb

OK (2 tests, 2 assertions)

Two tests were executed because of the first one of the first article. Like in the first article I’ll not talk about the testing theory and its benefits but I would hardly recommend that you should do. As a reference, I’ll let this link with many samples of the PHPUnit Selenium functions and I recommend the read of the whole file for a better understanding of all the possibilities Selenium offers.

References