PHPUnit is a very handy tool you can use to test and detect possible mistakes in your PHP project.
Functional testing is also possible with PHPUnit using default headless browser (crawler). It works perfect as long as you don’t need to test functionality which uses JavaScript. For this purpose, you can use PHPUnit and Selenium.
Selenium is a powerful test tool that allows you to perform tests for web applications in any programming language using any mainstream browser.
Installation requirements
Before you start using Selenium to test your PHP project, you need to install the following:
- Selenium Server
- PHPUnit_Selenium package
1. Installing Selenium Server
Perform the following steps to install the Selenium Server:
- Download a distribution archive of Selenium Server.
- Unzip the distribution archive and copy selenium-server-standalone-2.35.0.jar (check the version suffix) to a folder from where you will run the server (such as /usr/local/bin).
- Start the Selenium Server by running the following command:
java -jar /usr/local/bin/selenium-server-standalone-2.35.0.jar
2. Installing PHPUnit_Selenium package
- The PHPUnit_Selenium package is necessary for natively accessing the Selenium Server from PHPUnit.
- To install it, run the following command:
pear install phpunit/PHPUnit_Selenium
Using Selenium in PHPUnit tests
There are two Selenium test cases:
- PHPUnit_Extensions_Selenium2TestCase
- PHPUnit_Extensions_SeleniumTestCase
PHPUnit_Extensions_Selenium2TestCase test case allows you to use the WebDriver API (partially implemented).
<?php class WebTest extends PHPUnit_Extensions_Selenium2TestCase { protected function setUp() { $this->setBrowser('firefox'); $this->setBrowserUrl('http://www.example.com/'); } public function testTitle() { $this->url('http://www.example.com/'); $this->assertEquals('Example WWW Page', $this->title()); } } ?>
PHPUnit_Extensions_SeleniumTestCase test case implements the client/server protocol to talk to Selenium Server as well as specialized assertion methods for web testing.
<?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->assertTitle('Example WWW Page'); } } ?>
Using different browsers with WebDrivers
For running tests with different browsers you should have WebDriver of that browser. As an example, we’ll try to use the Google Chrome browser and its WebDriver.
- Download WebDriver.
- To make Selenium Server aware of this WebDriver, perform one of the following tasks:
- Store the Chrome WebDriver binary in the system path
- Start the Selenium Server with -Dwebdriver.chrome.driver=path/to/your/chromedriver
You can now set Chrome as a browser for your functional test:
... $this->setBrowser('chrome'); ...
Running headless Selenium Server with Xvfb
Sometimes you don’t want the browser to be launched at your desktop during testing, or you may not be using Xserver. To make it work, one of the easiest solutions is to use Xvfb.
Xvfb is an X11 server that performs various graphical operations in memory, without displaying any screen output.
So, let’s try it. At first ensure that you have Xvfb installed on your server. If not, you can install it from your OS repository. For example, to install it on Ubuntu, run the following command:
sudo apt-get install xvfb
Once installed, to run your Selenium Server in Xvfb, run the following command:
DISPLAY=:1 xvfb-run java -jar selenium-server-standalone-2.35.0.jar
Once the server starts, run any of the test case examples. The output should display as follows:
PHPUnit 3.8.0 by Sebastian Bergmann. F Time: 4 seconds, Memory: 6.25Mb There was 1 failure: 1) WebTest::testTitle Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'Example WWW Page' +'Example Domain' /your/project/Tests/Functional/WebTest.php:14 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
That’s all for now. Enjoy testing!
Source references:
Very nice post.
Was searching for complete howto use Selenium with PHPUnit in Google, but all other posts are simply readme reposts.
Good.. I wanted to try Selenium with PHPUnit.
The below link provides tutorial for Selenium With Java
http://seleniumeasy.com