Jenkins. Running BDD Tests with WebDriver Support

This article is a continuation of Debian. A Jenkins Continuous Integration Server for Continuous Testing of Django Projects, but today I’ll explain how to run BDD tests using Lettuce/Selenium/WebDriver for Django projects. We’ll still be using a Debian-based distribution.
Installing the Virtual Framebuffer ‘fake’ X server
To run WebDriver, we’ll need FireFox (or IceWeasel), or Chrome (or Chromium). But servers usually don’t have the X11 graphical server installed, so we’ll need to install xvfb.
Xvfb or X virtual framebuffer is a display server implementing the X11 display server protocol. In contrast to other display servers Xvfb performs all graphical operations in memory without showing any screen output. http://en.wikipedia.org/wiki/Xvfb
You can do that like this:
sudo apt-get install xvfb
On FreeBSD, you can install it from a port:
cd /usr/ports/x11-servers/xorg-vfbserver make install cleanYou may also find my modified script for launching xvfb useful; you can view it here https://github.com/adw0rd/xvfb-run/blob/master/freebsd/xvfb-run
You can also read Linux: Running Graphical Applications in the Background.
I also recommend taking a look at xvfbwrapper for Python. We won’t need it, but it’s a useful package.
Installing FireFox
Now let’s install FireFox. The page at http://mozilla-russia.org/products/firefox/linux.html has instructions for various Linux distributions, but we’re interested in the Debian section, where you’ll find the instructions you need:
Add the following repositories to /etc/apt/sources.list, or create a file in /etc/apt/sources.list.d/ and add them there:
deb http://backports.debian.org/debian-backports squeeze-backports main
deb http://mozilla.debian.net/ squeeze-backports iceweasel-release
Now update the package database and install IceWeasel:
apt-get update
apt-get install -t squeeze-backports iceweasel
On Ubuntu, I simply install Firefox:
apt-get install firefox
Installing Chrome
Personally, this is the only browser I use, because it runs faster, supports modern technologies, and, in my opinion, has better Developer Tools.
Let’s add the following to “/etc/apt/sources.list”:
deb http://dl.google.com/linux/chrome/deb/ stable main
Add the keys and install google-chrome-stable:
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo apt-get update
sudo apt-get install google-chrome-stable
You can read Installing Google Chrome on Debian
Installing ChromeDriver
Go to http://chromedriver.storage.googleapis.com/index.html (this is where the official link from https://code.google.com/p/chromedriver/wiki/WheredAllTheDownloadsGo leads) and choose the appropriate version.
I chose “2.10” (the latest version at the time of writing); now let’s install it:
sudo su - jenkins
cd ~
wget http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
Now you can move the “chromedriver” binary to /usr/local/bin (and don’t forget to grant execute permissions to everyone else with “o+x”), or put it in the home directory of the user you’ll be running the tests as (for example, jenkins):
mkdir ~/bin
mv chromedriver ~/bin
echo "export PATH=$PATH:$HOME/bin" >> ~/.bash_profile
There are also instructions on the Splinter page: Chrome WebDriver
Run chromedriver and check that it works:
chromedriver
Starting ChromeDriver (v2.10.267518) on port 9515
Only local connections are allowed.
Press Ctrl+C.
Adding and Configuring a Jenkins Job
Click New Job (or “New Item” in newer versions of Jenkins), select Build a free-style software project, and name the job whatever you like, for example “Testing_Project”.
In the “Source Code Management” section, select the “Git Repositories” checkbox. Enter git@git.example.org:repo-name.git in “Repository URL”, and specify the branch you want in “Branches to build”, for example “master”.
Now let’s configure "Poll SCM" > "Schedule". We’ll check the repository for changes every 2 minutes; if there are changes, a new build with tests will be triggered:
H/2 * * * *
In the Build Environment section, select:
- “Add timestamps to the Console Output”
- “Color ANSI Console Output” (I select “xterm” in the dropdown that appears)
Now for the most important part: add a section for running the tests "Build" > "Add build step" > "Execute shell":
virtualenv --no-site-packages --distribute venv
# Устанавливаем зависимости для проекта и тестов
venv/bin/pip install --upgrade -r requirements/base.txt
venv/bin/pip install --upgrade -r requirements/tests.txt
# Запускаем тесты с использованием lettuce
venv/bin/python ./manage.py harvest -d --apps=tests --verbosity=4 --settings=test_settings
mkdir -p reports
xvfb-run -l -s '-screen 0 1280x1024x16 -ac' venv/bin/python ./manage.py harvest --apps=example.tests --verbosity=3 --settings=test_settings --with-xunit --xunit-file=./reports/junit-lettuce.xml example/tests/features/*.feature
That’s really all there is to it—now we can run full-fledged functional BDD tests!
Comments are closed. Read the original discussion in Russian →