Brazilian, developer by profession, gamer for fun, amateour athletic and cyclist promising. Passionate for technology and inovation, Agile methodologies evangelist as SCRUM and Kanban, make continuous process improvements and sharing the knowledge. Especialist in Enterprise Java, web application and Open Source techonologies since 2007. Valdemar has posted 6 posts at DZone. View Full User Profile

Execute Selenium tests as unit test in Eclipse IDE

05.07.2011
| 5508 views |
  • submit to reddit

One more time I'm here talking about Selenium and tests. As I talked in the last post Functional Tests with Selenium 2.0 and cargo-maven-plugin, there was a problem if you want implement a new Selenium Test and execute it, you must to execute all Selenium tests, but it's not  efficient.

So, to resolve this problem we will be able to execute a specific test using maven in command line or in Eclipse IDE, which is very productive, and I'll show how to do that as a simple JUnit or TestNG test.

First of all, we'll create an abstract class that contain all necessary classes to execute Selenium tests in Eclipse IDE, and our tests must to extends this abstract class. In this class will have, for example, configurations to start/stop the application server that will deploy our application and configuration for create an instance of Selenium server and it engines start.

To help the manager the application server's classes we'll use Cargo and the application server will be Tomcat 6x. After that, It'll necessary to add Cargo and Selenium dependencies with test scope for both. They can be add by Eclipse IDE or maven configuration file(pom.xml), but in this example it added in pom.xml file as below:

<dependency>
   <groupId>org.codehaus.cargo</groupId>
   <artifactId>cargo-core-container-tomcat</artifactId>
   <version>1.0.6</version>
   <scope>test</scope>
</dependency>
   <dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium</artifactId>
   <version>2.0a4</version>
   <scope>test</scope>
</dependency>
After that, we'll create a method to recover a local Tomcat instance:
 /**
 * Method responsable to set all init tomcat's configuration
 */
 private TomcatExistingLocalConfiguration InitTomcatConfig() {
    String tomcatHome = System.getenv("TOMCAT_HOME");
    if (tomcatHome == null) {
       throw new IllegalArgumentException("The system property TOMCAT_HOME must be defined.");
    }
    TomcatExistingLocalConfiguration localTomcatConfiguration = new TomcatExistingLocalConfiguration(tomcatHome);
    return localTomcatConfiguration;
  }

The method will find a local Tomcat instance by environment variable called TOMCAT_HOME and it'll create an instance represented by TomcatExistingLocalConfiguration class.

And now we'll create a method which start the application server  Tomcat before start a test's suite as below:

// Method execute before all suite tests
 @BeforeSuite
 public void setUp() {
    LocalConfiguration tomcatConfiguration = InitTomcatConfig();

    // Containers configuration utilizado("tomcat6x") and war path
    war = new DefaultDeployableFactory().createDeployable("tomcat6x", "target/example.war",
        DeployableType.WAR);
    tomcatConfiguration.addDeployable(war);

    // Tomcat configurations
    container = new Tomcat6xInstalledLocalContainer(tomcatConfiguration);
    container.setHome(tomcatConfiguration.getHome());

    // Start o container
    container.start();

 }

After that, our Selenium tests's classes must to extends this abstract class and execute with our favorite test engines. In my example, it wasTestNG.

That it! To make code more maintainable, create tests and become the refactoring and maintainability of the applications less painful.

from: http://english.valdemarjr.net/2011/05/07/execute-selenium-tests-as-unit-test-in-eclipse-ide/

Sorry about my english. If you have any question or some difficult, contact me.

Published at DZone with permission of its author, Valdemar Júnior.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)