My linux world » Maven Selenium Survival Guide

Maven Selenium Survival Guide


Integration Selenium with xvfb (X Virtual Framebuffer)

First, add this plugin in your pom.xml:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>selenium-maven-plugin</artifactId>
    <version>2.3</version>
    <executions>
 
      <execution>
        <id>xvfb</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>xvfb</goal>
        </goals>
        <configuration>
          <display>:1</display>
        </configuration>
      </execution>
 
      <execution>
        <id>start</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>start-server</goal>
        </goals>
        <configuration>
          <background>true</background>
        </configuration>
      </execution>
 
      <execution>
        <id>stop</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>stop-server</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Then, install xvfb and firefox:

yum install Xvfb firefox

Next, you have to create the Xvfb service (/etc/init.d/xvfbd):

#!/bin/bash
#
# /etc/rc.d/init.d/xvfbd
#
# chkconfig: 345 95 28
# description: Starts/Stops X Virtual Framebuffer server
# processname: Xvfb
#
 
. /etc/init.d/functions
 
[ "${NETWORKING}" = "no" ] && exit 0
 
PROG="Xvfb"
PROG_OPTIONS=":7 -ac -screen 0 1024x768x24"
PROG_OUTPUT="/tmp/Xvfb.out"
 
case "$1" in
    start)
        echo -n "Starting : X Virtual Frame Buffer "
        $PROG $PROG_OPTIONS>>$PROG_OUTPUT 2>&1 &
        disown -ar
        /bin/usleep 500000
        status Xvfb & >/dev/null && echo_success || echo_failure
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            /bin/touch /var/lock/subsys/Xvfb
            /sbin/pidof -o  %PPID -x Xvfb > /var/run/Xvfb.pid
        fi
        echo
        ;;
    stop)
        echo -n "Shutting down : X Virtual Frame Buffer"
        killproc $PROG
        RETVAL=$?
        [ $RETVAL -eq 0 ] && /bin/rm -f /var/lock/subsys/Xvfb /var/run/Xvfb.pid
        echo
        ;;
    restart|reload)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
    status)
        status Xvfb
        RETVAL=$?
        ;;
    *)
     echo $"Usage: $0 (start|stop|restart|reload|status)"
     exit 1
esac
 
exit $RETVAL

And launch the service:

chmod +x /etc/init.d/xvfbd
chkconfig xvfbd on
service xvfbd start
 
#init dbus (deal with this error:Failed to open "/var/lib/dbus/machine-id")
dbus-uuidgen > /var/lib/dbus/machine-id

Finally, run you maven project:

export DISPLAY=:7
mvn clean install

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.