Appunti per la creazione di integration test usando Selenium, HtmlUnit, Maven e Cargo.
Dipendenze nel pom.xml:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>5.9</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>org.seleniumhq.selenium.client-drivers</groupId> <artifactId>selenium-java-client-driver</artifactId> <version>1.0-beta-2</version> <scope>test</scope> </dependency> <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit-core-js</artifactId> <version>2.4</version> </dependency>
Configurazione per selenium-maven-plugin :
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>selenium-maven-plugin</artifactId> <version>1.0-rc-1</version> <executions> <execution> <phase>pre-integration-test</phase> <goals> <goal>start-server</goal> </goals> <configuration> <background>true</background> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludes> <exclude>**/it/**</exclude> </excludes> </configuration> <executions> <execution> <phase>integration-test</phase> <goals> <goal>test</goal> </goals> <configuration> <excludes> <exclude>none</exclude> </excludes> <includes> <include>**/it/**</include> </includes> </configuration> </execution> </executions> </plugin>
Configurazione per cargo-maven2-plugin:
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <wait>false</wait> </configuration> <executions> <execution> <id>start</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <id>stop</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin>
Configurazione per Jetty plugin:
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.14</version> </plugin>
Profilo per far partire l'applicazione con Jetty
<profile> <id>jetty6x</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>jetty6x</containerId> <type>embedded</type> </container> </configuration> </plugin> </plugins> </build> </profile>
Profilo per far partire l'applicazione con Tomcat
<profile> <id>tomcat6x</id> <build> <plugins> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>tomcat6x</containerId> <zipUrlInstaller> <url>file:///path/to/apache-tomcat-6.0.18.zip</url> <installDir>${user.home}/tomcat6x</installDir> </zipUrlInstaller> </container> <configuration> <home>${project.build.directory}/tomcat6x</home> <properties> <cargo.servlet.port>8080</cargo.servlet.port> <cargo.logging>high</cargo.logging> </properties> </configuration> </configuration> </plugin> </plugins> </build> </profile>
Un semplice test, se index.jsp risponde regolarmente:
import java.net.URL; import java.net.HttpURLConnection; import org.testng.annotations.Test; import static org.testng.Assert.*; public class WebappTest { @Test public void testCallIndexPage() throws Exception { URL url = new URL("http://localhost:8080/webapp"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); assertEquals(200, connection.getResponseCode()); } }
Primo test con Selenium
import static org.testng.Assert.*; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.Selenium; public class SeleniumFirstTest { Selenium selenium; @BeforeClass public void init() { selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080/webapp/index.jsp"); selenium.start(); } @AfterClass public void shutdown() { selenium.stop(); } @Test(enabled = true) public void testTitle() { selenium.open("http://localhost:8080/webapp/index.jsp"); selenium.waitForPageToLoad("5000"); assertEquals(selenium.getTitle(), "hello world"); } }
Primo test con HtmlUnit
import java.io.IOException; import java.net.MalformedURLException; import org.testng.annotations.Test; import static org.testng.Assert.*; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class HtmlUnitTest { @Test public void homePage() throws FailingHttpStatusCodeException, MalformedURLException, IOException { final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2); final HtmlPage page = webClient.getPage("http://localhost:8080/webapp/index.jsp"); assertEquals("hello world", page.getTitleText()); } }
Per lanciare gli integration test:
mvn integration-test