Skip to main content

Manage headless displays with Xvfb (X virtual framebuffer)

Project description

xvfbwrapper

Manage headless displays with Xvfb (X virtual framebuffer)


About

xvfbwrapper is a python module for controlling X11 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 programs that run on a headless servers, but require X Windows.


Installation

pip install xvfbwrapper

System Requirements

  • Python 3.9+
  • 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

xvfb = Xvfb()
xvfb.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
    xvfb.stop()

Specifying display geometry:

from xvfbwrapper import Xvfb

xvfb = Xvfb(width=1280, height=740)
xvfb.start()
try:
    # launch stuff inside virtual display here
finally:
    xvfb.stop()

Specifying display number:

from xvfbwrapper import Xvfb

xvfb = Xvfb(display=23)
xvfb.start()
# Xvfb is started with display :23
# see vdisplay.new_display
try:
    # launch stuff inside virtual display here
finally:
    xvfb.stop()

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

Multithreaded execution:

To run several Xvfb displays 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 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:

import os

from xvfbwrapper import Xvfb

isolated_environment1 = os.environ.copy()
xvfb1 = Xvfb(environ=isolated_environment1)
xvfb1.start()

isolated_environment2 = os.environ.copy()
xvfb2 = Xvfb(environ=isolated_environment2)
xvfb2.start()

try:
    # launch stuff inside virtual displays here
finally:
    xvfb1.stop()
    xvfb2.stop()

Usage in testing - headless Selenium WebDriver tests:

This is a test using selenium and xvfbwrapper to run tests on Chrome with a headless display. (see: selenium docs)

import os
import unittest

from selenium import webdriver
from xvfbwrapper import Xvfb

# force X11 in case we are running on a Wayland system
os.environ["XDG_SESSION_TYPE"] = "x11"


class TestPages(unittest.TestCase):

    def setUp(self):
        xvfb = Xvfb()
        self.addCleanup(xvfb.stop)
        xvfb.start()
        self.driver = webdriver.Chrome()
        self.addCleanup(self.driver.quit)

    def test_selenium_homepage(self):
        self.driver.get("https://www.selenium.dev")
        self.assertIn("Selenium", self.driver.title)


if __name__ == "__main__":
    unittest.main()
  • virtual display is launched
  • browser launches inside virtual display (headless)
  • browser quits during cleanup
  • virtual display stops during cleanup

xvfbwrapper Development

Clone the repo:

git clone https://github.com/cgoldberg/xvfbwrapper.git
cd xvfbwrapper

Create a virtual env, install required testing packages, and run all unit tests in the default Python environment::

python -m venv venv
source ./venv/bin/activate
pip install --editable --group dev --group test .
pytest

Run all unit tests, linting, and type checking across all supported/installed Python environments:

tox

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.14.tar.gz (8.5 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.14-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xvfbwrapper-0.2.14.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xvfbwrapper-0.2.14.tar.gz
Algorithm Hash digest
SHA256 f6e42ae827e308c211f60db3c9301be6d6849701f2de1060954c9dc53d9ebf6b
MD5 f576bba333fa99647bc5df676a957814
BLAKE2b-256 38c5ee92c2d42326c1fde5cb7ba962f7918140fc54df408b10a0782fddae33d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xvfbwrapper-0.2.14-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for xvfbwrapper-0.2.14-py3-none-any.whl
Algorithm Hash digest
SHA256 e5ec86870455fd3dd38cdccb72b4362cdf0272d9de282e73f58cba03439321b9
MD5 db7c8a9784d2ba82c35b7b8579864504
BLAKE2b-256 8738d0dab4dca8bc8e33b9404417c590ca792145dbe58e146cf0c2d1f182ace9

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