Headless testing with Firefox

Last week I wanted to show you how to run Selenium tests using headless Firefox. Unfortunately my Browser Capabilitiesturned out to be outdated so I opted for correct Firefox Selenium Browser Capabilities explanation instead.

It turned out that most of the work was done already and I just needed few minutes to run Firefox in headless mode. Here is how I did it.

Getting started - requirements

According to official Mozilla guideyou need Firefox 55+ on Linux and Firefox 56+ on Windows/Mac.

I’m currently using Windows, so I had to download Firefox 56 beta first (stable version is 55).

Headless Firefox configuration

My code from last week was almost sufficient. I only needed to add one option (–headless). Configuration is almost identical as in Chrome.

    @Override
        public WebDriver newWebDriver() {
        return new FirefoxDriver(getFirefoxCapabilities());
    }

    private DesiredCapabilities getFirefoxCapabilities() {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, getFirefoxOptions());
        return capabilities;
    }

    private FirefoxOptions getFirefoxOptions() {
        FirefoxOptions options = new FirefoxOptions();
        options.addArguments("--headless");
        return options;
    }

Demo

As usual, I prepared quick demo for my readers. I run it few times in headless/non-headless mode on my local machine and didn’t observe any time differences. Still the main benefit of headless testing is ease of use on Unix machines. We don’t have to install xvfb and other tools related to graphic environment.

Demo is testing that only 5 posts is displayed on my homepage.

PageObject code:

@PageUrl("http://www.awesome-testing.com")
public class AwesomeTestingPage extends FluentPage {

    @FindBy(css = "[alt='Awesome Testing']")
    private FluentWebElement logo;

    @FindBy(css = "h1.post-title")
    private FluentList<FluentWebElement> postTitle;

    @Override
    public void isAt() {
        await().atMost(5, TimeUnit.SECONDS).until(logo).displayed();
    }

    public void assertThatCorrectNumberOfPostsIsDisplayed(int numberOfPosts) {
        await().until(postTitle).displayed();
        assertThat(postTitle.size()).isEqualTo(numberOfPosts);
    }
}

Class in which I configure Firefox browser:

public class FirefoxManipulator extends FluentTestNg {

    private static final String MY_GECKO_PATH = "C:\\drivers\\geckodriver.exe";

    @BeforeTest
    public void setUp() {
        if (SystemUtils.IS_OS_WINDOWS) {
            System.setProperty("webdriver.gecko.driver", MY_GECKO_PATH);
        }
    }

    @Override
    public WebDriver newWebDriver() {
        return new FirefoxDriver(getFirefoxCapabilities());
    }

    private DesiredCapabilities getFirefoxCapabilities() {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, getFirefoxOptions());
        return capabilities;
    }

    private FirefoxOptions getFirefoxOptions() {
        FirefoxOptions options = new FirefoxOptions();
        options.addArguments("--headless");
        return options;
    }
}

Actual test code:

public class HeadlessFirefoxDemoTest extends FirefoxManipulator {

    private static final int EXPECTED_NUMBER_OF_POSTS = 5;

    @Page
    private AwesomeTestingPage awesomeTestingPage;

    @Test
    public void correctNumberOfPostsShouldBeDisplayed() {
        goTo(awesomeTestingPage)
                .assertThatCorrectNumberOfPostsIsDisplayed(EXPECTED_NUMBER_OF_POSTS);
    }

}

As usual, I posted all the code from above in myGitHub Awesome Testing project.

Tags:

Categories:

Updated: