[Webtest] user contributions / verifyimages

Marc Guillemot webtest@lists.canoo.com
Wed, 28 Jul 2004 10:13:51 +0200


This is a multi-part message in MIME format.
--------------010501080705040206080107
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

I've got one, it works for me, but no assurance on its robustness.
It's on my todo list to incorporate it in Webtest's distrib.

User written webtests are those in the package com.canoo.webtest.extension.

Marc.

Luca Scheuring wrote:
> hello!
> 
> I was looking for a "verifyimages" teststep for webtest and I found some
> posts in the mailinglist archive about this issue. obviously some people
> have written such teststeps. I also found a discussion about the problem
> what to do with such user contributions, wheter and how they should be
> included in the distribution (there was something about a "contrib"-dir).
> since I can't find any user written teststeps in the webtest cvs, here my
> question: is there a place where such contributions are collected? or can
> somebody mail me the verifyimages-code?
> 
> thanx a lot!
> 
> cheers, 
> 
> luca
> 
> _______________________________________________
> WebTest mailing list
> WebTest@lists.canoo.com
> http://lists.canoo.com/mailman/listinfo/webtest
> 


--------------010501080705040206080107
Content-Type: text/plain;
 name="VerifyImages.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="VerifyImages.java"

package de.internetzky.webtests.extension;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.xml.sax.SAXException;

import com.canoo.webtest.engine.Context;
import com.canoo.webtest.engine.StepExecutionException;
import com.canoo.webtest.engine.StepFailedException;
import com.canoo.webtest.steps.Step;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebImage;
import com.meterware.httpunit.WebResponse;

/**
 * Verifies that all images referenced in the last response are present.  
 * @author Marc Guillemot
 * @version $Revision: 1.3 $ $Date: 2003/10/13 10:07:59 $ Last change by $Author: cvs-marc $
 * @webtest
 * 	stepname="VerifyImages"
 * 	description="Verifies that all images referenced in the last page are available"
 */
public class VerifyImages extends Step
{
	/**
	 * looks for missing images referenced in the page
	 */
	public void doExecute(Context _context)
	{
		WebConversation conversation = _context.getWebConversation();
		WebResponse lastResponse = _context.getLastResponse();

		List liFailedImagesUrl = checkImages(conversation, lastResponse);

		if (!liFailedImagesUrl.isEmpty())
		{
			StringBuffer sb =
				new StringBuffer(
					getStepId(_context)
						+ ": missing images in page "
						+ lastResponse.getURL()
						+ ": ");
			for (Iterator iter = liFailedImagesUrl.iterator(); iter.hasNext();)
			{
				sb.append(iter.next()).append(";\r\n ");
			}
			throw new StepFailedException(sb.toString(), this);
		}
	}

	/**
	* Checks the images and return the url of the missing ones
	*/
	protected List checkImages(
		WebConversation _conversation,
		WebResponse _response)
	{
		List liRep = new ArrayList();
		WebImage[] tabImg = null;
		try
		{
			tabImg = _response.getImages();
		}
		catch (SAXException e)
		{
			throw new StepExecutionException(
				"Exception occured trying to get images: " + e.toString(), this);
		}

		for (int i = 0; i < tabImg.length; ++i)
		{
			try
			{
				WebResponse resp =
					_conversation.sendRequest(tabImg[i].getRequest());
			}
			catch (Exception e)
			{
				liRep.add(tabImg[i].getSource());
			}
		}

		return liRep;
	}
}
--------------010501080705040206080107--