Cross browser testing with Selenium

By Barnaby Golden, 5 September, 2011

Combine Maven, Jetty and Selenium to automate your cross browser testing at build time. The approach I have taken is to start Jetty and Selenium in the Maven pre-integration test phase. You also need to tell Maven what to run as integration tests (I use the convention of classes ending in *IT). Here is an exert from the plugins section of my POM:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.9</version>
 <configuration>
 </configuration>
 <executions>
  <execution>
   <id>integration-tests</id>
   <phase>integration-test</phase>
   <goals>
    <goal>test</goal>
   </goals>
   <configuration>
    <skip>false</skip>
    <excludes>
     <exclude>none</exclude>
    </excludes>
    <includes>
     <include>**/*IT.java
     </include>
    </includes>
   </configuration>
  </execution>
 </executions>
</plugin>
<plugin>
 <groupId>org.mortbay.jetty</groupId>
 <artifactId>maven-jetty-plugin</artifactId>
 <version>6.1.26</version>
 <configuration>
  <scanIntervalSeconds>5</scanIntervalSeconds>
  <stopPort>7966</stopPort>
  <stopKey>foo</stopKey>
  <connectors>
   <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
    <port>7080</port>
    <maxIdleTime>60000</maxIdleTime>
   </connector>
  </connectors>
 </configuration>
 <executions>
  <execution>
   <id>start-jetty</id>
   <phase>pre-integration-test</phase>
   <goals>
    <goal>run</goal>
   </goals>
   <configuration>
    <daemon>true</daemon>
   </configuration>
  </execution>
  <execution>
   <id>stop-jetty</id>
   <phase>post-integration-test</phase>
   <goals>
    <goal>stop</goal>
   </goals>
  </execution>
 </executions>
</plugin>
<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>selenium-maven-plugin</artifactId>
 <version>2.0</version>
 <executions>
  <execution>
   <id>start</id>
   <phase>pre-integration-test</phase>
   <goals>
    <goal>start-server</goal>
   </goals>
   <configuration>
    <background>true</background>
    <logOutput>true</logOutput>
    <multiWindow>true</multiWindow>
   </configuration>
  </execution>
  <execution>
   <id>stop</id>
    <phase>post-integration-test</phase>
    <goals>
     <goal>stop-server</goal>
    </goals>
  </execution>
 </executions>
</plugin>

Cross browser testing is then easy to implement. You just run individual tests that use the vaious Selenium browser start commands. Here is an example from a JUnit 4 test:

    /**
     * Test Firefox.
     */
    @Test
    public void testFirefox() {

        final DefaultSelenium seleniumFF =
            new DefaultSelenium(HOST, SELENIUM_PORT,
                    "*firefox", URL);
        seleniumFF.start();

        seleniumFF.open(PATH);
        assertTrue("FF: recent lyrics not shown",
                seleniumFF.isTextPresent(SEARCH_TEXT));

        seleniumFF.close();
        seleniumFF.stop();
    }

    /**
     * Test IE.
     */
    @Test
    public void testIE() {

        final DefaultSelenium seleniumIE =
            new DefaultSelenium(HOST, SELENIUM_PORT,
                    "*iexploreproxy", URL);

        seleniumIE.start();

        seleniumIE.open(PATH);
        assertTrue("IE: recent lyrics not shown",
                seleniumIE.isTextPresent(SEARCH_TEXT));

        seleniumIE.close();
        seleniumIE.stop();
    }

 

Tags

Target Audience