Headless Browser Testing With Xvfb

Jan 09 ’15·javascripttesting

These days, when the phrase "headless browser" is mentioned, you immediately think of PhantomJS, but - there are alternatives. In this article, I am going to introduce a nice alternative you can use on Linux which allows you to run real browsers - Firefox and Chrome, headless - using Xvfb.

What's Wrong With PhantomJS?

PhantomJS is great tool and is widely used by companies and developers around the world. It has particularly been widely adopted for running JavaScript test suites. However, using PhantomJS isn't without drawbacks. Although it is built on top on WebKit - the same rendering engine both Safari and Chrome use, it still behaves in subtly different ways from those browsers, which can cause false positives (tests failing when there is not a real defect) and in some cases performance degradation of your test suite. Since PhantomJS isn't a real browser in the sense that no end-user actually use it, fixing these issues specifically for PhantomJS becomes an upkeep cost of the test suite, rather than something that directly improves the product. Getting developers to write tests consistently is hard enough, but if maintaining the tests becomes annoying - which false positives definitely are - it can become demoralizing.

Run Real Browsers Headless

Because of the above reasons, at one point in my career I decided to use real browsers only to run JavaScript test suites. This has meant opening up a separate browser window to run the tests during development which I would simply hide in the background. I am okay with this. However, in many cases the continuous integration servers we use are Linux servers, and do not have displays. This is where Xvfb comes in.

Xvfb is a virtual display framebuffer for X - the display system used by Linux. It provides a fake display buffer for graphical programs to write to, thus allowing any program to run headlessly.

Installing Xvbf

If you are running Debian or Ubuntu and are using apt-get, installing is simply as:

apt-get install xvfb

If you are on CentOS and using yum, it's

yum install xorg-X11-server-Xvfb

Installing the Browsers

On Ubuntu, I found installing Chrome and Firefox painless.

  • For Chrome simply go to the Chrome download page, download the .deb file, then do dpkg -i <the path to the .deb file> to install.
  • For Firefox, it's just apt-get install firefox.

On CentOS, I have had more trouble.

  • For starters, Chrome is not supported on CentOS and I haven't even found a simply way to build Chrominum and I gave up.
  • Installing firefox also had a little hiccup: I ran yum install firefox, but then running firefox resulted in an error, which I solved by also installing gdk-pixbuf2 via yum install gdk-pixbuf2.

If you are on other Linux distros I haven't mentoned, checkout the Chrome download page and the Firefox on Linux page.

Xvfb Usage

The usage of Xvfb is:

xvfb-run <some command>

That's all you need to know! For example, you can run Firefox within Xvfb:

xvfb-run firefox http://google.com

This should just start a firefox process in the background. You may see a cryptic warning in the terminal, but otherwise it's not very exciting because you don't actually see anything.

If you use Testem to run tests, you can do:

xvfb-run testem -l firefox

to run your tests in Firefox or

xvfb-run testem -l chrome

to run them in Chrome - headless!

Of course, this is not limited to Testem, you can use xvfb-run with Karma, Grunt, Gulp or any other test runner that spawns a real browser and it will just work.

Implications

If you use Linux for development, this is an attractive solution. It allows you to run your tests without having an unwanted window and it's easy to setup and use. However, even if you don't use Linux for development, you may still use Linux for continuous integration, in which case, maybe you are currently using PhantomJS to run tests. If that's working well for you, great. But if PhantomJS is starting to create unwanted overhead for you, consider switching to this setup.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值