Skip to main content

Manage headless displays with Xvfb (X virtual framebuffer)

Project description

Manage headless displays with Xvfb (X virtual framebuffer)


Info:


About xvfbwrapper:

xvfbwrapper is a python module for controlling virtual displays with Xvfb.


What is Xvfb?:

Xvfb (X virtual framebuffer) is a display server implementing the X11 display server protocol. It runs in memory and does not require a physical display or input devices. Only a network layer is necessary.

Xvfb is useful for running acceptance tests on headless servers.


Install xvfbwrapper from PyPI:

pip install xvfbwrapper

System Requirements:

  • Python 3.8+

  • X Window System

  • Xvfb (sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc)

  • File locking with fcntl


Examples:

Basic Usage:

from xvfbwrapper import Xvfb

vdisplay = Xvfb()
vdisplay.start()

try:
    # launch stuff inside virtual display here.
finally:
    # always either wrap your usage of Xvfb() with try / finally,
    # or alternatively use Xvfb as a context manager.
    # If you don't, you'll probably end up with a bunch of junk in /tmp
    vdisplay.stop()

Basic Usage, specifying display geometry:

from xvfbwrapper import Xvfb

vdisplay = Xvfb(width=1280, height=740)
vdisplay.start()

try:
    # launch stuff inside virtual display here.
finally:
    vdisplay.stop()

Basic Usage, specifying display number:

from xvfbwrapper import Xvfb

vdisplay = Xvfb(display=23)
vdisplay.start()
# Xvfb is started with display :23
# see vdisplay.new_display

Usage as a Context Manager:

from xvfbwrapper import Xvfb

with Xvfb() as xvfb:
    # launch stuff inside virtual display here.
    # Xvfb will stop when this block completes

Usage in Testing: Headless Selenium WebDriver Tests:

This test class uses selenium webdriver and xvfbwrapper to run tests on Chrome with a headless display.

import unittest

from selenium import webdriver
from xvfbwrapper import Xvfb


class TestPages(unittest.TestCase):

    def setUp(self):
        self.xvfb = Xvfb(width=1280, height=720)
        self.addCleanup(self.xvfb.stop)
        self.xvfb.start()
        self.browser = webdriver.Chrome()
        self.addCleanup(self.browser.quit)

    def testUbuntuHomepage(self):
        self.browser.get('https://www.ubuntu.com')
        self.assertIn('Ubuntu', self.browser.title)

    def testGoogleHomepage(self):
        self.browser.get('https://www.google.com')
        self.assertIn('Google', self.browser.title)


if __name__ == '__main__':
    unittest.main()
  • virtual display is launched

  • Chrome launches inside virtual display (headless)

  • browser is not shown while tests are run

  • conditions are asserted in each test case

  • browser quits during cleanup

  • virtual display stops during cleanup

Look Ma’, no browser!

(You can also take screenshots inside the virtual display to help diagnose test failures)


Usage with multi-threaded execution

To run several xvfb servers at the same time, you can use the environ keyword when starting the Xvfb instances. This provides isolation between threads. Be sure to use the environment dictionary you initialize Xvfb with in your subsequent system calls. Also, if you wish to inherit your current environment you must use the copy method of os.environ and not simply assign a new variable to os.environ:

from xvfbwrapper import Xvfb
import subprocess as sp
import os

isolated_environment = os.environ.copy()
xvfb = Xvfb(environ=isolated_environment)
xvfb.start()
sp.run(
    "xterm & sleep 1; kill %1 ",
    shell=True,
    env=isolated_environment,
)
xvfb.stop()

xvfbwrapper Development: running the unit tests:

To create a virtual env and install required testing libraries:

$ python -m venv venv
$ source ./venv/bin/activate
(venv)$ pip install -r requirements_test.txt

To run all tests, linting, and type checking across all supported/installed Python environments:

(venv)$ tox

To run all tests in the default Python environment:

(venv)$ pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xvfbwrapper-0.2.10.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xvfbwrapper-0.2.10-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file xvfbwrapper-0.2.10.tar.gz.

File metadata

  • Download URL: xvfbwrapper-0.2.10.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for xvfbwrapper-0.2.10.tar.gz
Algorithm Hash digest
SHA256 d662cf8f266efd3d0a1c022eebb8d2c04083d6e87fd81392fb5400d95036ed8c
MD5 7a080624960c4bfaf68fceca1bd0d3a5
BLAKE2b-256 ac80fe0d1fa2df04d8556b8be614c66c21f476521f8e61653afae90e74f9f003

See more details on using hashes here.

File details

Details for the file xvfbwrapper-0.2.10-py3-none-any.whl.

File metadata

  • Download URL: xvfbwrapper-0.2.10-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for xvfbwrapper-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 8fcffe37d5f90579458341dd449b47947b6b241ae1daefc362edb2fea7bf7c79
MD5 6eae2cd57ab34fc8b9af58666a562cb4
BLAKE2b-256 aa53425aa7729bde3a5406e12d207446a95dcd2ad881925d13660032d1daaaf4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page