ryanbridges.org
21May/1315

Putting the Flash back in PhantomJS

I think PhantomJS has reached a point where it no longer needs an introduction. I've been using it more and more in recent months for a variety of tasks. Everything from automating web page snapshots to page performance analysis.  The value of this tool is immeasurable and it's headless design makes it convenient to use on the desktop or server.

But there was one place where it wasn't convenient.

In my day job, I manage a highly instrumented RIA with several Flash components. As of version 1.5, plugin support was removed from PhantomJS. This was actually a brilliant move, as this allowed it to run without dependencies on X11, which meant it could run on almost any build server out of the box without additional dependencies.

But I really, REALLY wanted to perform headless testing of the callbacks from my flash plugins.

So on a rainy Atlanta Sunday, after reading blog post after blog post lamenting the lack of plugin support in current versions of PhantomJS, I forked the repo and added it back. You can grab my fork on github. The build process is still identical to the REAL PhantomJS :-)

https://github.com/r3b/phantomjs

The --load-plugins command line option returns, and the X11 dependencies along with it.  As a test, I used the page rendering example script to make a PNG of http://www.dhs.state.il.us/accessibility/tests/flash/video.html.

The 'snap.js' script. The 3-second delay should give the flash object enough time to load.

var page = require('webpage').create();
page.open('http://www.dhs.state.il.us/accessibility/tests/flash/video.html', function () {
	window.setTimeout(function(){
        page.render('video.png');
        phantom.exit();
    },3000);
});

Run from the command line
./bin/phantomjs --load-plugins=true snap.js

Creates video.png in the current directory

video

Feel free to open a ticket on github if you find a bug that is specific to this fork.  If you have any comments, good ideas, or if you just found this useful, drop me a line. I'd love to hear about it.

Stay lucky,

ryan

UPDATE:

You will need a few extra dependencies to build and run this version.  Also note that you will need to build from the master branch, as the version tags were created on the main project before I forked.

This is the script I used to provision my build servers

apt-get -y update
apt-get --no-install-recommends -y install build-essential git-core g++
apt-get --no-install-recommends -y install openssl chrpath libssl-dev libfontconfig1-dev libglib2.0-dev libx11-dev libxext-dev libfreetype6-dev libxcursor-dev libxrandr-dev libxv-dev libxi-dev libgstreamermm-0.10-dev xvfb

For those having issues, Here are 32- and 64-bit x86 binaries built on Ubuntu 10.04 (specifically for use with the Lucid boxes from Vagrant)

http://www.ryanbridges.org/downloads/phantomjs/phantomjs--linux-i686.tar.bz2

http://www.ryanbridges.org/downloads/phantomjs/phantomjs--linux-x86_64.tar.bz2

UPDATE 2: Build information for CentOS
Commenter julian writes:

We recently built a 64 bit version for Centos. It took a long time to get it working, which in the end hinged on getting the installation in the right order.

We built from source. At the compilation stage we found that we needed to do the following:

export LIBXCB_ALLOW_SLOPPY_LOCK=1

Having built the executable it was hard to get Flash working with it. Flash needed to be installed last for this to work successfully.

The version that finally worked was:

yum install -y xorg-x11-server-Xvfb xorg-x11-server-Xorg xorg-x11-fonts* dbus-x11 xulrunner.x86_64 nspr.x86_64 nss.x86_64
yum install -y phantomjs flash-plugin nspluginwrapper libcurl

The RPM (version 1.10.0 development) is downloadable here here:
http://www.botsphere.com/rpms/phantomjs-1.9-1.x86_64.rpm

Posted by ryan

Comments (15) Trackbacks (0)
  1. This is very exciting to me, even as a PhantomJS collaborator. :)

    What OSes and architectures have you verified this on?

  2. Very cool! I’m excited about this, even as a PhantomJS collaborator. :)

    Questions:
    1. What process did you take to reinstate Flash support?
    2. Do you intend to continue merging in new commits/releases of the PhantomJS mainline?
    3. What OS and architecture configurations have you tested this on?

  3. >> The build process is still identical to the REAL PhantomJS :-)

    It seems to be not so identical. In Ubuntu i needed to install some additional packages.
    apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
    and do
    git checkout flash

    • You are correct. I wrote the article before I set up my automated build configuration. The full list of pre-reqs I install is:

      build-essential
      git-core
      g++
      openssl
      chrpath
      libssl-dev
      libfontconfig1-dev
      xvfb
      libglib2.0-dev
      libx11-dev
      libxext-dev
      libfreetype6-dev
      libxcursor-dev
      libxrandr-dev
      libxv-dev
      libxi-dev
      libgstreamermm-0.10-dev

  4. Any possibility of you compiling a binary for Ubuntu 12.04 ? I’m having a hell of a time compiling the source.

  5. Any idea how to cope with “phantomjs: cannot connect to X server”? (This is on a headless cloud instance, which is not running an X server and will not be running an X server.) All the articles online that appear to cover this are for old versions of the standard phantomjs, and reccomend running the current one.

    • For my day job, I put together a Vagrant project for them to use for automated testing. I use an upstart script to run Xvfb as a service at startup. You’ll also need to export your DISPLAY environment variable with the value of the screen below. This can be done from a chef cookbook with the magic_shell recipe.

      ########################################
      description ‘xvfb upstart script’
      author ‘ryan.bridges’

      stop on [06]

      respawn

      exec /usr/bin/Xvfb :99 -screen 0 1280x1024x24 &
      ########################################

  6. windows can you send me a binary
    thank you !!!

  7. Just a note to say thank you.

    We got this working from the instructions here, plus the following:

    apt-get install flashplugin-installer gsfonts-x11

    • We recently built a 64 bit version for Centos. It took a long time to get it working, which in the end hinged on getting the installation in the right order.

      We built from source. At the compilation stage we found that we needed to do the following:

      export LIBXCB_ALLOW_SLOPPY_LOCK=1

      Having built the executable it was hard to get Flash working with it. Flash needed to be installed last for this to work successfully.

      The version that finally worked was:

      yum install -y xorg-x11-server-Xvfb xorg-x11-server-Xorg xorg-x11-fonts* dbus-x11 xulrunner.x86_64 nspr.x86_64 nss.x86_64
      yum install -y phantomjs flash-plugin nspluginwrapper libcurl

      The RPM (version 1.10.0 development) is downloadable here here:
      http://www.botsphere.com/rpms/phantomjs-1.9-1.x86_64.rpm


Leave a comment

No trackbacks yet.