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

1

10

Background:
I'm using selenium-server-2.25.0 in conjunction with J-Unit 4 to run through a handful of UI testing scenarios for my GWT app. In my IDE (Netbeans 7.2), I can right-click on my project, choose "Test", and see Firefox windows pop up all over (as they should) with the Selenium tests running as expected. From the command line, I can also run mvn integration-test and see the same.

Goal:
I'm trying to get these tests to run headless in an Xvfb display, but I seem to be having trouble getting this to work with Maven. I can manually run export display=:2 (:2 being my Xvfb display) beforehand, and then the tests then DO run in the invisible display successfully.

Issue:
Nothing seems to change at all when I include the full <plugin> entry from here in my pom.xml and run mvn integration-test. I still see Windows popping up all over and the tests running not in the Xvfb display. If I take it out and run again, same results. When I change the phase from pre-integration-test to qwertyasdf however, Maven does complain about an invalid lifecycle phase - so I know it's not completely ignoring it, and I am editing the appropriate pom.xml.

Thanks!

Caulk answered 24/9, 2012 at 21:7 Comment(0)
C
8

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!

Caulk answered 25/9, 2012 at 21:45 Comment(2)
Please show me the content of file target/selenium/display.propertiesGreek
Maybe anyone knows how to pass resolution configuration to Xvfb through maven plugin? I'd need to get similar result to command line execution with parameters: Xvfb :0 -ac -screen 0 1024x768x24 &.Astrograph

© 2022 - 2024 — McMap. All rights reserved.