The Selenium and Webdriver projects are merged to create a powerfull framework open source project for functional tests based in javascript for testing web applications and sites. The tests can be executed by a browser like Firefox or Chrome or can be executed by a lightweight, super-fast browser emulation based on HtmlUnit treating normal situation you might want test in an application web.

If you want to do some tests just download the .jars and add in your classpath’s project.
In this How-To I’ll use Selenium and Webdriver to execute integration tests in maven life cycle with cargo-maven-plugin.

The cargo-maven-plugin is used to deploy applications using maven. It’s install the container and the dependencies thats required to deploy your application and can be configured by almost containers avaiable as Tomcat, JBoss, Jetty(embedded) or GlashFish. The cargo-maven-plugin will start the container and deploy the application, after that it will execute the tests and stop the container.

For example, I’ll use the “Modelos de Celulares” project developed in the last post and I’ll add the Selenium tests and configuration of cargo-maven-plugin.

First of all lets add the cargo-maven-plugin dependency to start the container to execute the tests in the pre-integration-test maven lifecycle, after that stop the container in the post-integration-test maven lifecycle. The container used in this example was Tomcat 6x installed local and defined in the <home> attribute. It was necessary to add another plugin to execute all tests when the container started. The plugin is maven-surefire-plugin.

See the example of maven-surefire-plugin and cargo-maven-plugin configured in the pom.xml file:

<plugin>
   <groupId>org.codehaus.cargo</groupId>
   <artifactId>cargo-maven2-plugin</artifactId>
   <version>1.0.6</version>
   <configuration>
      <wait>false</wait>
      <!-- Container configuration -->
      <container>
         <type>installed</type>
         <containerId>tomcat6x</containerId>
         <home>${TOMCAT_HOME}</home>
      </container>
   </configuration>
   <executions>
      <execution>
         <id>start-container</id>
         <phase>pre-integration-test</phase>
         <goals>
            <goal>start</goal>
         </goals>
      </execution>
      <execution>
         <id>stop-container</id>
         <phase>post-integration-test</phase>
         <goals>
           <goal>stop</goal>
         </goals>
      </execution>
   </executions>
</plugin>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <configuration>
   <!-- Skip the normal tests,
                 we'll run them in the integration-test phase -->
      <skip>true</skip>
   </configuration>
   <executions>
      <execution>
         <phase>integration-test</phase>
         <goals>
            <goal>test</goal>
         </goals>
         <configuration>
            <skip>false</skip>
         </configuration>
      </execution>
   </executions>
</plugin>

The next step is add the Selenium dependency to implements the functional tests. Just add in the pom.xml file:

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium</artifactId>
   <version>2.0a4</version>
   <scope>test</scope>
</dependency>

After that, lets create a test class. A way to acess the object browser is through four official drivers: FirefoxDriver, the most mature driver and used in this example project, Internet Explorer Driver, tested in versions IE6, IE7 on windows Vista and XP and compared with others drivers, its relatively slow. The ChromeDriver, it’s newer driver and the HtmlUnit as a lightweight, super-fast browser emulation.

Lets interacting with the browser by FirefoxDriver‘s method class call “findElement” with WebElement parameter and can be recover by By class like in this method tentaCadastrarSemNenhumDado below:

public void tentaCadastrarSemNenhumDado()  {
    driver.get("http://localhost:8080/CadastroCelular");
	driver.findElement(By.id("botaoSubmit")).click();

	String mensagemErroNome = driver.findElement(By.id("mensagemErroNome")).getText();
        // name required field message
	Assert.assertEquals("Campo Nome Obrigatório", mensagemErroNome);

	String mensagemErroDescricao = driver.findElement(By.id("mensagemErroDescricao")).getText();
        // description required field message
	Assert.assertEquals("Campo Descrição Obrigatório", mensagemErroDescricao);

	driver.close();
}

This test try to insert a model mobile without fill the required fields and check the messages of required fields.

Through By class is possible recover web elements by links, name, tag html, css or link label. In this version of Selenium there is an annotation call @FindBy which designed to clean up the code specifying the location strategy. The method “tentaCadastrarSemNenhumDado()” example was developed without annotation and see the same method using annotation @FindBy:

     @FindBy(id = "botaoSubmit")
	private WebElement botaoSubmit;

	@FindBy(id = "mensagemErroNome")
	private WebElement msgErroNome;

	@FindBy(id = "mensagemErroDescricao")
	private WebElement msgErroDescricao;

	@Test
	public void tentaCadastrarSemNenhumDado() {
	    driver.get("http://localhost:8080/CadastroCelular");
		botaoSubmit.click();
	        // name required field message
		Assert.assertEquals("Campo Nome Obrigatório", msgErroNome.getText());
               // description required field message
		Assert.assertEquals("Campo Descrição Obrigatório", msgErroDescricao.getText());

		driver.close();
	}

As you can see the code is cleaner and simple to understand. Configuration and implemantion of functional tests are so simples which you don’t have reason to do not it.

Nice coding.
Sorry about my english. If you have any question, contact me.

Tags: , , , , ,

8 Comments on Functional Tests with Selenium 2.0 and cargo-maven-plugin

  1. [...] Functional Tests with Selenium 2.0 and cargo-maven-plugin updates some common documentation around launching the Se server and tests from inside Maven GA_googleAddAttr("AdOpt", "1"); GA_googleAddAttr("Origin", "other"); GA_googleAddAttr("theme_bg", "ffffff"); GA_googleAddAttr("theme_text", "000000"); GA_googleAddAttr("theme_link", "b54141"); GA_googleAddAttr("LangId", "1"); GA_googleAddAttr("Autotag", "technology"); GA_googleFillSlot("wpcom_below_post"); Leave a Comment LikeBe the first to like this post. [...]

  2. Jason says:

    Hi There,
    Nice post. We're doing something similar in our build to run selenium tests. Question though: do you know of a straight forward way to trigger Selenese-based tests? Can Selenese tests be used within a JUnit test like you have?

    thanks,
    Jason

    • valdemarjuniorr says:

      Hi Janson,
      you can implement a class using Selenium-core classes and cargo framework to start a container, execute the selenium tests and stop the container. It's a awesome suggestion to the next post. I'll implement an example as soon as possible. Ok?

  3. Understandable. Interesting experience.

  4. Jonie Enzor says:

    I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again!

  5. Nice read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch because I found it for him smile Thus let me rephrase that: Thanks for lunch! “Do you want my one-word secret of happiness–it’s growth–mental, financial, you name it.” by Harold S. Geneen.

  6. Dude, please tell me that youre heading to publish extra. I notice you havent written an additional blog for a while (Im just catching up myself). Your blog is just too important to be missed. Youve obtained so significantly to say, this kind of knowledge about this topic it would be a shame to see this weblog disappear. The internet needs you, man!

Leave a Reply

*