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)) 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()
install PyVirtualDisplay on Ubuntu/Debian:
$ sudo apt-get install xvfb python-pip $ sudo pip install pyvirtualdisplay
25 comments:
That sounds neat! I'll definitely give it a try.
I have vague memories of seeing that Jenkins has a native xvfb plugin.
For buildbot jobs, I used to wrap my test-runner command with Debian's xvfb-run wrapper.
I find the PyVirtualDisplay solution (that I didn't know about) very interesting as well. You could take a screenshot when tests fail, to figure out why that happened.
Great post! Like Marius, I find that xvfb-run java -jar selenium-server-standalone-2.0b3.jar works really well if your stuck with Selenium RC.
Very nice, I hadn't heard of Selenium before - looks very cool, especially as it can be paired with xvfb.
Try xvnc plug-in for Jenkins
This code doesn't work for me on OS X Snow Leopard, I see the Firefox window.
Is there something other than PyVirtualDisplay and Selenium I need to install?
I agree with John this doesn't work for me on OS X 10.6. It still opens like normal.
John, Jason,
pyvirtualdisplay has some dependencies (xvfb, etc). I show how to satisfy them on Ubuntu/Debian. You are on your own for Mac.
hth,
-Corey
Just wanted to say this was quite helpful for me, was struggling to find docs on how to run it headless. Works with no probs, Debian wheezy.
Thanks Corey! This was a big help!
Just tested this. Exactly the solution I'm looking for! Great stuff!
thanks, work great for me. Great tip
You should try http://testutils.org/sst/index.html
yay for SST :)
I wrote the blog post while implementing this feature in SST.
Give a try to Simple Selenium Test: http://testutils.org/sst/index.html
Yes, I know :P
Thanks, works 100% under Ubuntu 12.10 and so needed this today
I am just trying this under Redhat and this seems to stall at the
browser = webdriver.Firefox()
suggestions please
@avisual
can you run it *with* display turned on?
can you launch firefox from a command prompt without error? is firefox installed? is selenium installed? do you get any stack trace?
not a mind reader :)
Hi,
sorry yes it turns out that selenium will not run Firefox from a script and I needed to pass in the location of the binary
ffbin = webdriver.firefox.firefox_binary.FirefoxBinary('/usr/lib/firefox/firefox')
self.driver = webdriver.Firefox(firefox_binary=ffbin)
I then ran into another bug or location problem really
http://code.google.com/p/selenium/issues/detail?id=3049
I did the ugly symbolic link and everything was fine.
Thanks very much for the post.
I hope this helps the next person that reads your post
I had to install EasyProcess to get pyvirtualdisplay working.
sudo pip install EasyProcess
BTW very nice tutorial.
How is this headless if you are getting the screenshot by using firefox?
@teja,
Firefox *is* running, just inside a virtual x server. It is not displayed. So it is headless in that sense. You can run it from a server with no display attached.
Lovely, dude, lovely.... Just what I was in need of.
Post a Comment