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

https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Red+Hat+distributions

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

Jenkins HOME will be located under
/var/lib/jenkins


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

PHPUnit
http://phpunit.de/manual/4.1/en/installation.html

additionally install phpunit/PHP_Invoker

Phing
http://www.phing.info/trac/wiki/Users/Installation

for xdebug:
http://www.euperia.com/development/php/php-warning-xdebug-must-be-loaded-as-a-zend-extension-in-unknown-on-line-0-fix/234

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

http://www.jpalomaki.fi/?p=403

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
See: http://www.if-not-true-then-false.com/2010/install-google-chrome-with-yum-on-fedora-red-hat-rhel/
Also see the: http://stackoverflow.com/questions/10792403/how-do-i-get-chrome-working-with-selenium-using-php-webdriver
# google-chrome http://google.com/
Chrome driver should be downloaded from this page:http://code.google.com/p/chromedriver/downloads/list
Follow the next steps:
# cd /usr/lib/selenium/
# wget http://chromedriver.googlecode.com/file … 1383.0.zip
# unzip chromedriver_linux64_26.0.1383.0.zip
# rm -f chromedriver_linux64_26.0.1383.0.zip



in order to test the whole setup you can do:
# firefox http://google.com &

# 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
http://ralf.schaeftlein.de/2012/05/26/running-headless-webdriver-based-selenium-junit-tests-inside-jenkins-under-ubuntu-linux/

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 "http://seleniumhq.org/download/" 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 http://seleniumhq.org/download/ and download the latest version of Selenium WebDriver

# wget http://selenium.googlecode.com/files/selenium-server-standalone-2.25.0.jar

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

#!/bin/bash

case "${1:-''}" in
        'start')
                if test -f /tmp/selenium.pid
                then
                        echo "Selenium is already running."
                else
                        echo "Starting Selenium..."
# ugly fix of display issue
export DISPLAY=:99
# start selenium server
                        java -Dwebdriver.chrome.driver=/usr/lib/selenium/chromedriver -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/selenium.pid

                        error=$?
                        if test $error -gt 0
                        then
                                echo "${bon}Error $error! Couldn't start Selenium!${boff}"
else
echo "Selenium Server STARTED"
                        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
echo "Selenium STOPPED"
                                else
                                        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/selenium.pid
echo "Killed server"
                                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
                        java -Dwebdriver.chrome.driver=/usr/lib/selenium/chromedriver -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/selenium.pid
                        echo "Reload Selenium..."
                else
                        echo "Selenium isn't running..."
                fi
                ;;
        *)      # no parameter specified
                echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
                exit 1
        ;;
esac


STOUBLESHOOTING TIPS


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 http://google.com

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

1 comment:

  1. Ways & Works Consulting is an ISO 9001:2015 certified HR Consulting Firm, Manpower Recruitment and Practical Training firm career with an aim to provide value aided service to employersTop Manpower Recruitement company in india . with an in-depth understanding of their requirements. We are working as a perfect bridge between the employer and employee to fulfil their needs by placing the best suitable at place.

    ReplyDelete