PyVirtualDisplay: A Guide to Virtual Displays in Python

Pyvirtualdisplay is one of the most commonly used libraries to run headless tests for applications without a Graphical User Interface. Go through this article to learn more about this Python library.

What is headless testing?

This is a type of testing that exists without GUI, i.e., Graphical User Interface. In this type of testing, no interaction is needed. It is faster than the old testing methods and is used to test the following:

  • Web applications
  • APIs
  • Embedded systems
  • Mobile Applications

It has the following workflow:

  • The web application receives requests from the headless testing script.
  • The web app redirects to a webpage.
  • The test checks the script of the webpage and goes through its content.

About pyvirtualdisplay

pyvirtualdisplay is a headless testing framework. Some frameworks don’t support Graphical User Interface. The pyvirtualdisplay library can help one run tests based on the Graphical User Interface. It basically makes a false terminal. This is also referred to as pseudo-terminal. This library works on the Xvfb X server.

Installing pyvirtualdisplay

Use the pip command to install the pyvirtualdisplay library. Import it and then use the display function to create a virtual display.

pip install PyVirtualDisplay
import pyvirtualdisplay
display = pyvirtualdisplay.Display()

After running the tests and checking the working of your web application, you can close this with the help of stop() command.

display.stop()

Features of pyvirtualdisplay

The pyvirtualdisplay library of Python is all about ease. It has the following features:

  • Feasibility
  • Efficiency
  • Portability

This library is suitable for all platforms like Windows, Mac OS, and Linux. GUI-based testing frameworks, like Selenium and PyQt, can work with pyvirtualdisplay and support its functions. It’s quite versatile and doesn’t need a large number of results.

Locating elements with pyvirtualdisplay

While testing of web apps is going on, you might need to check the ID, class, name, XPath, or other such elements. Thus, locating elements is necessary. There exist a few functions through which you can locate elements on a web application. These are:

  • evaluate()
  • getElementsByName()
  • getElementsByClassName()
  • getElementById()

An ID is a unique element on a webpage. So, you may use it to locate the element you want. In the same manner, you can check for elements with the same CSS class or name attribute. The evaluate() function is used with XPath. It tells about the location of the element inside the DOM tree.

Let’s consider an example of the same. First, you need to import pyvirtualdisplay and selenium libraries. Next, you need to create a virtual display using the start() function and a webdriver instance. Provide the link to the webpage and locate the element using any of the above-mentioned functions. Use the click() function to interact and stop() at the end.

import pyvirtualdisplay
import selenium

def locate_and_click_element(url, element_id):
    """Locates and clicks on an element on a web page using pyvirtualdisplay and selenium.

    Args:
        url: The URL of the web page.
        element_id: The ID of the element to locate and click.
    """

    display = pyvirtualdisplay.Display()
    display.start()

    driver = selenium.webdriver.Chrome()
    driver.get(url)

    element = driver.find_element_by_id(element_id)
    element.click()

    display.stop()

if __name__ == "__main__":
    url = "https://www.example.com"
    element_id = "my_element"

    locate_and_click_element(url, element_id)

pyvirtualdisplay with ubuntu

If you are an Ubuntu user and wish to work with the pyvirtualdisplay library, use the first command to update the packages. Then, install the pyvirtualdisplay library using the second command. The third command lets you check whether the library has been correctly installed or not.

sudo apt update
sudo apt install python3-pyvirtualdisplay
python3 -m pip show pyvirtualdisplay

The rest of the usage is the same. For example, if you are trying to create a simple web app and do its testing, you may check the given code:

import pyvirtualdisplay

display = pyvirtualdisplay.Display()
display.start()

import webbrowser
webbrowser.open("https://www.abc.com")

display.stop()

Keystrokes with pyvirtualdisplay

Sometimes, you might want to use a web driver instance to test your web browser. This means that you can test your web browser with the help of a script without manually testing it.

You can use pyvirtualdisplay and selenium libraries to send keystrokes. Use the display() function to curate a virtual display, as explained earlier. After this, you need to make a web driver instance. This example shows Chrome driver instance creation. Specify the website and use the send_keys() function to send keystrokes. Lastly, the stop() function ends the virtual display.

The given example uses a function send_keystrokes that accepts two parameters: URL and keystrokes. It creates a script that you may use to send keystrokes.

import pyvirtualdisplay
import selenium

def send_keystrokes(url, keystrokes):
    """Sends keystrokes to a web page using pyvirtualdisplay and selenium.

    Args:
        url: The URL of the web page.
        keystrokes: The keystrokes to send.
    """

    display = pyvirtualdisplay.Display()
    display.start()

    driver = selenium.webdriver.Chrome()
    driver.get(url)

    driver.send_keys(keystrokes)

    display.stop()

if __name__ == "__main__":
    url = "https://www.abc.com"
    keystrokes = "test."

    send_keystrokes(url, keystrokes)

pyvirtualdisplay installation on Anaconda

If you are an Anaconda user, you should first activate the environment. Post this, install the package, and using the third command, check whether this package has been installed successfully.

source activate [environment_name]
conda install pyvirtualdisplay
conda list pyvirtualdisplay

The output will be in this format:

pyvirtualdisplay   3.0-py39haa244fe_3

Working with Jupyter Notebook or Spyder IDE

Now, whether it’s a Jupyter Notebook or Spyder IDE, you can create the virtual display using this library easily. Check the sample code for help:

import pyvirtualdisplay

display = pyvirtualdisplay.Display()
display.start()

# Run your  code here

display.stop()

pyvirtualdisplay selenium

If you are working with pyvirtualdisplay on selenium, you should create a backend for pyvirtualdisplay. Take the help of either xvfb or xephyr. Run the ‘sudo apt-get install xvfb xserver-xephyr’ command in your terminal.

Another thing to note is that your website may be dynamically built. Thus, it might take some time for pyvirtualdisplay to load. In this scenario, you can use the sleep function. time.sleep() works well.

There’s an alternative to the sleep() function too. The browser.implicitly_wait(20) function adds a 20-second wait time automatically to the browser. You can adjust according to your requirements and the content on the webpage.

pyvirtualdisplay mac

In order to work on Mac with pyvirtualdisplay, you need to install XQuartz first. The brew install –cask xquartz command will let you run graphical applications on a remote server.

pyvirtualdisplay xvfb

The xvfb server doesn’t require a proper browser. If you wish to utilize the pyvirtualdisplay library on xvfb server, you should first install it. The following commands can be used:

sudo apt install xvfb 
# Ubuntu/Debian
sudo yum install xorg-x11-server-xvfb 
# Fedora/Red Hat

Once this process is complete, you can create a virtual display with the help of pyvirtualdisplay.

from pyvirtualdisplay import Display

display = Display(visible=0, size=(1024, 768))
display.start()

# Run your graphical application here

display.stop()

pyvirtualdisplay colab

pyvirtualdisplay might be a bit slow on colab. This is because colab can’t be properly used to create the virtual display. You can use matplotlib or any such library to analyze the work. However, as a basic step, you should consider the installation part.

!pip install pyvirtualdisplay
!apt update && !apt install -y xvfb python-opengl ffmpeg

FAQs on Pyvirtualdisplay

How does pyvirtualdisplay work?

It creates a false terminal first and then creates a virtual display with the help of the Xvfb X server.

Conclusion

This article explains the pyvirtualdisplay library of Python in detail. Succinctly, it elaborates on the workings of this library and its functions. Also, it explains how the library works with the formation of scripts, in a colab environment, on Selenium, and on Mac.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments