23

I'm using Amazon Linux. I want to run a headless chromium browser to use on my node.js Selnium tests. So I fire up my Xvfb server like so ...

if ! pidof /usr/bin/Xvfb; then Xvfb :0 -screen 5 1024x768x8 & fi

However, when I try and take a screenshot after my tests have failed,

DISPLAY=:0 import -window root /tmp/screenshot.png

I get the error mentioned below...

+ DISPLAY=:0
+ import -window root /tmp/screenshot.png
import: unable to open X server `:0' @ error/import.c/ImportImageCommand/369.

How do I take a screenshot of Xvfb buffer?

Rakib Fiha
  • 580
  • 3
  • 9
  • 24
Dave
  • 2,348
  • 21
  • 54
  • 84
  • 2
    Your example works fine here (debian 9). You can try `xwd` and `xwud` instead of `image`, works here, too. Maybe check again whether Xvfb is still running, and running on :0? – mviereck May 15 '17 at 23:48
  • Do you hvea an example of what you're talking about -- e.g. using another tool besides imagemagick? – Dave May 16 '17 at 13:42
  • 3
    it is similar to your example. `xwd -display :0 -root -out /tmp/pic` takes a snapshot, and `xwud -in /tmp/pic` shows it. – mviereck May 16 '17 at 15:02
  • Wrks on RHEL/CentOS 7 too. @mviereck you must write an answer. – akostadinov Apr 19 '19 at 11:50
  • I see an error says unable to open X server, it should be a problem that the X server itself is not reachable – Dickens A S Apr 23 '19 at 11:21

1 Answers1

9

I tried something similar (on another distribution and to take a screenshot of an xterm window, but this should not be much different) I had to use display :1 as display :0 is already used :

$ Xvfb :1 -screen 5 1024x768x8 &
[1] 23728
$ pidof /usr/bin/Xvfb
23728
$ DISPLAY=:1 xterm 2>/dev/null &
[2] 23767
$ DISPLAY=:1 xwd -root -silent | convert xwd:- png:/tmp/screenshot.png
$

And I have a "/tmp/screenshot.png" file with the expected xterm window on a black background. You can also use

xwd -display :1 -root -silent | convert xwd:- png:/tmp/screenshot.png
Nathael Pajani
  • 314
  • 2
  • 6
  • 2
    Debian/Ubuntu: To install `xwd` and `convert`, run: `apt-get install x11-apps imagemagick` – kenorb Jun 22 '21 at 23:45