I’ve been doing some work this iteration on getting Selenium RC integrated into our build process so we can run a suite of automated functional regression tests against our application on each build. The application I’m working on is written in PHP, normally when you use Selenium IDE to record a test script it saves it as a HTML file.
For example a simple test script that goes to Google and verifies that the text “Search:” is present on the screen and the title of the page is “iGoogle” looks like this:
-
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
<title>New Test</title>
-
</head>
-
<body>
-
<table cellpadding="1" cellspacing="1" border="1">
-
<thead>
-
<tr><td rowspan="1" colspan="3">New Test</td></tr>
-
</thead><tbody>
-
<tr>
-
<td>open</td>
-
<td>/ig?hl=en</td>
-
<td></td>
-
</tr>
-
<tr>
-
<td>verifyTextPresent</td>
-
<td>Search:</td>
-
<td></td>
-
</tr>
-
<tr>
-
<td>assertTitle</td>
-
<td>iGoogle</td>
-
<td></td>
-
</tr>
-
-
</tbody></table>
-
</body>
-
</html>
-
You can choose to export the script in several other languages, including PHP, in which case the test script it produces looks like this:
-
span class=”st0″>’Testing/Selenium.php’‘PHPUnit/Framework/TestCase.php’"*firefox", "http://localhost:4444/""/ig?hl=en""Search:""iGoogle"
The Export produces a valid PHPUnit test case that uses the Selenium PHP Client Driver(Selenium.php). Whilst the script is valid and will run you do need add a little more to it before the test will correctly report errors. As it stands all errors captured during the test are added to an array called verificationErrors, by catching the assertion Exceptions that are thrown when an assert fails, in other words if you ran this test as it is, and it did fail you wouldn’t know! To correct this we need to do two things. Firstly, each assert needs to have a message added to it which will printed out in the test report if the assert fails. Secondly we need to modify the tearDown method so that once a test has run, it checks the verificationErrors array, and if any failures have occurred, fails the test. After making these changes the PHP test script looks like this:
-
span class=”st0″>’Testing/Selenium.php’‘PHPUnit/Framework/TestCase.php’"*firefox",
-
"http://localhost:4444/""VERIFICATION ERRORS:""\n""/ig?hl=en""Search:"),
-
"The string Search: was not found""iGoogle",
-
$this->selenium->getTitle(),
-
"The page title did not match iGoogle."
Obviously, I have also given the PHP Class and test function slightly more meaningful names. Now you have a PHP Unit Test case that will use the Selenium PHP Client Driver with Selenium Remote Control to launch a browser, go to the specified URL, and test a couple of assertions. If any of those assertions fail, the tearDown method fails the test … pretty cool, right?
Well now it get’s better. Because the Selenium Client Driver has a published api which is pretty easy to follow, there’s no reason why you can’t just write test cases without using Selenium IDE … for those who want to you could even incorporate this into a TDD process. But for all this to hang together we need to be able to run a build, on a Continuous Integration server which checks out the code, runs unit tests and selenium regression tests against that code line, and only if all tests succeed passes the build.
We are currently using ANT, and CruiseControl to handle our CI/Automated build process. When running the automated suite of tests we need to ensure that the Selenium Remote Control server is also running which creates some complications. The Selenium Remote Control server takes several arguments which can also include the location of a test suite of html based selenium tests – which is really nice because the server will start, execute those tests and then end. Unfortunately you can’t invoke the server and pass it the location of a PHP based test suite. This means you need to find a way to start up the server, then run your tests, and once they are complete, shut the selenium server down.
He are the ANT targets that I have written to achieve this, if anyone can think of better ways of doing this I’d welcome any feedback or suggestions, to run this example you’d simply enter the command “ant selenium” :
-
-
<target name="selenium" depends="clean, init" description="Run the Selenium tests">
-
<parallel>
-
<antcall target="StartRCServer" />
-
<antcall target="RunSeleniumTests" />
-
</parallel>
-
</target>
-
-
<target name="StartRCServer" description="Start the Selenium RC server">
-
<java jar="dependencies/SeleniumRC/lib/selenium-server.jar"
-
fork="true" failonerror="true">
-
<jvmarg value="-Dhttp.proxyHost=host.domain.com"/>
-
<jvmarg value="-Dhttp.proxyPort=80"/>
-
</java>
-
</target>
-
-
<target name="RunSeleniumTests" description="RunAllSeleniumTests">
-
<sleep milliseconds="2000" />
-
<echo message="======================================" />
-
<echo message="Running Selenium Regression Test Suite" />
-
<echo message="======================================" />
-
<exec executable="php"
-
failonerror="false"
-
dir="test/seleniumtests/regressiontests/"
-
resultproperty="regError">
-
<arg line="../../../dependencies/PHPUnit/TextUI/Command.php –log-xml ../../../doc/SeleniumTestReports/RegressionTests/TestReport.xml AllRegressionTests" />
-
</exec>
-
<get taskname="selenium-shutdown"
-
src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"
-
dest="result.txt" ignoreerrors="true" />
-
-
<condition property="regressionTest.err">
-
<or>
-
<equals arg1="1" arg2="${regError}" />
-
<equals arg1="2" arg2="${regError}" />
-
</or>
-
</condition>
-
-
<fail if="regressionTest.err" message="ERROR: Selenium Regression Tests Failed" />
-
</target>
-
A couple of notes, the reason I have to use a conditional check at the end of the selenium target is because, if the exec task that runs the PHP tests was set to failonerror=true then the build would never reach the next line which shuts the Selenium RC server down. To ensure that always happens I have to set the exec to failonerror=false, but this means I have to check what the result was from the exec. Which if successful will return 0, if test failures exist will return 1, and if there were any errors (preventing a test to be exectuted ) will return 2. Hence the conditional check sets regressionTest.err if either of these latter two conditions are true.
Also in order to start up the server, which could take up to a second, but can’t be sure precisely how long. I have to use the Ant Parallel task, which calls the task to start the server and the task to run the tests at the same time. The task to run the tests has a 2 second sleep in it, which should be more than enough time to allow the server to start. This all kind of feels a little clunky, but at the moment it does work very well.
In a nutshell, thats how you integrate PHP based Automated Selenium Regression tests into a continuous build.
-
3 thoughts on “Automated regression testing and CI with Selenium PHP”
-
Nadeem,
By the way, Parabuild provides formatted PHPUnit logs and statistics on its results pages:
http://www.viewtier.com/products/parabuild/eap/phpunit.htm
Slava
Good idea to evaluate $this->verificationErrors in the tearDown() function. I told Sebastian Bergmann (author of PHPUnit) of this idea … he might add it to a future version of PHPUnit.
Hi, I am new to selenium and phpunit. downloaded selenium IDE, selenium RC and Selenium PHP Client Driver. my task is to automate register page of a webpage. I recorded for one test user using selenium IDE. exported it php format , add some changes and saved the file as register.php. now how to run my register.php. If i ran this file it should create many users automatically. please help me with this. Thanks in advance!