Dec
31
2012

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

Installing

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 google.com in firefox and write www.google.com 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 google.com, 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:

<?php

class Example extends PHPUnit_Extensions_SeleniumTestCase

{

protected function setUp()

{

$this->setBrowser(“*firefox”);

$this->setBrowserUrl(“https://www.google.com/“);

}

public function testMyTestCase()

{

$this->open(“/”);

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

$this->click(“id=gbqfb”);

$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

About the Author:

1 Comment + Add Comment

  • I am comparatively new to selenium and played a little with selenium IDE, and trying to do with selenium rc. I am baffled to use the scripting language there. Can you tell me what is the most used language in selenium across industries? If someone does in Java and he joins a company where everyone does in ruby, then it’ll be a pain to learn ruby again. And also it would be great if you address any comparison about the available languages like (perl, python, ruby, java etc.) or tell me any other guidance would really appreciate help and also i would like to thank for all the information you are providing.

Leave a comment

Partners