This article intends to summarize the steps taken to set up PHPUnit and Selenium for a Yii project, with IntelliJ IDEA Ultimate (the IDE). It was done on Mac OS X 10.6.
The article will most likely also be applicable for PhpStorm, since these two products share the same foundation (IDEA uses the same code for PHP support as PhpStorm has in its core). It should at most be a matter of slightly different menu items and stuff.
We assume that you have the following:
.jar file for Seleniumyiic webapp ...). Note: In the examples below this app is in the folder /Users/rawtaz/Sites/yii/test in the filesystem, and the URL to it is http://localhost/~rawtaz/yii/test/ This part will first upgrade PEAR so that it has the latest resources, then configure it so that the PEAR channels are discovered automatically, and finally install PHPUnit and its Selenium support.
Run the following commands in your Terminal (use Spotlight to find the Terminal application in case you haven't used it before):
% sudo pear upgrade PEAR % sudo pear config-set auto_discover 1 % sudo pear install pear.phpunit.de/PHPUnit % sudo pear install pear.phpunit.de/PHPUnit_Selenium
Next we need to install the actual Selenium Server. Download the "Selenium Server" .jar file, e.g. selenium-server-standalone-2.25.0.jar, from the Selenium download page. Save the file wherever it makes sense to you; It is an application file that you will run directly.
Once the file is downloaded, start the Selenium Server by running the following commands in your terminal:
Note: For the sake of example, we assume that the file's name is selenium-server-standalone-2.25.0.jar and that it was saved in the folder ~/jar – change these details as appropriate.
% cd ~/jar % java -jar selenium-server-standalone-2.25.0.jar
Selenium should now start and run in this Terminal window. The output should look like the following:
% java -jar selenium-server-standalone-2.25.0.jar 2012-sep-02 14:58:17 org.openqa.grid.selenium.GridLauncher main INFO: Launching a standalone server 14:58:19.373 INFO - Java: Apple Inc. 20.8-b03-424 14:58:19.375 INFO - OS: Mac OS X 10.6.8 x86_64 14:58:19.384 INFO - v2.25.0, with Core v2.25.0. Built from revision 17482 14:58:19.527 INFO - RemoteWebDriver instances should connect to: http://127.94.0.1:4444/wd/hub 14:58:19.528 INFO - Version Jetty/5.1.x 14:58:19.529 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver] 14:58:19.530 INFO - Started HttpContext[/selenium-server,/selenium-server] 14:58:19.531 INFO - Started HttpContext[/,/] 14:58:19.593 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@13f6ba0f 14:58:19.593 INFO - Started HttpContext[/wd,/wd] 14:58:19.601 INFO - Started SocketListener on 0.0.0.0:4444 14:58:19.602 INFO - Started org.openqa.jetty.jetty.Server@65faba46
Note: These instructions might differ slightly if you are using PhpStorm, but in general it's the same.
Assuming that your project in IDEA/PhpStorm only knows about the Yii application's files, we need to add the Yii Framework files and PHPUnit's files to the so called "include path". The purpose of this is to make IDEA/PhpStorm aware of methods and properties on the various classes from Yii and PHPUnit, so that it can provide code completion and inspection for you.
Open [Preferences] -> Project Settings -> PHP -> Include path and click the + button to add the following two items to the Include path list:
~/Sites/yii/yii/framework (this can of course be skipped if you already did this or you have added the Yii framework files as a Content Root in a module)/usr/pkg/lib/php/PHPUnit*Note: Both of the filesystem paths above might very well be different on your system - you'll have to figure out where PHPUnit was installed and where you have the Yii Framework files (the folder named framework).
We need to make an initial configuration of the class that is used for functional testing, so that it knows which URL to open using Selenium.
Open the file protected/tests/WebTestCase.php in your Yii projec and edit the TEST_BASE_URL constant so that it points to the URL of the index-test.php file in your Yii project
In this example my URL is http://localhost/~rawtaz/yii/test/index-test.php/, so I changed the constant like this:
define('TEST_BASE_URL','http://localhost/~rawtaz/yii/test/index-test.php/');
A summary of this problem is in the Yii Forum thread at http://www.yiiframework.com/forum/index.php/topic/30511-firefoxphp-faild-to-include-in-yiibase/. There will most likely be a fix for this on the future, but for now we work around the problem like suggested in the aforementioned forum thread.
protected/tests/WebTestCase.php in your Yii projectAdd $this->setBrowser('*firefox'); as the last thing in the setUp() method
Open the file protected/tests/phpunit.xml in your Yii project
<browser .../> tags you can find inside the <selenium>...</selenium> tagsSelenium will now use Firefox for the testing.
Finally, we try to run the default test for your Yii application (we assume it is still the default one generated by yiic webapp ...), to see that they work:
Run the following commands in a new Terminal window:
% cd ~/Sites/yii/test/protected/tests % phpunit functional/SiteTest.php
Note: In the example above, the path to the project is ~/Sites/yii/test, but yours might be different – adjust accordingly.
What you should see is something like this:
% phpunit functional/SiteTest.php PHPUnit 3.6.12 by Sebastian Bergmann. Configuration read from /Users/rawtaz/Sites/yii/test/protected/tests/phpunit.xml ... Time: 23 seconds, Memory: 5.50Mb OK (3 tests, 12 assertions) %
The three dots in the fourth line of text in the above output means that three tests passed. If one of them had failed there would have been an 'F' instead of a dot for that test.
Be the first person to leave a comment
Please login to leave your comment.