Introduction
This article focuses on a common issue when working with Xvfb (X Virtual FrameBuffer) sessions and taking screenshots: not seeing the cursor. This guide will walk you through the process of setting up Xvfb sessions, as well as discuss various methods to take screenshots while addressing this specific challenge.
Xvfb Sessions
Xvfb, or X Virtual FrameBuffer, is a display server that creates a virtual screen, which is not tied to a physical graphics device. Developers use Xvfb sessions for testing, Continuous Integration (CI), and other purposes where having a headless X server is beneficial.
Taking Screenshots
When working with Xvfb sessions, taking screenshots might prove challenging, especially when the cursor is not visible. In this section, we'll discuss common tools for capturing screenshots and demonstrate possible solutions for addressing the cursor's absence.
xwd
xwd (X Window Dump) is a utility for capturing screenshots of X windows. Although it works well for capturing the content of the window, it does not capture the cursor by default.
# Run Xvfb session
Xvfb :99 -screen 0 1920x1080x24 &
# Run application within Xvfb
export DISPLAY=:99
your_application &
# Capture screenshot with xwd
xwd -root -out out.xwd
import
import, a utility similar to xwd, also suffers from the same issue of not capturing the cursor.
# Capture screenshot with import
import -window root out.png
Scrot
Scrot (SCReen ShOT) is a command-line screen capturing utility that is an improvement over xwd and import. By default, scrot also does not capture the cursor. However, it allows the inclusion of the cursor via the -c or --cursor flag.
# Install scrot (Ubuntu, for example)
sudo apt-get install scrot
# Capture screenshot with scrot, including the cursor
scrot -c out.png
imagemagick
imagemagick is a powerful image manipulation library. We can use it in combination with xwd to capture screenshots with the cursor.
# Install imagemagick (Ubuntu, for example)
sudo apt-get install imagemagick
# Capture screenshot with xwd and add the cursor
xwd -root -out out.xwd && convert out.xwd -gravity NorthWest -draw 'image over 0,0 0,0 "cursor.png"' out.png