Headless Selenium From Your VM

Posted 2012-08-23
Written by Matt Frost
Category code

This morning I asked about installing Firefox/Chrome on CentOS server, so I could run Selenium from my VM; quite honestly, I've never interacted with Selenium before today. I was admittedly ignorant about what Selenium actually did, and even after today know that I still have a lot to learn. After an encouraging nudge from @leedavis81, I decided to try to tackle it.

Priming the Pump

My VM setup is pretty straight-forward, I run Vagrant and created a base box using CentOS 5.8 and installed all the goodies that I needed. Pretty common, so to get started; I ran the following commands.

sudo yum install java
sudo yum install Xvfb
sudo yum install firefox
If you're curious about how Firefox worked in a non-GUI environment, I was too. Continuing, I'm going on the assumption that you've already got PHPUnit, PHPUnit_Selenium set up and ready to go and that it's configured properly.

Getting Xvfb Configured

So @leedavis81 sent me a link to Stackoverflow, which had a question/answer with a link to a blog post; so I started there (and I'll post the links at the end). The problem with a lot of blog posts like that are the implicit assumptions that are made. Most people get frustrated if their setup isn't the same and they don't get the same results right off the bat. The blog post, I found was a great starting point, but it didn't quite get me to the finish line. So here's how the fine folks at alittlemadness.com got me started...(just now noticed the post was from 2008)

Xvfb :99 -ac
export DISPLAY=:99
firefox &
So a quick summary of what these lines do, the first line runs Xvfb on display number 99. This number can really be anything, I went with 99 because that is what the blog post used. Since I'm running this via Vagrant, I could have used 1...doesn't matter. This is where my first error cropped up though. I got an error stating that RGB_DB couldn't be opened and I immediately speculated that it was because I disabled all the video stuff when I set up the box. This stumped me for a little bit, until I stumbled across this little number from corpocrat.com.

I installed Xorg as the post described

sudo yum install Xorg
Then I re-ran the command from above: Xvfb :99 -ac and no error! Cool! So I ran the export command and started up Firefox. Firefox was not spectacular, but if you background it, it will just sit there waiting for something to do. So now let's recap.

You have the selenium server running, PHPUnit and PHPUnit_Selenium installed and configured, Xvfb running on the number of your choice and firefox running in the background; quite a bit going on no? What's next, well now you start running your Selenium tests and you get your feedback! After investing a bit of time in this today, I came to the realization that's really not advantageous to run it headless; but it was a geeky thing to do; and no one that I had talked to told me they had done it before.

Conclusion

I'm sure there is a use case for running Selenium this way that isn't totally crazy; but from what I've seen you're only going to get Firefox out of this equation. There are a lot of things you can verify in one browser, so again not to totally discount it. I'll probably stick to using Selenium on the Mac, but I found this to be an interesting trek into the world of Selenium. If you have a use case that fits the bill here, hopefully this is a help to you. I've linked to the blog posts I used throughout the article, so I've decided not to link them down here. corpocrat.com and alittlemadness.com were the sites though. Let me know what you think though, was this cool? a total waste of time? somewhere between?

Comments

Gravatar
Dm

2014-04-10

good work! wanted to do exactly this.. just love the fact that this can be run headless. its very exciting!

Post a comment