Skip to content

Instantly share code, notes, and snippets.

@eloytoro
Last active August 29, 2015 14:19
Show Gist options
  • Save eloytoro/3ca884e6d9fe1f2c9ae7 to your computer and use it in GitHub Desktop.
Save eloytoro/3ca884e6d9fe1f2c9ae7 to your computer and use it in GitHub Desktop.

Para lograr ejecute pruebas funcionales hay que configurar el server de selenium, xvfb, librerias necesarias para el headless browser y los reporters de jasmine que trabajan sin X11.

##Instalacion

  • node 0.10+ y protractor en tu server. sudo npm install protractor -g
  • xvfb sudo apt-get install xvfb
  • Librerias de x11 para los browsers sudo apt-get install x11-xkb-utils xfonts-100dpi xfonts-75dpi
  • sudo apt-get install xfonts-scalable xserver-xorg-core
  • sudo apt-get install dbus-x11
  • sudo apt-get install libfontconfig1-dev
  • Browsers sudo apt-get install chromium-browser firefox

##Xvfb

Crear un servicio que instancie xvfb como un daemon

/etc/init.d/xvb

#!/bin/bash
#
# Xvfb init script for Debian-based distros.
#
# The display number used must match the DISPLAY environment variable used
# for other applications that will use Xvfb. e.g. ':10'.
#
# From: https://github.com/gmonfort/xvfb-init/blob/master/Xvfb
#
### BEGIN INIT INFO
# Provides:          xvfb
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop custom Xvfb
# Description:       Enable service provided by xvfb
### END INIT INFO
 
NAME=Xvfb
DESC="$NAME - X Virtual Frame Buffer"
SCRIPTNAME=/etc/init.d/$NAME
XVFB=/usr/bin/Xvfb
PIDFILE=/var/run/${NAME}.pid
 
# Using -extension RANDR doesn't seem to work anymore. Firefox complains
# about not finding extension RANDR whether or not you include it here.
# Fortunately this is a non-fatal warning and doesn't stop Firefox from working.
XVFB_ARGS=":10 -extension RANDR -noreset -ac -screen 10 1024x768x16"
 
set -e
 
if [ `id -u` -ne 0 ]; then
  echo "You need root privileges to run this script"
  exit 1
fi
 
[ -x $XVFB ] || exit 0
 
. /lib/lsb/init-functions
 
[ -r /etc/default/Xvfb ] && . /etc/default/Xvfb
 
case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --background --make-pidfile --exec $XVFB -- $XVFB_ARGS ; then
      log_end_msg 0
    else
      log_end_msg 1
    fi
    log_end_msg $?
    ;;
 
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --retry 5
    if [ $? -eq 0 ] && [ -f $PIDFILE ]; then
      /bin/rm -rf $PIDFILE
    fi
    log_end_msg $?
    ;;
 
  restart|force-reload)
    log_daemon_msg "Restarting $DESC" "$NAME"
    $0 stop && sleep 2 && $0 start
    ;;
 
  status)
    status_of_proc -p $PIDFILE $XVFB $NAME && exit 0 || exit $?
    ;;
 
  *)
    log_action_msg "Usage: ${SCRIPTNAME} {start|stop|status|restart|force-reload}"
    exit 2
    ;;
esac
exit 0

Configurar permisos

sudo chown root:root /etc/init.d/xvfb
sudo chmod a+x /etc/init.d/xvfb
sudo update-rc.d  /etc/init.d/xvfb defaults

Inicia tu servicio con sudo service xvfb start


##Selenium

Ya protractor contiene un server que levanta selenium en /usr/local/lib/node_modules/protractor/selenium/selenium-server-standalone-X-XX-X.jar asi que usaremos eso para correr el daemon.

/etc/init.d/selenium

#!/bin/bash
#
# Selenium standalone server init script.
#
# For Debian-based distros.
#
### BEGIN INIT INFO
# Provides:          selenium-standalone
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Selenium standalone server
### END INIT INFO
 
DESC="Selenium standalone server"
USER=root
PID_FILE=/var/run/selenium.pid
WEBDRIVER_FILE=/usr/local/bin/webdriver-manager

# The value for DISPLAY must match that used by the running instance of Xvfb.
export DISPLAY=:10
 
# Make sure that the PATH includes the location of the ChromeDriver binary.
# This is necessary for tests with Chromium to work.
export PATH=$PATH:/usr/local/bin
 
case "$1" in
    start)
        echo "Starting $DESC: "
        start-stop-daemon -c $USER --start --background --pidfile $PID_FILE --make-pidfile --exec $WEBDRIVER_FILE start
        ;;

    stop)
        echo  "Stopping $DESC: "
        start-stop-daemon --stop --pidfile $PID_FILE
        ;;

    restart)
        echo "Restarting $DESC: "
        start-stop-daemon --stop --pidfile $PID_FILE
        sleep 1
        start-stop-daemon -c $USER --start --background --pidfile $PID_FILE  --make-pidfile --exec $WEBDRIVER_FILE start
        ;;

    *)
        echo "Usage: /etc/init.d/selenium-standalone {start|stop|restart}"
        exit 1
    ;;
esac

exit 0

Configurar permisos

sudo chown root:root /etc/init.d/selenium
sudo chmod a+x /etc/init.d/selenium
sudo update-rc.d  /etc/init.d/selenium defaults

Inicia tu servicio con sudo service selenium start


##Reporters

Para poder usar los headless browsers con jasmine usa los siguientes paquetes

En tu archivo de configuracion de pruebas del proyecto protractor.conf.js

exports.config = {
    onPrepare: function () {
        var jUnitReporter = require('jasmine-reporters').JUnitXmlReporter,
            htmlReporter = require('protractor-html-screenshot-reporter'),
            env = jasmine.getEnv();
        
        env.addReporter(new jUnitReporter('test/junit-report', true, true));
        
        env.addReporter(new htmlReporter({
            baseDirectory: 'e2e-reports/html-report'
        }));
    }
};

Ahora puedes correr protractor en tu servidor y las pruebas funcionales se ejecutaran.

Happy Testing :)


Fuentes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment