[Webtest] Refresh-tasks

Parker, Michael webtest@lists.canoo.com
Wed, 28 Jul 2004 09:47:49 +0200


Hallo,


to avoid problems with less supported javascipt we added a Refresch task:

It's a supplement to the deliverd classes as far as we understood:
invoke=20
	puts a request ab
	most of the other tasks work on the resonse
Clickbutton maps the funcitionality of a Submit-Buttons
clicklink verifies links

The task refresh just requests an actualization of ther response.

So we can force some "dynamics" between our input-fields which is not always
guaranteed by the js.jar.

We also had to patch httpunit to work:

The deliverd httpunit-1.5.4.jar
gives us a

compile:
    [mkdir] Created dir: D:\webentw\canoo\canoo468\build\classes
    [javac] Compiling 196 source files to
D:\webentw\canoo\canoo468\build\classes
    [javac]
D:\webentw\canoo\canoo468\src\com\canoo\webtest\steps\request\Refresh.java:8
6: cannot resolve symbol
    [javac] symbol  : method getRequestByForm ()
    [javac] location: class com.meterware.httpunit.WebForm
    [javac]                             WebRequest req =3D
form.getRequestByForm();
    [javac]                                                      ^
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -deprecation for details.
    [javac] 1 error

So we added the method

	/*
		 submit without button-event to simulate
		 'onFocusChanged"
	*/
	public WebRequest getRequestByForm() {
	  if (getMethod().equalsIgnoreCase( "post" )) {
			return new PostMethodWebRequest( this );
		} else {
			return new GetMethodWebRequest( this );
		}
	}


Is this to complicate, is there an easier way to do  or could this be a
contribution to canoo ?

Michael



// Copyright =A9 2002 Canoo Engineering AG, Switzerland.
package com.canoo.webtest.steps.request;

import com.canoo.webtest.engine.Context;
import com.canoo.webtest.engine.StepExecutionException;
import com.canoo.webtest.engine.StepFailedException;
import com.canoo.webtest.interfaces.IButtonLocator;
import com.canoo.webtest.interfaces.IFormLocator;
import com.canoo.webtest.interfaces.IIndexLocator;
import com.canoo.webtest.self.Block;
import com.canoo.webtest.steps.ParameterHolder;
import com.canoo.webtest.steps.Step;
import com.canoo.webtest.steps.locator.*;
import com.meterware.httpunit.*;
import org.xml.sax.SAXException;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/*********************************************************
*  Klasse au=DFerhalb der canoo-Distribution
*  bewirkt submit ohne Button-Event
*  fordert Aktualisierung des aktuellen Response an
**********************************************************/


public class Refresh extends Target
{

	IFormLocator fFormLocator =3D null;
=09
	/**
	 * C'tor used for instance creation as nested element by ant
	 */
	public Refresh()
	{
		super();
	}

=09

	/**
	 * Gotcha: Use the implementation and *not* the interface for
FormLocator
	 * or a "strange" ant introspection exception will occur ...
	 */
	public void addForm(FormLocator locator)
	{
		fFormLocator =3D locator;
	}

	/**
	 * Use the locator mechanism to get to the required request by using
the configured
	 * locators (if any). Configure the generated request with
parameters.
	 *
	 * @throws com.canoo.webtest.engine.StepFailedException if the
locator has a problem (e.g. no button found, multiple
	 * buttons found, ...) or if a problem with parameters exists.
	 */
	public void doExecute(Context context) throws Exception
	{
		super.doExecute(context);

		try
		{
			WebResponse resp=3Dcontext.getLastResponse();
			WebForm form =3D resp.getForms()[0];
			setParameters(context, form);
			gotoTarget(context, form);
		}
		catch (Exception error)
		{
			throw new StepFailedException(error.getMessage(),
this);
		}
	}


	protected WebResponse gotoTarget(final Context context, final
WebForm form) throws Exception
	{
	=09
		return protectedGoto(context, "refresh", new Block(){
			public void call() throws Exception
			{
				prepareConversation(context);
				WebRequest req =3D form.getRequestByForm();
=09
setIntermediateResponse(context.getWebConversation().getResponse(req));
			}
		});
	}

=09

