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 device. Only a network layer is necessary.

Xvfb allows GUI applications that use X Windows to run on a headless system.


Installation

Official releases are published on PyPI:

pip install xvfbwrapper

System Requirements

  • Python 3.10+
  • X Window System (or Xwayland)
  • Xvfb (sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc)
  • Support for locking with fcntl system call (non-Windows systems)

Examples

Basic Usage:

Note: Always either wrap your usage of Xvfb() with try/finally, or use it as a context manager to ensure the display is stopped. If you don't, you'll end up with a bunch of junk in /tmp if errors occur.

from xvfbwrapper import Xvfb

xvfb = Xvfb()
xvfb.start()
try:
    # launch stuff inside virtual display here
finally:
    xvfb.stop()

Usage as a context manager:

from xvfbwrapper import Xvfb

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

Specifying display geometry:

from xvfbwrapper import Xvfb

xvfb = Xvfb(width=1280, height=720)
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()

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 processes or 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 TestPage(unittest.TestCase):

    def setUp(self):
        xvfb = Xvfb()
        xvfb.start()
        self.driver = webdriver.Chrome()
        self.addCleanup(xvfb.stop)
        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 Issues

To report a bug or request a new feature, please open an issue on GitHub.


xvfbwrapper Development

  1. Fork the project repo on GitHub

  2. Clone the repo:

    git clone https://github.com/<USERNAME>/xvfbwrapper.git
    cd xvfbwrapper
    
  3. Make changes and run the tests:

    Create a virtual env and install required testing packages:

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

    Run all tests in the default Python environment::

    pytest
    

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

    tox
    
  4. Commit and push your changes

  5. Submit a Pull Request

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.15.tar.gz (9.0 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.15-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for xvfbwrapper-0.2.15.tar.gz
Algorithm Hash digest
SHA256 695a4580ab4c9f69b653629ec243a873107536845879356f98d27569c43dac7e
MD5 7f24107c68dd80266c3f30c8d773c22f
BLAKE2b-256 f74f830b1e7324facdb8d3e05008174c6b487f79b22b6869a4c58e351a4c55e0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for xvfbwrapper-0.2.15-py3-none-any.whl
Algorithm Hash digest
SHA256 47a746bc2b95ea864e9a24b71dd67e6b71a4a51fecb63c55f2c5b552e2ee56ad
MD5 a401881f84a21cc67f72f29eca606bea
BLAKE2b-256 0cd591d52203cff4ae6981a9ad9af80501de3e402b4122446f462f901d160572

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