Execute Selenium tests as unit test in Eclipse IDE

DZone 's Guide to

Execute Selenium tests as unit test in Eclipse IDE

· Java Zone ·
Free Resource

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:

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
 public void setUp() {
    LocalConfiguration tomcatConfiguration = InitTomcatConfig();

    // Containers configuration utilizado("tomcat6x") and war path
    war = new DefaultDeployableFactory().createDeployable("tomcat6x", "target/example.war",

    // Tomcat configurations
    container = new Tomcat6xInstalledLocalContainer(tomcatConfiguration);

    // Start o container


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.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}