[Webtest] custom exportproperty step

Janno Kusman webtest@lists.canoo.com
Fri, 23 Jan 2004 14:47:52 +0200


This is a multi-part message in MIME format.
--------------080106010601040500080705
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I have written my custom step that exports WebTest dynamic property as 
ANT property.  It may be useful if you want to use ANT script task on 
WebTest extracted property or if you simply want to debug WebTest 
dynamic property value by using ANT echo task.

For building you need first to unzip src.zip in (WebTest home ) and copy 
attached ExportProperty.java to 
%WEBTEST.HOME%\src\com\canoo\webtest\extension

then run 'ant compile' and edit webtestTaskdefs.properties by adding 
following line to it:
exportproperty=com.canoo.webtest.extension.ExportProperty

Documentation:

*exportproperty*

Export WebTest dynamic property (which are not accesible outside WebTest 
testspec) into ANT project properties

Attributes:
name - The name of ANT project property that will be created
value - WebTest dynamic property name to export as #{xxx}

Example:

<testSpec name="...">
   <config ... />
   <steps>
      <invoke ... />
        <storexpath
            stepid="Extract href of first link on page"
            xpath="//a[1]/@href"
            property="uri" />
        <invoke
            stepid="Invoke target page specified in href"
            url="#{uri}" />
       
        <exportproperty name="first_link" value="#{uri}"/>
      ...
   </steps>
</testSpec>

<echo message="First link: ${first_link}"/>

BR,
Janno Kusman

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

package com.canoo.webtest.extension;

import org.apache.tools.ant.Project;

import com.canoo.webtest.engine.Context;
import com.canoo.webtest.engine.StepExecutionException;
import com.canoo.webtest.steps.Step;

import java.util.HashMap;
import java.util.Map;


 /**
  * Exports WebTest specific dynamic property as ANT project property
  *
  * Attributes
  *      stepid      The name of this test step                   optional
  *      name        The name of ANT property                     mandatory
  *      value       The name of the dynamic property to export   mandatory
  *
  * @author  Janno Kusman (jannokusman@hot.ee)
  * @version 1.0, 23 Januar 2004
  */


public class ExportProperty extends Step
{

	private String fName;
	private String fValue;

	/**
	 * Perform the step's actual work.
	 * @param context Contains references to lastResponse, configuration, etc.
	 * @throws com.canoo.webtest.engine.StepFailedException if step was not successful
	 */
	public void doExecute(Context context)
	{
		verifyParameters();
		// execution goes here
                
                String propertyValue = expandDynamicProperties(getValue());
                
                Project antProject = getProject();
                
                //sets property only, if its not already set
                antProject.setNewProperty(getName(), propertyValue );
	}

	/**
	 * Ant does not support mixed-case attribute names. All characters are lower
	 * case except the first one.
	 */
	public void setName(String myValue)
	{
		fName = myValue;
	}
        
        public String getName()
        {
            return fName;
        }

	public void setValue(String myValue)
	{
		fValue = myValue;
	}
        
        public String getValue()
        {
            return fValue;
        }

	/**
	 * Verify that name and value parameters are set
	 * @throws StepExecutionException if a mandatory attribute is not set
	 */
	protected void verifyParameters()
	{
		if (fName == null || getName().length() < 1)
			throw new StepExecutionException("Property name must be at least one character!");
                
                if (fValue == null)
			throw new StepExecutionException("Required parameter value not set!");

	}

	/**
	 * override to add actual attribute values to the reporting
	 */
	public Map getParameterDictionary()
	{
		Map map = super.getParameterDictionary();               // general attributes (stepId)
		map.put("name", getName());     // cannot be null (mandatory)
                map.put("value", getValue());     // cannot be null (mandatory)
		return map;
	}

	/**
	 * Hook for property expansion in our parameters
	 */
	public void expandProperties()
	{
		super.expandProperties();
		fName = expandDynamicProperties(getName());
		fValue = expandDynamicProperties(getValue());
	}
}

--------------080106010601040500080705--