--- title: Headless mode slug: Mozilla/Firefox/Headless-Mode tags: - Automaisierung - Firefox - Flag - Flagge - Headless Modus - Kopflos - Mozilla - QA - Testautomatisierung - Testen - headless - headless mode - node.js translation_of: Mozilla/Firefox/Headless_mode ---
Der Headless-Mode ist eine sehr nützliche Weise auf dem der Firefox betrieben werden kann — dabei funktioniert Firefox normal, nur dass die Oberfläche nicht sichtbar ist. Das erscheint nicht besonder sinnvoll, wenn man im Web surfen möchte, aber es ist sehr nützlich für automatisierte Tests. Dieser Artikel gibt dir alle Infos, die du brauchst um Firefox im Headless-Mode laufen zu lassen.
Dieser Bereich bietet eine grunsätzliche Anleitung wie man den Headless-Mode verwendet.
Auf der Komandozeile kannst du Firefox im Headless-Mode starten indem du das -headless
Flag anhängst. Zum Beispiel:
/path/to/firefox -headless
Seit Firefox 57 ermöglicht es das -screenshot
Flag auf Webseiten im Headless-Mode Screenshots aufzunehmen. Grundsätzlich sieht das dann so aus:
/path/to/firefox -headless -screenshot https://developer.mozilla.org/
Das erzeugt einen Screenshot in voller Höhe von https://developer.mozilla.com
in das aktuelle Verzeichnis mit dem Dateinamen screenshot.png
, mit einer Viewport-Breite von 800px.
Beachte, dass du -headless
weg lassen kannst, wenn du -screenshot
angibst— es ist implizit enthalten.
/path/to/firefox -screenshot https://developer.mozilla.org/
Um die Default-Werte, die oben angegeben sind, zu überschreiben,kannst du folgende Flags/Features
verwenden:
-screenshot name url
— Setze einen beliebigen Dateinamen für den Screenshot indem du zwischen dem -screenshot
Flag und der URL diesen angibst. Beachte, dass du auch weitere web-kompatible Bildformate wie z.B. .jpg
, .bmp
, etc.--window-size=x,y
— Setze eine beliebige Bildbreite und -höhe für den Screenshot.Zum Beispiel erzeugt der folgende Befehl einen Screenshot von https://developer.mozilla.com
im aktuellen Verzeichnis mit Dateinamen test.jpg
, mit einer Breite von 800px und einer Höhe von 1000px:
/path/to/firefox -screenshot test.jpg https://developer.mozilla.org/ --window-size=480,1000
Headless Firefox works on Fx55+ on Linux, and 56+ on Windows/Mac.
The most useful way to use headless Firefox is to run automated tests with it, meaning that you can make your testing process much more efficient.
Here we'll create a Selenium test using Node.js and the selenium-webdriver
package. For this guide we'll assume that you already have basic familiarity with Selenium, Webdriver, and Node, and that you already have a testing environment set up. If you don't, work through our Setting up Selenium in Node guide first, then come back.
First, of all, make sure you've got Node installed on your system, and the selenium-webdriver
package installed, then create a new file called selenium-test.js
and follow the steps below to populate it with test code.
Note: Alternatively, you could clone our headless-examples repo; this also includes a package file so you can just use npm install
to install necessary dependencies.
Let's add some code. Inside this file, start by importing the main selenium-webdriver
module, and the firefox
submodule:
var webdriver = require('selenium-webdriver'), By = webdriver.By, until = webdriver.until; var firefox = require('selenium-webdriver/firefox');
Next, we will create a new binary
object representing Firefox Nightly, and add the -headless
argument to it so that it will be run in headless mode:
var binary = new firefox.Binary(firefox.Channel.NIGHTLY); binary.addArguments("-headless");
Now let's create a new driver instance for Firefox, and use setFirefoxOptions()
to include an options object that specifies that we want to run the test using the binary we created above (this step will be unnecessary on Linux, and after headless mode lands in the release channel on Windows/Mac, but it is still useful if you want to test a Nightly-specific feature):
var driver = new webdriver.Builder() .forBrowser('firefox') .setFirefoxOptions(new firefox.Options().setBinary(binary)) .build();
Finally, add the following code, which performs a simple test on the Google search homepage:
driver.get('https://www.google.com'); driver.findElement(By.name('q')).sendKeys('webdriver'); driver.sleep(1000).then(function() { driver.findElement(By.name('q')).sendKeys(webdriver.Key.TAB); }); driver.findElement(By.name('btnK')).click(); driver.sleep(2000).then(function() { driver.getTitle().then(function(title) { if(title === 'webdriver - Google Search') { console.log('Test passed'); } else { console.log('Test failed'); } }); }); driver.quit();
Finally, run your test with following command:
node selenium-test
That's it! After a few seconds, you should see the message "Test passed" returned in the console.
Headless Firefox in Node.js with selenium-webdriver by Myk Melez contains additional useful tips and tricks for running Node.js Selenium tests with headless mode.
Note: Thanks a lot to nicholasdipiazza for writing these instructions!
This guide assumes that you already have Geckodriver on your machine, as explained in our Setting up Selenium in Node guide, and that you have an IDE set up that supports Gradle projects.
Download our headlessfirefox-gradle.zip archive (see the source here), extract it, and import the headlessfirefox folder into your IDE as a gradle project.
Edit the build.gradle
file to set selenium to a later version if needed. At the time of writing, we used 3.5.3.
group 'com.mozilla' version '1.0' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile('org.seleniumhq.selenium:selenium-api:3.5.3') compile('org.seleniumhq.selenium:selenium-remote-driver:3.5.3') compile('org.seleniumhq.selenium:selenium-server:3.5.3') testCompile group: 'junit', name: 'junit', version: '4.12' }
Edit the webdriver.gecko.driver
property in the HeadlessFirefoxSeleniumExample.java file to equal the path where you installed geckodriver (see line 15 below).
package com.mozilla.example; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxBinary; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import java.util.concurrent.TimeUnit; public class HeadlessFirefoxSeleniumExample { public static void main(String [] args) { FirefoxBinary firefoxBinary = new FirefoxBinary(); firefoxBinary.addCommandLineOptions("--headless"); System.setProperty("webdriver.gecko.driver", "/opt/geckodriver"); FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.setBinary(firefoxBinary); FirefoxDriver driver = new FirefoxDriver(firefoxOptions); try { driver.get("http://www.google.com"); driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); WebElement queryBox = driver.findElement(By.name("q")); queryBox.sendKeys("headless firefox"); WebElement searchBtn = driver.findElement(By.name("btnK")); searchBtn.click(); WebElement iresDiv = driver.findElement(By.id("ires")); iresDiv.findElements(By.tagName("a")).get(0).click(); System.out.println(driver.getPageSource()); } finally { driver.quit(); } } }
Run the java class and you should see the HTML content of this page printed in your console/terminal.
In addition, you will be able to use headless Firefox to run automated tests written in most other popular testing apps, as long as they allow you to set environment variables.
If you are having trouble getting headless mode to work, then do not worry — we are here to help. This section is designed to be added to as more questions arise and answers are found.
If you want to ask the engineers a question, the best place to go is the #headless
channel on Mozilla IRC. If you are pretty sure you've found a bug, file it on Mozilla Bugzilla.