Posted

tl;dr Firefox Nightly on Linux supports running SlimerJS headlessly. More platforms and full headless Firefox are coming soon.

Over the last couple of years, I’ve worked on a few big web projects like PDF.js and PluotSorbet where I’ve wanted a better way to run automated tests in Firefox. My usual workflow would either involve opening a test page in Firefox manually, using a homegrown automation script that would pop open Firefox and steal focus as it ran tests, or fighting with XVFB to run Firefox in the background. I missed the days of running C++ or Node unit tests directly from the command line and not having some new window open. If only Firefox supported a headless mode…

Headless Firefox had been in the back of my mind for awhile, so when January rolled around this year and my previous project was winding down, I was happy to hear I’d be working on headless Firefox. It also turns out, I wasn’t the only person who wanted a headless mode for Firefox. We heard from multiple organizations and developers that they wanted an easier way to test Firefox and there was even a feature request nine years ago with quite a bit of following. For the first phase of headless browsing we decided to target SlimerJS, since it is a simpler application based on Firefox, it comes with an API for controlling the browser, and developers are already using it for testing. Fast forward a few months, and I’m happy to announce the I’ve finished the first big milestone by getting SlimerJS to run completely headlessly with Firefox Nightly on Linux.

If you’re on Linux and want to try it out:

  1. Download and install Firefox Nightly.
  2. Get the SlimerJS source. Note: once the changes land in a stable release you can just follow the regular download instructions for SlimerJS.
    git clone https://github.com/laurentj/slimerjs.git
    cd slimerjs
    
  3. Modify src/application.ini to bump the version of Firefox supported.
    --- a/src/application.ini
    +++ b/src/application.ini
    @@ -8,7 +8,7 @@ Copyright=Copyright 2012-2017 Laurent Jouanneau & Innophi
     
     [Gecko]
     MinVersion=38.0.0
    -MaxVersion=52.*
    +MaxVersion=55.*
     
     [XRE]
     EnableExtensionManager=1
  4. Create a SlimerJS test script. For example snapshot.js:
    var webpage = require('webpage').create();
    webpage
      .open('http://mozilla.com')
      .then(function () {
        // store a screenshot of the page
        webpage.viewportSize = { width: 650, height: 320 };
        webpage.render('page_snapshot.png',
                       { onlyViewport: true });
        slimer.exit();
      });
    
  5. Test it out. Make sure to set the MOZ_HEADLESS environment variable and change the SLIMERJSLAUNCHER path to point to wherever you installed Firefox nightly
    MOZ_HEADLESS=1 SLIMERJSLAUNCHER=/home/bdahl/nightly/dist/bin/firefox ./src/slimerjs snapshot.js

 

Next up, I’ll be working on getting Firefox to run headlessly without SlimerJS and instead being controlled by either WebDriver (Selenium) or maybe a devtools protocol (to be decided) and on getting headless working on MacOS and Windows. For more updates, follow along in the headless browsing meta bug and in SlimerJS’s bug.

Comments

  1. Simon

    Definitely interested in seeing a more generalised headless support for testing, avoiding the need for dummy X servers to run stuff in. This is especially true for WebDriver, but also for any test framework that can be invoked simply by starting a browser instance pointed at a URL…

    Reply
  2. Javo

    Hi Brendan, great work! I needed this feature since I was using XVFB and was not very stable.
    I tried this out and seems to work ok, but sometimes Firefox crashes.
    (Gdk-CRITICAL **: gdk_x11_display_get_xdisplay: assertion ‘GDK_IS_DISPLAY (display)’ failed)
    I’m following the bug (id 1356681) you are working on , that as I understand takes into account this case.
    Do you know when the nightly release will include your patch?
    Thanks for the work you are doing!

    Reply
    • xyrka

      Hi Java, sorry I missed your comment. Things have progressed quite a bit so hopefully your issue has been fixed, but please open a bug under Firefox->Headless if you’re still having issues.

      Reply
  3. CMDR

    Hi, thx for the great work on Firefox headless. During my tests, headless Firefox performed better (meaning closer to the non-headless state) than headless Chrome. Chrome headless has difficulties on some javascript-based sites – although Chrome GUI version could process the page correctly.

    One question: its nice to be able to save a screenshot of a page via -screenshot cmd line option. But I am missing a command line option to save the page source to a file, which in many cases might be more important.

    Is there a (maybe undocumented) cmd line option for that function, or will it be implemented in future releases?

    Thanks….

    Thanks

    Reply
      • CMDR

        Thx for quick reply…. OK then I probably will go for a feature request. I already thought of building my own personal Firefox version by adding that cmd line switch to the code….

        As an advantage, the page source can be checked programmatically; screenshots are better for manual/visual layout control. Hopefully the feature will be added.

        Thanks again…

        Reply
        • Bob M

          CMDR
          Of course I’m way late on this but I could not find any other references. Just wondering if you ever found a solution to downloading the source of a web page with FF headless?
          If so please point me into the right direction?
          Thanks

          Reply

Trackbacks/Pingbacks

  1.  Headless Firefox | Mykzilla
  2.  Headless SlimerJS with Firefox – Javascript World
  3.  Headless Firefox in Node.js with selenium-webdriver | Mykzilla
  4.  Headless mode is a useful way to run Firefox – ITantz.com
  5.  Using Headless Mode in Firefox – Mozilla Hacks – the Web developer blog

Leave a Reply

Your email address will not be published. Required fields are marked *