in Tutorial

PHPUnit and Selenium in a Yii WebApp

Let’s look at how we use selenium and phpunit in a Yii app. After this tutorial you will get understand how functional tests can be written in a web App. We assume that you have general information about phpunit and testing in yii framework or you may want to read previous posts


The tools we need to install are listed below

  • PHPUnit with Selenium package(pear install phpunit/PHPUnit_Selenium)
  • Selenium IDE is a firefox plugin that records the user actions and generates test cases. It can be downloaded and installed from web site.
  • Selenium IDE: PHP Formatters is required to generate PHP codes from test cases. It can be downloaded from Firefox add-ons
  • Selenium Server is needed to run functional test cases just download from web site and save it with name selenium-server.jar

Using Selenium IDE

At this stage, we assume that you have installed Selenium IDE, Selenium IDE: PHP Formatters plugins to Firefox so open your firefox and follow the steps.

  • Open “Tools > Selenium IDE”. You should see the window below.

When Selenium IDE is opened it is on recording mode ( the red button in right upper corner). If you click again it stops recording and click again to start recording.

  • Open in firefox and write to Base URL field in Selenium IDE, start recording.
  • Write “Selenium” to search box and click the search button.
  • Highlight “Selenium – Web Browser Automation”, right click and click verifyTextPresents item.
  • Return the Selenium IDE and stop recording. Selenium IDE should look like below.

  • Now “Play the test case”. Selenium IDE should open the, write the “Selenium” to search box and click the search button. Lastly, it checks if “Selenium – Web Browser Automation” is exists. (if test fails at one step, please make the test run a bit slower by arranging the fast-slow bar.) I know, this test is meaningless but it gives the idea what we can do with Selenium IDE.
  • Assume that test is ok and we want to export it. Then “File > Export Test Case As > PHP (PHPUnit)” and save the file somewhere appropriate. File content should be like that:


class Example extends PHPUnit_Extensions_SeleniumTestCase


protected function setUp()





public function testMyTestCase()



$this->type(“id=gbqfq”, “selenium”);


$this->verifyTextPresent(“Selenium – Web Browser Automation”);




If you know somethings about PHPUnit, codes are above should be familiar to you. Before we can run this code with phpunit command we should run the Selenium Server by typing the command below. (We have downloaded Selenium Server and saved it our computer)

java -jar selenium-server.jar

Now we can run our test case as we are running a traditional phpunit test case.

phpunit Example

If everthing goes well, firefox will be opened and test case will be run as we specified.

If you get understand the general concept, you can study Selenium IDE, look at commands and their descriptions.

Selenium and Yii

Almost same but there are some points we need to take care in writing functional tests in Yii Framework. Let me summarize them…

  • All functional tests should be under “protected/tests/functional” folder by convention
  • Base class for testing classes is “WebTestCase” not “PHPUnit_Extensions_SeleniumTestCase”.
  • We can use fixtures like we use in yii unit testing.
  • We need to open “index-test.php” in functional tests. Sometimes phpunit opens default “index.php” so I put “index-test.php” to “open” function at the head of the functions.

Ahmet Oguz Mermerkaya

ahmetmermerkaya at gmail dot com

Related Posts

Written By:

One Comment

  1. sumit kher February 18, 2013 Reply

Add a Comment

Your email address will not be published. Required fields are marked *