Headless X11 with Xvfb

To allow builds on headless Unix systems to function correctly with Selenium, you may need to configure Xvfb (virtual framebuffer X server for X Version 11).

Use the xvfb goal to start up an Xvfb process. This will try to detect an unused X11 display starting from :20 and configure the Selenium server with that DISPLAY environment variable so that when launching browsers they startup using the virutal X11 server.

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>selenium-maven-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <id>xvfb</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>xvfb</goal>
                </goals>
            </execution>
            
            <execution>
                <id>selenium</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start-server</goal>
                </goals>
                <configuration>
                    <background>true</background>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

NOTE: For execution of Xvfb to work correctly as a non-root user, the Xvfb process needs to be SUID-root.

chmod u+s `which Xvfb`

You can also configure an explicit display to be used instead of autodetecting:

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>selenium-maven-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <id>xvfb</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>xvfb</goal>
                </goals>
                <configuration>
                    <display>:2</display>
                </configuration>
            </execution>
            
            <execution>
                <id>selenium</id>
                <phase>pre-integration-test</phase>
                <goals>
                    <goal>start-server</goal>
                </goals>
                <configuration>
                    <background>true</background>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

You may get an error like this in target/selenium/xvfb.log when you try to start Xvfb

 Fatal server error:
 PAM authentication failed, cannot start X server.
        Perhaps you do not have console ownership?

From the sdsc.edu mailing list

To allow users to start an xserver you need to modify /etc/pam.d/xserver and make it look something like this:

#%PAM-1.0
auth       sufficient   /lib/security/pam_rootok.so
#auth      required     /lib/security/pam_console.so
auth       required     /lib/security/pam_permit.so
account    required     /lib/security/pam_permit.so

This will permit authorization for users not at the console to start an X-server.