This a tutorial about front-end unit testing using Selenium Remote Control and Firefox on a headless Linux server.
Requirements
Install Selenium
Install Firefox
Install Xvfb
Install PHPUnit
Ant's Build File
Copy / paste as build.xml
PHPUnit Test Script
Copy / paste as apw.php
Screenshots
Start Selenium
Execute Test Script
Result
screenshots
Stop Selenium
Troubleshooting
SecurityPolicy file error
Error message
You need to specify SecurityPolicy file location by using -sp argument
Suppress exec output
Prevent Ant's <exec> output messages from displaying. Add spawn attribute and set value to true.
Firefox error
Make sure Firefox is accessible and you have properly configured PATH variable.
Requirements
- Selenium Remote Control
- Firefox
- Xvfb (X Windows Virtual Frame Buffer)
- PHPUnit
Install Selenium
$ wget http://selenium.googlecode.com/files/selenium-remote-control-1.0.3.zip
$ unzip selenium-remote-control-1.0.3.zip -d /opt/selenium-remote-control-1.0.3
$ ln -s /opt/selenium-remote-control-1.0.3 /opt/selenium
Install Firefox
$ wget http://openaccess.oldapps.com/mozilla.org//firefox/releases/3.6.3/linux-i686/en-US/firefox-3.6.3.tar.bz2
$ tar xjf firefox-3.6.3.tar.bz2
$ export PATH=$PATH:/path/to/firefox/dir
Install Xvfb
$ yum install Xvfb
Install PHPUnit
$ pear channel-discover pear.phpunit.de
$ pear install channel://pear.phpunit.de/PHPUnit
Ant's Build File
Copy / paste as build.xml
<project name="apw" default="build" basedir=".">
<target name="start-selenium">
<exec executable="Xvfb" spawn="true">
<arg line=" :0 -ac -noreset -screen 0 1024x768x24 -sp /etc/X11/fs/config"/>
</exec>
<exec executable="java" spawn="true">
<env key="DISPLAY" value=":0"/>
<arg line="-jar /opt/selenium/selenium-server-1.0.3/selenium-server.jar -log /tmp/selenium.log"/>
</exec>
</target>
<target name="stop-selenium">
<get taskname="selenium-shutdown"
src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"
dest="/tmp/result.txt" ignoreerrors="true" />
<echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
</target>
</project>
PHPUnit Test Script
Copy / paste as apw.php
<?php
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class APW extends PHPUnit_Extensions_SeleniumTestCase
{
protected $autoStop = FALSE;
protected $captureScreenshotOnFailure = TRUE;
protected $screenshotPath = '/tmp/screenshots';
protected $screenshotUrl = 'http://localhost/screenshots';
protected function setUp()
{
$this->setBrowser("*chrome");
$this->setBrowserUrl("http://www.autopartswarehouse.com/");
}
public function test_search()
{
$this->open("/");
$this->click("search_text");
$this->type("search_text", "brakes");
$this->click("submit");
$this->waitForPageToLoad("30000");
try {
$this->assertTrue($this->isTextPresent("You searched for \"Brakes\" (2 matches)"));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
//$this->drivers[0]->captureEntirePageScreenshot($this->screenshotPath . DIRECTORY_SEPARATOR . str_replace('/', '_', $this->getLocation()) . $this->testId . '.png');
$this->drivers[0]->captureEntirePageScreenshot($this->screenshotPath . DIRECTORY_SEPARATOR . rawurlencode($this->getLocation()) . '.png');
}
}
}
Screenshots
$ mkdir /tmp/screenshots
Start Selenium
$ ant start-selenium
Execute Test Script
$ phpunit apw.php
Result
PHPUnit 3.4.12 by Sebastian Bergmann.
F
Time: 17 seconds, Memory: 7.75Mb
There was 1 failure:
1) APW::test_search
Failed asserting that <boolean:false> is true.
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
screenshots
$ ls -l /tmp/screenshots
Stop Selenium
$ ant stop-selenium
Troubleshooting
SecurityPolicy file error
Error message
FreeFontPath: FPE "unix/:7100" refcount is 2, should be 1; fixing.
error opening security policy file /usr/lib64/xserver/SecurityPolicy
You need to specify SecurityPolicy file location by using -sp argument
$ Xvfb :0 -ac -noreset -screen 0 1024x768x24 -sp /etc/X11/fs/config
Suppress exec output
Prevent Ant's <exec> output messages from displaying. Add spawn attribute and set value to true.
<exec executable="java" spawn="true">
<arg line="-jar /opt/selenium/selenium-server-1.0.3/selenium-server.jar -log /tmp/selenium.log"/>
</exec>
Firefox error
Failed to start new browser session: java.lang.RuntimeException: Firefox 3 could not be found in the path!
Please add the directory containing ''firefox-bin' or 'firefox'' to your PATH environment
Make sure Firefox is accessible and you have properly configured PATH variable.
$ export PATH=$PATH:/path/to/firefox/dir