June 9, 2011

Python - Headless Selenium WebDriver Tests using PyVirtualDisplay

I need to integrate my functional UI tests (Selenium/WebDriver) with my Jenkins CI system. The problem is that my Jenkins CI server has no display, so I must run my GUI tests in a headless X-server.

A colleague pointed me to PyVirtualDisplay, a Python wrapper for Xvfb and Xephyr.

This makes running headless Python Selenium/WebDriver tests very easy.

Here is some Python code showing WebDriver with a virtual display provided by Xvfb:

#!/usr/bin/env python

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))

# now Firefox will run in a virtual display. 
# you will not see the browser.
browser = webdriver.Firefox()
print browser.title


install PyVirtualDisplay on Ubuntu/Debian:

$ sudo apt-get install xvfb python-pip
$ sudo pip install pyvirtualdisplay

June 8, 2011

Python - Selenium WebDriver - Capture Screenshot

Example of capturing a screenshot from Selenium WebDriver in Python:

#!/usr/bin/env python
from selenium import webdriver

browser = webdriver.Firefox()

selenium/webdriver bindings for Python: http://pypi.python.org/pypi/selenium