PhantomJS + Plugins + AWS + BrowserMob Proxy
What an adventure to get this working. Long story short, it was RPM hell on a standard aws instance, get the Ubuntu instance from the AWS marketplace.
The other week I needed to do some work testing flash events which are triggered during video playback. Sauce Labs is nice enough to allow us to use browsermob proxy now, but it made little sense to pay them to do this testing. They were basically going to record a video of us watching a video… That’s a good use of those minutes I tell you…
I had a machine in aws which was being utilized as a BrowserMob Proxy instance and a Sauce Connect instance. I decided I would attempt to re-purpose that machine to be a PhantomJS Grid instance which I could connect to. That was easy enough. Our base tests which relied upon the proxy worked just fine. However, the ones which required flash were not working. Makes sense, since flash support was dropped in 1.5. Then I stumbled across this post. I was excited… Then I tried to get it running.
Since this was a default aws instance, I tried the RPM which was listed. It refused to work (I tested with the small script on the blog post). I then tried to use the pre-compiled binary. Enter dependency hell. Things were not going smoothly, I wasn’t having much luck finding all of the dependencies, and they didn’t seem to live in the epel repository, which you can easily enable on a default aws instance. I wasted a day and a half trying to get everything going. I’ll be the first to admit, my Linux chops are a bit rusty, but finding what repo a dependency lives in is a chore. Adding that repo is even worse (my google fu was weak on this, and I wasn’t sure where to find a list of repo rpms or .repo files for download).
I decided to start fresh. I found this image for Ubuntu 12.04 in the AWS marketplace. I got the instance going. Added the dependencies (much easier this time), and then added flash. To get the flash installer, I simply needed to add multiverse to the security.ubuntu.com/ubuntu repo.
After starting up Xvfb and exporting the DISPLAY environment variable, I reran the snap test listed on Ryan’s blog, and lo and behold I had an image of the flash video working. Now I needed it to work as a RemoteWebDriver instance…
Sure enough it was as simple as I hoped it would be. I got my proxy up and running:
browsermob-proxy -port 9090 & curl -X POST -d 'port=9091' http://localhost:9090/proxy
Then I started the grid:
java -jar selenium-server-standalone-2.35.0.jar -role hub -port 9001 &
Then I started up phantomjs:
phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:9001 --proxy=127.0.0.1:9091 --load-plugins=yes
Then I used the following as my driver instance:
new RemoteWebDriver(new URL("http://ip.of.aws.instance:9001/wd/hub"), DesiredCapabilities.phantomjs());
To test that flash was working in the grid instance, I wrote a bit of Swing code to grab the screenshot of the remote browser, and display it. Now I can even see what the headless browser is doing while testing.
I can now make calls to the Restful API of browsermob and retrieve the har files where I can verify if my events are taking place as they should.