Selenium-RC and Continuous Integration

This page is about using Selenium-RC in a continuous integration system- running Selenium tests from a commandline, from Ant, or TestNG.

Currently, we gather informations. Then we will try to organize it in a didactic and non-redundant way. The point is that there is a lot of way to launch the tests and gather the results,  so you have to be coherent with the tools you are using (ANT, TestNG, CC, ...)

There are a couple of things to know about using Selenium-RC in this way:

  • The Selenium server (selenium-server.jar) is the thing that actually starts the web browser. This is important, because if you need to pass parameters to the browser through commandline parameters, you do it by changing the server's environment.
  • Use Xvfb (X Windows Virtual Frame Buffer): If you want to run Selenium on a Unix server- without an X Windows display- or if you just don't want to see the web browser windows popping up, use xvfb. This is an X server that just runs in memory, without a display.

ANT

A target to start the server : 

<java jar="${selenium-server.jar}" fork="true" spawn="true" />

 A target to stop the server (from Forum thread) :

<target name="stop-server">
    <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"
        dest="result.txt" ignoreerrors="true" />
    <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
</target>

A target to launch Selenese tests (from Forum thread) : 

<target name="runSeleniumTests">
    <java jar="${acceptanceTestLibDir}/selenium-server.jar" fork="true">
        <arg line="-htmlSuite &quot;${firefox}&quot;"/>
        <arg line="&quot;${baseURL}&quot;"/>
        <arg line="&quot;${acceptanceTestListDir}/testSuite.html&quot;"/>
        <arg line="&quot;${acceptanceTestReportDir}/results.html&quot;"/>
        <arg line="-timeout 30"/>
    </java>
</target>

A complete example :

<?xml version="1.0" encoding="UTF-8"?>
<project name="Run Test" default="run_test" basedir=".">

    <property name="test.dir" value="src\test" />

    <property name="testLibDir" value="lib" />

    <path id="run.cp">
        <pathelement path="build"/>
        <fileset dir="build/">
            <include name="*.jar"/>
        </fileset>
        <pathelement path="lib"/>
        <fileset dir="lib/">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="run_test" description="Start Proxy ; Run TestNG ; stop Proxy">
        <parallel>
            <antcall target="start-server"></antcall>
            <sequential>
                <echo taskname="waitfor" message="Wait for proxy server launch" />
                <waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
                    <http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete"/>
                </waitfor>
                    <antcall target="run_testNG"></antcall>
                    <antcall target="stop-server"></antcall>
            </sequential>
        </parallel>
    </target>

    <target name="run_testNG" description="Run TestNG">
        <testng classpathref="run.cp" haltOnfailure="false">
            <xmlfileset dir="." includes="testng.xml" />
        </testng>
    </target>

    <target name="start-server">
        <java jar="lib/selenium-server.jar" fork="true" spawn="true">
            <arg line="-timeout 30"/>
            <jvmarg value="-Dhttp.proxyHost=proxy.corporate.com"/>
            <jvmarg value="-Dhttp.proxyPort=3128"/>
        </java>
    </target>

    <target name="stop-server">
        <get taskname="selenium-shutdown"
            src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"
            dest="result.txt" ignoreerrors="true" />
        <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
    </target>

    <taskdef resource="testngtasks" classpath="lib/testng-5.0-jdk15.jar" />

</project>

ANT users (feedback welcome) : jattardi ; alxdark ; mustaphakhan ; jmheneman. ANT+CC : paulocheque ; lance.li2005 ...

MAVEN 2

To start/stop Selenium RC before/after integration tests, edit the pom.xml to include the following stanzas (from this post on Maven users list).

