RSS

Selenium , testing framework for acceptance tests

17 Sep

selenium logo

Selenium is a testing framework that allows you automatizing your acceptance tests. Selenium gives us the opportunity to test the interaction between our application pages and the users. Basically Selenium framework allows us to do two different things:

  • Recording an use case that mock the interaction between an user and the web application user interface. We can record that interaction with the Selenium IDE tool.
  • This use case can be exported from the Selenium IDE to a specified testing framework such as JUnit or TestNG. Once the test has been exported we can run this test in any web browser we prefer with the Selenium WebDriver. Currently there are different implementations (drivers) to test our use case in Internet Explorer, Firefox y Chrome. So that we can test in an automatized way our web application in different web browsers. There is also a generic driver called HtmlUnit to run our tests in any web browser.

Selenium framework allows us to do lots of things, like testing our test within the Selenium IDE, debuging the interaction between the user and a specified page or running our test in a remote server. Last example could be really useful if we expect to use some CI tool like Jenkins or Hudson in our development lifecycle.

Steps to follow.

In our case we are going to use Selenium along with theSpring MVC and Maven JEE application we started in Spring MVC basic example with Maven. To write an acceptance test with Selenium framework in our application we have to follow the following steps:

  • We need to download Selenium IDE plugin (addon) for Firefox
  • We need to run our application in a Jetty (Tomcat, Glassfish, ..) container.
  • Open the CarForm page and fill up the basic form that is shown. The whole interaction is recorded with the Selenium IDE.
  • Export from Selenium IDE this use case to a JUnit class.
  • We need to include this test class inside the test/java folder within our project. Obviuously we must add the required dependencies and the Selenium plugin in orden to run test with Selenium.
  • We are going to recompile our project and execute the Maven test goal to see how Selenium runs our recently added test.

Download the Selenium IDE plugin.

All we need is downloading this Selenium IDE plugin from the Firefox addons page. There we will find a file with xpi extension that we need to install it. The Selenium IDE plugin can be found in the Selenium page too. Selenium IDE plugin is only available for Firefox. It isn’t a problem because we can record the user interaction in Firefox but test it in other web browser like IE, Chrome, …

SeleniumIDE

Record an interaction use case with Selenium IDE.

We will need to run our Spring MVC application with desde Maven in a Jetty container as we have previously seen. Once the application is started we need to open CarForm.html.

Start the test recording with Selenium IDE. Once started every single user interaction with a page will be recorded. As we can see in the following images we have filled our form and we have submitted. Within the Selenium IDE editor we can review these interactions.

Terminaremos de grabar nuestro test y lo exportaremos a un fichero con el formato adecuado para el framework JUnit.

Adding our test class to our project.

The only thing we need is:

  • Copy our test class to the /test/java folder.
  • Add the required dependencies to use Selenium in our pom.xml.
  • <dependency>
    			<groupId>org.seleniumhq.selenium</groupId>
    			<artifactId>selenium-firefox-driver</artifactId>
    			<version>2.5.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.seleniumhq.selenium</groupId>
    			<artifactId>selenium-chrome-driver</artifactId>
    			<version>2.5.0</version>
    		</dependency>
    		<dependency>
    			<groupId>org.seleniumhq.selenium</groupId>
    			<artifactId>selenium-server</artifactId>
    			<version>2.5.0</version>
    		</dependency>
    
  • Add the required Selenium plugin to run our acceptance test.

Running the tests

To start with we are going to test Selenium in our application this way, we will open two command consoles. The first one has to run our application with the next Maven command:

mvn jetty:run

In the second one we have to run our Maven test phase where our Selenium test will be executed .

mvn test

We will notice that a Selenium admin console will be open automatically. If we want we can configure some test parameters.

Another window will be open automatically as well. It will be here where the previously recorded actions with Selenium IDE will be repeated to test if our application will pass our acceptance tests.

About the source code

You can find the source code in my GitHub repository as I mentioned in the GitHub section within my previous post Spring MVC basic example with Maven. Anyway the code is shown below because I want to explain some details about it:

import org.junit.Test;
import junit.framework.TestCase;
import com.thoughtworks.selenium.DefaultSelenium;

public class SeleniumCarFormTest extends TestCase {
	private DefaultSelenium selenium;

	@Override
	public void setUp() throws Exception {
		super.setUp();
		selenium = createSeleniumClient("http://localhost:9080/");
		selenium.start();
		selenium.windowMaximize();
		selenium.setSpeed("1000");
	}

	@Override
	public void tearDown() throws Exception {
		selenium.stop();
		super.tearDown();
	}

	protected DefaultSelenium createSeleniumClient(String url) throws Exception {
		return new DefaultSelenium("localhost", 4444, "*chrome", url);
	}

	@Test
	public void testCarFormSelenium() throws Exception {
		selenium.open("/spring-mvc/carForm.html");
		selenium.type("id=brand", "Renault");
		selenium.type("id=name", "Laguna");
		selenium.type("id=price", "18500");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertEquals(selenium.getTitle(),"Car insert sucess");
	}
}

As we can see there is a setUp() method within the Test class where we set up our test configuration. We have to create a DefaultSelenium instance. This class is nothing more than a client to interact with the Selenium API. Through this API we can specify what our test has to run (the user interactions with the webpages). In this instance we could configure, i.e., the url where our test is going to be running, specify the web browser or the speed used as we running our test.

Besides there is a tearDown() method that will clean up our client resources in a proper way once the test is finished.

The test method is quite straightforward apart from notice how the different Selenium actions are called such as open a page, fill an input text or click a button to submit your form. Finally if the test is sucesful the test will open a page whose title will be “Car insert sucess”.

 
Deja un comentario

Publicado por en 17 septiembre, 2011 en JEE, Selenium, Testing

 

Etiquetas: , , , ,

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

 
A %d blogueros les gusta esto: