Acceptance tests on Codeigniter with PHPUnit and Selenium21 Apr 2014
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:
and install with:
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.
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.
Now we’ll write a sample test that’ll use Selenium Webdriver. In this test we’ll:
- Access the application using Firefox;
- 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:
After the file is created, run PHPUnit as we did before:
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.