<?xml version="1.0"?>
<project>
    <dependencies>
        <dependency>
            <groupId>org.openqa.selenium.client-drivers</groupId>
            <artifactId>selenium-java-client-driver</artifactId>
            <version>${selenium-version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.openqa.selenium.server</groupId>
            <artifactId>selenium-server</artifactId>
            <version>${selenium-version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>dependency-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.openqa.selenium.server</groupId>
                                    <artifactId>selenium-server</artifactId>
                                    <version>${selenium-version}</version>
                                    <type>jar</type>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                    <destFileName>selenium-server.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>start-selenium</id>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <tasks>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>
                                <echo taskname="selenium" message=" Starting Selenium Remote Control                        "/>
                                <echo taskname="selenium" message=" Please make sure that FireFox executable is on the PATH "/>
                                <java jar="${project.build.directory}/selenium-server.jar" fork="yes" spawn="true"/>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-selenium</id>
                        <phase>post-integration-test</phase>
                        <configuration>
                            <tasks>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>
                                <echo taskname="selenium" message=" Shutting down Selenium Remote Control                   "/>
                                <echo taskname="selenium" message=" DGF Errors during shutdown are expected                 "/>
                                <get taskname="selenium"
                                     src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"
                                     dest="result.txt" ignoreerrors="true"/>
                                <echo taskname="selenium" message="---------------------------------------------------------"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>openqa</id>
            <url>http://maven.openqa.org</url>
        </repository>
    </repositories>

    <properties>
        <selenium-version>0.8.1</selenium-version>
    </properties>
</project>

Maven2 users: Binil Thomas

A complete example on how to use Selenium, Cargo, TestNG and Maven can be found here

JAVA

Another way to launch the server (from Forum thread) :

Sure, just add selenium-server.jar to your classpath, create a new SeleniumServer object, and .start() it.

 This is typically used in SetUp() in TestNG.

LINUX 

Here are a couple of Unix commands that might be helpful:

 Starting Xvfb as display 1:

% startx -- `which Xvfb` :1 -screen 0 1024x768x24 2>&1 >/dev/null &

Starting a Selenium server that will launch web browsers on this display (display 1):

% DISPLAY=:1 java -jar selenium-server.jar

Taking a snapshot of what is happening in the xvfb display (useful for debugging):

% DISPLAY=:1 import -window root /tmp/xvfb_snapshot.png

Note: the '%' is the Unix shell prompt. The 'DISPLAY=:1" is an environment variable being set on the same commandline. You can just as easily set DISPLAY to something like foo.bar.com:2, which would make Selenium's web browser appear on foo.bar.com's second X Windows display.

Note2: if you have no startx available, try something like 'sudo apt-get install xserver-xorg' (worked for me on Ubuntu)

Note3: If you have a headless display, you may need to use a display other than 1. Here is what we did to get it working on our headless Fedora 8 box:
yum update firefox
yum update xorg-x11-server-Xvfb
yum update cairo

Now firefox will work with the Xvfb on Fedora 8 - there were some bugs in the older packages. It's always good to update your packages. This problem shouldn't exist out of the box in Fedora 9.

We edited /etc/rc.d/rc.local to include:
Xvfb :99 -ac -noreset &

(And you have to reboot the machine, or run this line manually for it to be active, or make the whole xinetd scripts if you care to)

In Hudson (our CI server) we added a "shell execute" that did this:
export LD_LIBRARY_PATH="/usr/lib/firefox/"; export PATH="$PATH:/usr/lib/firefox/"; export DISPLAY=:99;
java -jar /usr/share/selenium-rc/selenium-server/selenium-server.jar -htmlSuite "*firefox /usr/lib/firefox/firefox-bin" "http://test-server/" "/var/www/html/hs/tests/TestSuite.html" "/var/www/html/hs/tests/selenium-results.html"


A Linux user : obinho.


This information is adapted from the following pages:

Labels

xvfb xvfb Delete
ant ant Delete
maven maven Delete
headless headless Delete
virtual_framebuffer virtual_framebuffer Delete
automation automation Delete
selenium_rc selenium_rc Delete
continuous_integration continuous_integration Delete
hudson hudson Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Dec 08, 2006

    David says:

    There is a simple plugin that lets you use Selenium-IDE HTML tests in your maven...

    There is a simple plugin that lets you use Selenium-IDE HTML tests in your maven webapp project and integrates in maven's build life cycle, stopping it whenever a test fails.

    Upload to the maven central repository pending.

    http://mavenium.sourceforge.net/

  2. May 02, 2007

    Dave Cameron says:

    When running with Ant and CruiseControl it's important to both fork and spawn th...

    When running with Ant and CruiseControl it's important to both fork and spawn the selenium-server. Not doing so caused our selenium-shutdown target to hang indefinetly.

    Ant 1.7.0
    CruiseControl 2.6.2
    Selenium-RC 0.9
    Java 1.5.0_11

  3. Jun 07, 2007

    Jason Dillon says:

    FYI, you can use the selenium-maven-plugin to automate the configuration of the ...

    FYI, you can use the selenium-maven-plugin to automate the configuration of the Selenium RC server when using Maven 2.

    And you can also automate configuration/execution of Xvfb to allow tests to run on headless unix systems too.

  4. Oct 14, 2008

    Jeff says:

    For those who want to use the ant example above, it is important to use the 'lin...

    For those who want to use the ant example above, it is important to use the 'line' argument instead of the 'value' arguments when passing the port and timeout to the server jar.

    If you use the 'value' argument syntax, the following will happen:

    [antlib:org.apache.tools.ant] Could not load definitions from resource org/apache/tools/ant/antlib.xml. It could not be found.
    [java] Executing '/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java' with arguments:
    [java] '-jar'
    [java] '/path/to/jar/selenium-server.jar'
    [java] '-port 4444'
    [java] '-timeout 10'
    [java]
    [java] The ' characters around the executable and arguments are
    [java] not part of the command.
    [java] unrecognized argument -port 4444:
    [java] Usage: java -jar selenium-server.jar [-interactive] [options]
    [java]
    [java] -port <nnnn>: the port number the selenium server should use
    [java] (default 4444)
    [java] -timeout <nnnn>: an integer number of seconds before we should give
    [java] up

  5. Dec 12, 2008

    Joan says:

    I was using the ant goal above, and using selenium.stop immediately before selen...

    I was using the ant goal above, and using selenium.stop immediately before selenium.start.  Quite often we were left with selenium in an indeterminant state: the stop takes a second to finish and the start overlapped - and we had a selenium process we couldn't connect to.

     So I've replace the target with this: 

      <target name="selenium.stop">
            <waitfor maxwait="3" maxwaitunit="second">
                <not>
                    <http url="http://localhost:${selenium.server.port}/selenium-server/driver/?cmd=shutDown"/>
                </not>
            </waitfor>
        </target>
    

      
    Which is giving a more stable build

  6. Jan 20, 2011

    NLR REDDY says:

    I'm seeing the below server when I'm running this on RHEL box. 12:34:18.350 IN...

    I'm seeing the below server when I'm running this on RHEL box.

    12:34:18.350 INFO - Version Jetty/5.1.x
    12:34:18.351 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
    12:34:18.351 INFO - Started HttpContext[/selenium-server,/selenium-server]
    12:34:18.352 INFO - Started HttpContext[/,/]
    12:34:18.365 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@10a3b24
    12:34:18.365 INFO - Started HttpContext[/wd,/wd]
    12:34:18.368 INFO - Started SocketListener on 0.0.0.0:4444
    12:34:18.369 INFO - Started org.openqa.jetty.jetty.Server@1c6f579
    12:34:18.443 WARN - Caution: '/usr/bin/firefox': file is a script file, not a real executable. The browser environment is no longer fully under RC control
    12:34:18.477 INFO - Preparing Firefox profile...
    Error: cannot open display: :1
    HTML suite exception seen:
    java.lang.RuntimeException: Timed out waiting for profile to be created!
    at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.waitForFullProfileToBeCreated(FirefoxChromeLauncher.java:348)
    at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.populateCustomProfileDirectory(FirefoxChromeLauncher.java:124)
    at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.launch(FirefoxChromeLauncher.java:91)
    at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.launchHTMLSuite(FirefoxChromeLauncher.java:393)
    at org.openqa.selenium.server.browserlaunchers.FirefoxLauncher.launchHTMLSuite(FirefoxLauncher.java:94)
    at org.openqa.selenium.server.htmlrunner.HTMLLauncher.runHTMLSuite(HTMLLauncher.java:121)
    at org.openqa.selenium.server.htmlrunner.HTMLLauncher.runHTMLSuite(HTMLLauncher.java:166)
    at org.openqa.selenium.server.SeleniumServer.runHtmlSuite(SeleniumServer.java:545)
    at org.openqa.selenium.server.SeleniumServer.boot(SeleniumServer.java:239)
    at org.openqa.selenium.server.SeleniumServer.main(SeleniumServer.java:198)
    Publishing Selenium report...

  7. Jul 20, 2011

    kishoreg says:

    Hi,   I am trying to integrate selenium+Java scripts in hudson. I followed the s...

    Hi,
     
    I am trying to integrate selenium+Java scripts in hudson. I followed the steps http://stackoverflow.com/questions/3294386/hudson-step-by-step-guide-to-set-up-master-and-slave-machines
    and am getting below error. Please help where i am worng.
    Started by user anonymous
    Building on master
    ERROR: Please configure the Selenium Remote Control htmlSuite Runner in admin of hudson
    Finished: FAILURE

  8. Jan 20, 2012

    Damien Papworth says:

    The Maven2 instructions are not correct for Selenium 2. The java call to seleniu...

    The Maven2 instructions are not correct for Selenium 2. The java call to selenium-server.jar will silently fail (running it from the command line will result in an error about Main-class not being defined in the manifest). You need to use selenium-server-standalone.jar, which is not available in Maven (at least when I looked). This also applies to the ant instructions. This is probably due to a change in later versions of Selenium. (You probably won't notice this problem when running tests locally, as I believe the local drivers aren't dependent on having an active server present)