ICE09 . playing with java, scala, groovy and spring .

Playing with Spring

Archive for September, 2008

The mini project series: Web Testing with WebDriver and Spring

Posted by ice09 on September 15, 2008

All files and the zipped Eclipse project can be downloaded at the end of this post.

It’s more fun to learn Spring by example, that’s why I start a mini project series, where I use Spring as glue code for completely different purposes.
First, I introduce a Web Testing “Framework” which builds upon WebDriver and obviously uses Spring for registering and managing the several entities.

Idea

The idea is to enable the user to write simple test modules which can easily be combined to use cases which themsevles can be combined to scenarios.
In terms of banking, a module could be “Login”, which performs a login on the banking site. The use case would be “Buy Fund”, which consists of several steps (modules), eg. “Login”, “Choose Fund”, etc. A complete scenario consists of several use cases, eg. “Buy Fund”, “Check Fund in Orderbook”, etc.

The structure is shown in the classdiagram.

The modules will be able to read their own test data, which itself consists of input data and test patterns, stored in either CSV or Excel files.

The testdata will be stored in directory structure.

Spring specials

At some point, we want to create Spring beans from modules and register them in some kind of map. However, we can not know if a bean is registered and we do not want to implement some “registering” logic inside the single steps.
Therefore, we create out own BeanPostProcessor and check if the bean which is currently created implements the IModule interface. If it does, we will register it.
This is a very nice way of using Springs interceptor mechanisms, which allow you to intercept each step in Springs initialization process.

The Bean Post Processor

package de.deka.webtest.springext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;

import de.webtest.entities.ModuleRegistry;
import de.webtest.entities.modules.IModule;

public class ModuleRegistryProcessor implements BeanPostProcessor, InitializingBean {

	private static Logger log = Logger.getLogger(ModuleRegistryProcessor.class);

	private ModuleRegistry registry;

	public void setRegistry(ModuleRegistry registry) {
		this.registry = registry;
	}

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    	if (bean instanceof IModule) {
            log.info("Registering module '" + beanName + "'");
    		registry.addModule(beanName, (IModule)bean);
    	}
        return bean;
    }

	public void afterPropertiesSet() throws Exception {
		if (registry == null)
			throw new IllegalStateException("registry not set.");
	}

}

This bean just has to be registered in an application-context and if a bean is registered and of type IModule, it is added to the Module registry.

Instructions

Installation

IMPORTANT: For the example Scenario to work you will have to switch GMail to HTML layout.

After importing the Eclipse project you will have to change the data in the provided Excel sheets (you can use CSV files as well). In the sample, Firefox will open and the workflow will try to log in to GMail with the provided account (in the Excelsheet in the “Login” subdirectory of the “Scenario…” directory). Afterwards, a mail will the composed and send and the sent mail folder will be examined for the sent mail (the subject). If it’s available, the first mail will be checked and deleted.
Finally, a logout is performed.

Besides having to change the data provided in the Excel sheet data files, the sample is far from finished. In the downloaded form, the sample will only work for the German version of Gmail (also in the current version). In the Login module, the text “Neue Nachricht” (new mail) is searched to validate a successful login. Obviously, this does not work in other languages. Furthermore, it would make much sense to refactor the sample and externalize the text search to the Excel sheet (as “test:text” column). But this is just an example and there is much to do on it.

Installation of Firefox plugin

Update 18.09.2008

After the comment by Simon I reassured that you just have to do nothing to get the Firefox extension installed – it will work by default. However, having played around with earlier versions of WebDriver which had to be installed manually, I did not understand this and was quite disturbed by not being able to find any installation instruction. So, ok, that’s why – there is no need for it.
It is important to note that you have to remove the existing “WebDriver” profile first.
If no profile exists, the driver will be installed automatically (or automagically) on the first startup of your Java application (that’s quite a lot magic for me – however, it’s really convenient).
I recommend reading his post regarding installation of firebug plugin which is really useful.

For what it’s worth, I keep the manual installation instructions in this post, for those who whyever can not delete the existing WebDriver profile.

Manual installation of WebDriver Firefox plugin

Compare the screenshots, first you have to create a Profile in Firefox named “WebDriver” (start Firefox with -p to start profile manager).
Afterwards, the zip inside the downloaded webdriver-zips must be renamed (the extension must be changed) to be able to install it as a Firefox plugin (just open the file in Firefox).
After the installation, a simple Start of the project class should be enough to start the webDriver Firefox plugin.



Posted in Webdriver | 1 Comment »