Ubuntu 14.04 – Install Selenium as service (headless)
This tutorial will show you how to install Selenium as a service on a Ubuntu 14.04 server. An Ubuntu server doesn’t have a GUI in contrast to Ubuntu desktop, so we will also need to setup a virtual screen where firefox will run.
Perform all actions as root!
Selenium is a Java application, so make sure Java is installed. Installation instructions: https://www.digitalocean.com/community/tutorials/how-to-install-java-on-ubuntu-with-apt-get.
Install Firefox and Xvfb.
apt-get update apt-get install firefox xvfb
Create a virtual screen a (re)boot.
crontab -e
Add the following line as first cronjob:
@reboot sh -c 'Xvfb :99 -ac -screen 0 1024x768x8 > /tmp/xvfb.log 2>&1 &'
Create the Selenium directory.
mkdir /usr/lib/selenium cd /usr/lib/selenium
Replace the following link with the latest version. Find the latest version here: http://selenium-release.storage.googleapis.com/index.html.
wget http://selenium-release.storage.googleapis.com/2.43/selenium-server-standalone-2.43.1.jar
ln -s selenium-server-standalone-2.43.1.jar selenium-server-standalone.jar
mkdir -p /var/log/selenium chmod a+w /var/log/selenium
Create a service file.
nano /etc/init.d/selenium
Paste the following code in the file. I added the -trustAllSSLCertificates option to accept self signed SSL certificates (for example in testing and staging environments). Remove it if you only want to allow valid and verified SSL certificates.
case "${1:-''}" in
'start')
if test -f /tmp/selenium.pid
then
echo "Selenium is already running."
else
export DISPLAY=localhost:99.0
java -jar /usr/lib/selenium/selenium-server-standalone.jar -port 4444 -trustAllSSLCertificates > /var/log/selenium/output.log 2> /var/log/selenium/error.log & echo $! > /tmp/selenium.pid
echo "Starting Selenium..."
error=$?
if test $error -gt 0
then
echo "${bon}Error $error! Couldn't start Selenium!${boff}"
fi
fi
;;
'stop')
if test -f /tmp/selenium.pid
then
echo "Stopping Selenium..."
PID=`cat /tmp/selenium.pid`
kill -3 $PID
if kill -9 $PID ;
then
sleep 2
test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
else
echo "Selenium could not be stopped..."
fi
else
echo "Selenium is not running."
fi
;;
'restart')
if test -f /tmp/selenium.pid
then
kill -HUP `cat /tmp/selenium.pid`
test -f /tmp/selenium.pid && rm -f /tmp/selenium.pid
sleep 1
export DISPLAY=localhost:99.0
java -jar /usr/lib/selenium/selenium-server-standalone.jar -port 4444 -trustAllSSLCertificates > /var/log/selenium/output.log 2> /var/log/selenium/error.log & echo $! > /tmp/selenium.pid
echo "Reload Selenium..."
else
echo "Selenium isn't running..."
fi
;;
*) # no parameter specified
echo "Usage: $SELF start|stop|restart"
exit 1
;;
esac
chmod 755 /etc/init.d/selenium
Add the Selenium service as startup script, so Selenium will start every time when the server starts.
update-rc.d selenium defaults
Reboot the Ubuntu server and Selenium should be running fine!
reboot
Troubleshooting
To check if Selenium is indeed running on port 4444:
netstat -tln
Log files:
- /var/log/selenium/selenium-error.log
- /var/log/selenium/selenium-output.log
- /tmp/xvfb.log
PHPUnit
An example to use Selenium with PHPUnit:
<phpunit bootstrap="test/Bootstrap.php" colors="true">
<testsuites>
<testsuite name="Project">
<directory suffix="Test.php">module</directory>
<directory suffix="Test.php">test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">module</directory>
<directory suffix=".php">test</directory>
</whitelist>
</filter>
</phpunit>
* @group SeleniumFunctionalTest
*/
class SeleniumTest extends PHPUnit_Extensions_SeleniumTestCase
{
public static $seleneseDirectory = 'test/selenium';
protected $captureScreenshotOnFailure = true;
protected $screenshotUrl = 'https://testing.example.com/selenium-screenshots';
protected function setUp()
{
parent::shareSession(true);
$this->screenshotPath = __DIR__ . '/../test/selenium/screenshots';
$this->setBrowser('*firefox');
$this->setTimeout(180);
$this->setBrowserUrl('https://testing.example.com');
}
}
Your service file example was very helpful. However, this line:
creates a serious issue when you try to stop or restart the service. By combining these two commands, the process id stored in $! is that of the export command, not the java command.
So the first time you run the start command, it works. The stop command reports that it worked, but it didn’t, because the pid stored in the pid file is wrong.