Saturday, July 26, 2014

Selenium WebDriver + Xvfb + PHP PHPUnit

Selenium WebDriver is one of the best and most mature tools for functional testing of WEB applications, supports wide range of browsers and bindings for all major language platforms.

Here is brief overview of possible selenium setup, which by the way has a lot of possibilities for improvement but can be good for quick and easy start.

1. Install Jenkins

Make sure Java is installed, and look at "Important Note on CentOS Java" notes

Jenkins HOME will be located under

go to "Manage Jenkins" = > "Manage Plugins"
and install the following plugins:

"Subversion" // also "Git" or whatever VCS you use
"Violations" // powerfull static code ana;ysis tool
"Phing"  // can be replaces with Ant, Maven or even Gradle
"xUnit"  // phpUnit output processing
"Xvfb" // Xvfb support

Upgrade all existing plugins

go to "Manage Jenkins" = > "configure System"

Setup SMTP and test if email notifications are send

2. Install PHP tools


additionally install phpunit/PHP_Invoker


for xdebug:

Try to run
# phing

You should receive:
Buildfile: build.xml does not exist!

4. Install virtual frame buffer, Selenium WebDriver, Install Firefox & Google Chrome browsers

Don't forget to run:
# chmod +x /etc/init.d/xvfb
and start service

# service xvfb start

Install Firefox right from repository:
# yum install firefox
# export DISPLAY=:99   # this is needed to run firefox from under current session by default on :99 display
echo "export DISPLAY=:99" >> /etc/sysconfig/jenkins

Install Google Chrome from Google repository
Also see the:
# google-chrome
Chrome driver should be downloaded from this page:
Follow the next steps:
# cd /usr/lib/selenium/
# wget …
# unzip
# rm -f

in order to test the whole setup you can do:
# firefox &

# yum install xorg-x11-apps ImageMagick

# xwd -root -display :99 | convert xwd:- capture.png

the screenshot should display browser with appropriate page

also please see the following articles for more information

USEFULL INFO: In case there will be any issues starting Firefox - you should drop the firefox profile by
# cd /var/lib/jenkins
# rm -rf ./.mozilla
# rm -rf ~/.mozilla
After this Firefox will start successfully.

USEFULL INFO: Selenium WebDriver should be updated manually along side with possbile Firefox automatic update that can come from repository. So each time you update firefox via "yum upgrade" you should download the latest version of selenium from this page "" into following directory "/usr/lib/selenium" and remove the old one
After each Firefox upgrade - new minor release of Selenium WebDriver is released as well in order to support the latest version of browser.

It is a good practice to install Selenium WebDruver as service

create Selenium installation directory

# mkdir -p /usr/lib/selenium
# cd /usr/lib/selenium

go to and download the latest version of Selenium WebDriver

# wget

create service file

# mkdir -p /var/log/selenium
# touch /var/log/selenium/selenium-output.log

# chmod +x /etc/init.d/selenium
# nano /etc/init.d/selenium

input the following content


case "${1:-''}" in
                if test -f /tmp/
                        echo "Selenium is already running."
                        echo "Starting Selenium..."
# ugly fix of display issue
export DISPLAY=:99
# start selenium server
                        java -jar /usr/lib/selenium/selenium-server-standalone-*.jar -port 4444 > /var/log/selenium/selenium-output.log 2> /var/log/selenium/selenium-error.log & echo $! > /tmp/

                        if test $error -gt 0
                                echo "${bon}Error $error! Couldn't start Selenium!${boff}"
echo "Selenium Server STARTED"
                if test -f /tmp/
                        echo "Stopping Selenium..."
                        PID=`cat /tmp/`
                        kill -3 $PID
                        if kill -9 $PID ;
                                        sleep 2
                                        test -f /tmp/ && rm -f /tmp/
echo "Selenium STOPPED"
                                        echo "Selenium could not be stopped..."
echo "Stopping aggresively..."
kill $(ps aux | grep '[s]elenium-server' | awk '{print $2}') # more aggressive kill
rm -f /tmp/
echo "Killed server"
                        echo "Selenium is not running."
                if test -f /tmp/
                        kill -HUP `cat /tmp/`
                        test -f /tmp/ && rm -f /tmp/
                        sleep 1
                        java -jar /usr/lib/selenium/selenium-server-standalone-*.jar -port 4444 > /var/log/selenium/selenium-output.log 2> /var/log/selenium/selenium-error.log & echo $! > /tmp/
                        echo "Reload Selenium..."
                        echo "Selenium isn't running..."
        *)      # no parameter specified
                echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
                exit 1


start selenium server manually (don't forget to stop appropriate service before):
# java -jar /usr/lib/selenium/selenium-server-standalone-*.jar -port 4444

start firefox with appropriate URL
# firefox

make a screenshot of Xvfb
# xwd -root -display :99 | convert xwd:- capture.png