I\'m on EC2 instance. So there is no GUI.
$pip install selenium
$sudo apt-get install firefox xvfb
Then I do this:
$Xvfb :1
This is the setup I use:
Before running the tests, execute:
export DISPLAY=:99 /etc/init.d/xvfb start
And after the tests:
/etc/init.d/xvfb stop
The init.d
file I use looks like this:
#!/bin/bash XVFB=/usr/bin/Xvfb XVFBARGS="$DISPLAY -ac -screen 0 1024x768x16" PIDFILE=${HOME}/xvfb_${DISPLAY:1}.pid case "$1" in start) echo -n "Starting virtual X frame buffer: Xvfb" /sbin/start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS echo "." ;; stop) echo -n "Stopping virtual X frame buffer: Xvfb" /sbin/start-stop-daemon --stop --quiet --pidfile $PIDFILE echo "." ;; restart) $0 stop $0 start ;; *) echo "Usage: /etc/init.d/xvfb {start|stop|restart}" exit 1 esac exit 0
open a terminal and run this command xhost +
. This commands needs to be run every time you restart your machine. If everything works fine may be you can add this to startup commands
Also make sure in your /etc/environment file there is a line
export DISPLAY=:0.0
And then, run your tests to see if your issue is resolved.
All please note the comment from sardathrion below before using this.
If you use Maven, you can use xvfb-maven-plugin to start xvfb before tests, run them using related DISPLAY
environment variable, and stop xvfb after all.
You can use PyVirtualDisplay (a Python wrapper for Xvfb) to run headless WebDriver tests.
#!/usr/bin/env python
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
# now Firefox will run in a virtual display.
# you will not see the browser.
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()
more info
You can also use xvfbwrapper, which is a similar module (but has no external dependencies):
from xvfbwrapper import Xvfb
vdisplay = Xvfb()
vdisplay.start()
# launch stuff inside virtual display here
vdisplay.stop()
or better yet, use it as a context manager:
from xvfbwrapper import Xvfb
with Xvfb() as xvfb:
# launch stuff inside virtual display here.
# It starts/stops in this code block.
The easiest way is probably to use xvfb-run:
DISPLAY=:1 xvfb-run java -jar selenium-server-standalone-2.0b3.jar
xvfb-run does the whole X authority dance for you, give it a try!