Skip to content

Instantly share code, notes, and snippets.

@abruzzi
Last active January 10, 2017 02:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abruzzi/8949454 to your computer and use it in GitHub Desktop.
Save abruzzi/8949454 to your computer and use it in GitHub Desktop.
Setup E2E testing enviornment on Ubuntu(with selenium standalone and Xvfb)

Setup E2E testing enviornment on Ubuntu

这篇文档中,会介绍如何在Ubuntu中设置端到端的测试环境。这个环境中,需要一些软件作为基础设置

  1. Xvfb
  2. Selenium
  3. Protractor
  4. Chrome+ChromeDriver

按装并设置完成之后,这个环境可以运行基于Web的应用程序的单元测试(基于Karma或者Jasmine),以及端到端测试(基于Protractor)。事实上,基于selenium客户端或者cucumber的测试也可以很容易的在该环境中运行起来。

Xvfb (X virtual framework buffer)

Xvfb是一个虚拟的X服务,简而言之,是一个运行在内存中的图形系统。任何图形界面的应用程序都可以运行其上。主要用途就是测试。

$ sudo apt-get install xvfb

创建Xvfb服务脚本,并保存到/etc/init.d/Xvfb中。然后修改该文件权限,并加入启动列表(这样当服务器重启之后,该服务会自动被启动)

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

Selenium服务器

下载seleniumjar包,选择standalone即可。可以通过命令行的wget来完成:

sudo /usr/sbin/useradd -m -s /bin/bash -d /home/selenium selenium
wget http://selenium.googlecode.com/files/selenium-server-standalone-2.39.0.jar
sudo mkdir /usr/local/share/selenium
sudo mv selenium-server-standalone-2.39.0.jar /usr/local/share/selenium
sudo chown -R selenium:selenium /usr/local/share/selenium

selenium服务创建一个日志目录

sudo mkdir /var/log/selenium
sudo chown selenium:selenium /var/log/selenium

创建selenium服务,修改权限,注册服务到启动项。

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

Browsers installation

安装浏览器所需要的依赖。

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 chromium-browser firefox

如果需要使用phantomjs,还需要安装一个额外的包:

sudo apt-get install libfontconfig1-dev

Protractor

protractor是运行angularjs端到端测试的工具,它会使用浏览器和selenium

$ sudo npm install -g protractor

Tricks

下载ChromeDriver,这个包用于和seleniumChrome浏览器通信。

Chrome在安装之后,默认路径在/usr/bin/chromium-browser,而selenium却会在另一个地方,以另一个名字来找Chrome,所以需要建立一个符号链接:

$ sudo ln -s /usr/bin/chromium-browser /usr/bin/google-chrome

启动所有图形界面,而服务器上有没有安装X服务器,都需要让图形程序运行在Xvfb中,由于上边的脚本中会把Xvfb启动在:10上,所以需要将系统的环境变量DISPLAY设置成:10,这样Chrome或者Firefox就会运行在Xvfb了:

export DISPLAY=:10

启动服务

sudo service Xvfb start
sudo service selenium start

启动Xvfb服务和selenium服务。

测试

$ grunt unit

或者

$ karma --single-run start karma.conf.js

来运行单元级别的测试,而

$ protractor protractor.conf.js

来运行E2E测试。

参考:

Selenium with Jenkins on Ubuntu AngularJS headless testing

nastar@ubuntu:~/galaxy-geo-web/WebContent$ cat karma
#!/bin/bash
KARMA_BIN=./node_modules/karma/bin/karma
if [ ! -x $KARMA_BIN ]; then
chmod +x $KARMA_BIN
fi
./node_modules/karma/bin/karma start "$@"
#!/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=selenium
JAVA=/usr/bin/java
PID_FILE=/var/run/selenium.pid
JAR_FILE=/usr/local/share/selenium/selenium-server-standalone-2.39.0.jar
LOG_FILE=/var/log/selenium/selenium.log
CHROME_DRIVER=/usr/local/bin/chromedriver
DAEMON_OPTS="-Xmx500m -Xss1024k -Dwebdriver.chrome.driver=$CHROME_DRIVER -jar $JAR_FILE -log $LOG_FILE"
# See this Stack Overflow item for a delightful bug in Java that requires the
# strange-looking java.security.egd workaround below:
# http://stackoverflow.com/questions/14058111/selenium-server-doesnt-bind-to-socket-after-being-killed-with-sigterm
DAEMON_OPTS="-Djava.security.egd=file:/dev/./urandom $DAEMON_OPTS"
# 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 $JAVA -- $DAEMON_OPTS
;;
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 $JAVA -- $DAEMON_OPTS
;;
*)
echo "Usage: /etc/init.d/selenium-standalone {start|stop|restart}"
exit 1
;;
esac
exit 0
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment