Question

I made some unit-tests using fest, now I need to run mvn my test using maven on a headless system. I would like to run test using Xvfb but I need help to configure maven to start Xvfb before testing and stop it when all is done.

Was it helpful?

Solution 2

Actually I use this plugin configuration:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>start-xvfb</id>
            <phase>process-test-classes</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo message="Starting xvfb ..." />
                    <exec executable="Xvfb" spawn="true">
                        <arg value=":1" />
                    </exec>
                </tasks>
            </configuration>
        </execution>
        <execution>
            <id>shutdown-xvfb</id>
            <phase>test</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo message="Ending xvfb ..." />
                    <exec executable="killall">
                            <arg value="Xvfb" />
                    </exec>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>

The good is that you get a background process (with spawn = "true") and you can kill the Xvfb process (with killall) without having to write any script. Moreover in my Ubuntu distro I don't hav to set any special setting in my sudoers file to let this works.

The shutdown-xvfb execution is executed at the end of the test phase but (here is the problem) is not executed if the test fail. This is not a problem if you want to restart another test (the old Xvfb is still running and the new one can't run, but this is not a problem) but the problem is that the resources are still busy by Xvfb. A workaround for this could be to add testFailureIgnore = "true" to the configuration of maven-surefire-plugin but this way I can't easily see if some test fail.

OTHER TIPS

With the exec-maven-plugin:

You'll have to define two executions, one for starting your server and one for stopping it. You'll have to tie those execution configurations with the appropriate maven phase -- start the Xvfb before your test phase, and stop Xvfb after your test phase.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>

            <executions>
                <execution>
                    <id>exec-at-test-compile</id>
                    <phase>test-compile</phase> <!-- runs right before 'test' -->
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>/home/anew/bin/manage-xvfb.sh</executable>
                        <arguments>
                            <argument>start</argument>
                        </arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>exec-at-prepare-package</id>
                    <phase>prepare-package</phase> <!-- runs right after 'test' -->
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>/home/anew/bin/manage-xvfb.sh</executable>
                        <arguments>
                            <argument>stop</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Here is the content of the manage-xvfb.sh script:

#!/bin/bash

XVFB_CMD="sudo /usr/bin/Xvfb :15 -ac -screen 0 1024x768x8"

function stop_xvfb {
  XVFB_PID=`ps ax | pgrep "Xvfb"`
  if [ "${XVFB_PID}" != "" ]; then
    sudo kill ${XVFB_PID}
  fi
}

if [ "${1}" == "start" ]; then
  stop_xvfb
  ${XVFB_CMD} &
elif [ "${1}" == "stop" ]; then
  stop_xvfb
fi 

Note that you'll need to have NOPASSWD set in your sudoers file.

This job can easily be done via the selenium-maven-plugin:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>selenium-maven-plugin</artifactId>

  <executions>
    <execution>
      <id>setup-xvfb</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>xvfb</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Unfortunately, this plugin seems quite unmaintained. It was part of the mojohaus project which recently migrated from codehaus.org to github.com. It seems no one has yet ported the selenium-maven-plugin to github and thus sources and documentation is currently not available on mojohaus' github page.

However, the JARs are available on Maven Central and the plugin can still be used. There is a fork of the sources and site available on github here, if you need to lookup some more configuration parameters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top