[Webtest] A alternate approach to extending WebTest

PETERSON,STEPHEN (A-Sonoma,ex1) webtest@lists.canoo.com
Fri, 24 May 2002 08:06:14 -0600


Hello all,
I've taken a slightly different approach to extending WebTest than I've seen discussed elsewhere. Rather than modifying the WebTest code directly (which changes quite often), I derive classes from it and use those classes. The advantage of this is: I can just drop in a new webtest.jar from Canoo and my changes are already integrated (well, sometimes I have to recompile my code -- but no source changes)! 
First you need to extend TestSpecificationTask -- this is just to get a "hook" into the system. No new functionality is added here -- is simply uses a different step container that is aware of the new / modified steps: 
	public class TestSpecificationTaskPlus extends TestSpecificationTask 
	{
	public void addSteps (TestStepSequencePlus steps)
	{
	super.addSteps( steps );
	}
	}
In the Ant file, refer to this class instead of TestSpecificationTask with the taskdef, namely 
	<taskdef name="testSpec" classname="com.agilent.wntd.webtest.ant.TestSpecificationTaskPlus">
	<classpath>
	<fileset dir="${lib}" includes="*.jar"/>
	</classpath>
	</taskdef>
Now extend TestStepSequence -- this is where you hook in altered or new functionality. For every new / modified element you want to handle you need to add a method in the form of "addElementName( <Desired Object Type )" ... this is driven by the Ant API. For example, if the only addition is <verifyimages> then the new class looks like: 
	public class TestStepSequencePlus extends TestStepSequence
	{
	/**
	* Checks for the existence of images and, optionally, whether the images have the width and height
	* attributes specified
	* @param step - a VerifyImages step
	*/
	public void addVerifyimages( VerifyImages step )
	{
	step.setStepType( "verifyimages" );
	addStep( step );
	}
	}
Of course, you have to also write the VerifyImages class.
One additional caveat: if you want your new functionality to be available "inside" other web test steps (such as <repeat>) you have to extend those steps to use "containers" that recognize your new steps. For example, to make <repeat> aware of <verifyimages>, add to TestStepSequencePlus: 
	public void addRepeat (RepeatStepPlus repeatStep)
	{
	RepeatWrapper step = new RepeatWrapper(repeatStep);
	step.setStepType("repeat");
	addStep(step);
	}
And, of course, you need to provide RepeatStepPlus -- in this case, unfortunately, I could not find a method that allowed me to simply extend RepeatStep ... so this is a copy of RepeatStep modified to extend TestStepSequencePlus instead of TestStepSequence -- this does introduce a maintenance problem, but less of one than modifying the original Web Test code. Hence the modified repeat step looks like 
	public class RepeatStepPlus extends TestStepSequencePlus implements IRepeatStep
	{
	private int fCount = -1;
	private String fCounterName = null; 
	/**
	* Just forward to superclass
	*/
	public RepeatStepPlus()
	{
	super();
	} 
	public void setCount(int count)
	{
	fCount = count;
	} 
	public int getCount()
	{
	return fCount;
	} 
	public String getCounterName()
	{
	return fCounterName;
	} 
	public void setCounterName(String counterName)
	{
	fCounterName = counterName;
	}
	}
Similarly, if you want to modify an existing step, extend that step and override the appropriate method(s), and add the addXxxx() method to TestStepSequencePlus.
Ideally Web Test would support "pluggable" components much the way Ant allows you to define any number of taskdefs and use those -- but I haven't found a way to do that yet (if anyone has figured out how to do that, I'd love to here from you!).
I hope this is useful to you.
Regards,

Stephen