How to properly configure the Selenium Maven Plugin to work with Xvfb to run headless How to properly configure the Selenium Maven Plugin to work with Xvfb to run headless selenium selenium

How to properly configure the Selenium Maven Plugin to work with Xvfb to run headless


It turns out the 'start-server' and 'stop-server' goals are for starting/stopping SeleniumRC servers. This is NOT what I wanted, as all my tests use the WebDriver API instead.

Apparently the 'xvfb' goal in the pom DOES start an Xvfb session during the specified lifecycle phase - I guess I just didn't see it before. And in it's configuration, you specify where to write a props file detailing which display Xvfb is running on. In the Java code, this file can be read and the value passed to the FirefoxBinary used when creating the WebDriver.

The relevant pom.xml bits are as follows:

<properties>    <displayProps>target/selenium/display.properties</displayProps></properties><build>    <plugins>        <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>            <configuration>                <systemPropertyVariables>                    <display.props>${displayProps}</display.props>                </systemPropertyVariables>            </configuration>        </plugin>        <plugin>            <groupId>org.codehaus.mojo</groupId>            <artifactId>selenium-maven-plugin</artifactId>            <version>2.3</version>            <executions>                <execution>                    <id>xvfb</id>                    <phase>test-compile</phase>                    <goals>                        <goal>xvfb</goal>                    </goals>                    <configuration>                        <displayPropertiesFile>${displayProps}</displayPropertiesFile>                    </configuration>                </execution>             </executions>          </plugin>    </plugins></build>

This starts Xvfb on the first free display (:20 or above) and writes the value to the props file that I read and use later in my Java code.

String xvfbPropsFile = System.getProperty("display.props");FirefoxBinary ffox = new FirefoxBinary();ffox.setEnvironmentProperty("DISPLAY", /*read value from xvfbPropsFile*/);WebDriver driver = new FirefoxDriver(ffox);

Now the driver will be in control of the Firefox instance spun up in the appropriate display. Voila!