	/**
	 * Add all parameters collected in the nextParameters HashMap to the
request and remove
	 * all parameters from the request that are listed in the
resetNextParameters list.
	 * Set also the click position if coordinates are configured.
	 *
	 * @param context The current test context
	 * @param form The form to add/remove parameters from
	 *
	 * @throws com.canoo.webtest.engine.StepFailedException if a
parameter to remove is not present in the request,
	 * if a parameter is not available in the form
	 * or if click positions are configured and target button is no
image button
	 */
	protected void setParameters(Context context, WebForm form) throws
StepFailedException
	{
		for (Iterator i =3D
context.getNextParameters().entrySet().iterator(); i.hasNext();)
		{
			ParameterHolder parameter =3D (ParameterHolder)
((Map.Entry) i.next()).getValue();
			try
			{
				addParameter(form, parameter);
			}
			catch (IllegalRequestParameterException irpe)
			{
				throw new StepFailedException("No such
parameter in form: " + parameter.getName(), this);
			}
		}

		for (Iterator i =3D
context.getNextResetParameters().iterator(); i.hasNext();)
		{

			String resetParameterName =3D (String) i.next();
			if
(form.getParameterValues(resetParameterName).length > 0)
			{
				form.removeParameter(resetParameterName);
			}
			else
			{
				throw new StepFailedException("Could not
remove parameter from request: " + resetParameterName, this);
			}
		}
	}

=09

	/**
	 * Add a new multivalue parameter to the request. Add the new values
to the existing ones if the parameter
	 * exists already in the request.
	 *
	 * @param form The WebForm to which the parameter shall be added
	 * @param parameterHolder The parameterHolder representing the
parameter
	 */
	private void addParameter(WebForm form, ParameterHolder
parameterHolder)
	{
		List parameterValues =3D parameterHolder.getValueList();
		if (parameterHolder.isPreserveExistingValue())
		{
			String[] existingParameters =3D
form.getParameterValues(parameterHolder.getName());
=09
parameterValues.addAll(Arrays.asList(existingParameters));
		}
		form.setParameter(parameterHolder.getName(),
		    (String[]) parameterValues.toArray(new
String[parameterValues.size()]));
	}

=09
=09
}

_______________________________________________
AXA Service AG
MICHAEL PARKER
PROGRAMMER/ANALYST
Colonia-Allee 10-20; D-51067 K=F6ln
Tel.: +49 (0)221 - 148 - 24 173
Fax:  +49 (0)221 - 148 - 44 - 42 663
E-Mail: mailto:Michael.Parker@axa.de
_______________________________________________



---------------------------------------------------------------------------=
---
Aus Rechts- und Sicherheitsgruenden ist die in dieser E-Mail gegebene Infor=
mation nicht rechtsverbindlich. Eine rechtsverbindliche Bestaetigung reiche=
n wir Ihnen gerne auf Anforderung in schriftlicher Form nach. Beachten Sie =
bitte, dass jede Form der unautorisierten Nutzung, Veroeffentlichung, Vervi=
elfaeltigung oder Weitergabe des Inhalts dieser E-Mail nicht gestattet ist.=
 Diese Nachricht  ist ausschliesslich fuer den bezeichneten Adressaten oder=
 dessen Vertreter bestimmt. Sollten Sie nicht der vorgesehene Adressat dies=
er E-Mail oder dessen Vertreter sein, so bitten wir Sie, sich mit dem Absen=
der der E-Mail in Verbindung zu setzen.
----------------------------
For legal and security reasons the information provided in this e-mail is n=
ot legally binding. Upon request we would be pleased to provide you with a =
legally binding confirmation in written form. Any form of unauthorised use,=
 publication, reproduction, copying or disclosure of the content of this e-=
mail is not permitted. This message is exclusively for the person addressed=
 or their representative. If you are not the intended recipient of this mes=
sage and its contents, please notify the sender immediately.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D