[Webtest] Patch: Fix NPE on empty title

Dierk Koenig webtest@lists.canoo.com
Tue, 10 Jun 2003 10:08:43 +0200


ok. thanks
Mittie

P.S. I think it would be unwise to insist on a TestCase here :-)

> -----Original Message-----
> From: webtest-admin@lists.canoo.com
> [mailto:webtest-admin@lists.canoo.com]On Behalf Of Brian O'Kelley
> Sent: Montag, 9. Juni 2003 18:47
> To: Canoo Webtest Mailing List (E-mail)
> Subject: [Webtest] Patch: Fix NPE on empty title
>
>
> To reproduce this bug, make an HTML page with no title element and run a
> verifytitle test on it. It will crash with an NPE. The fix is below.
>
> Diff:
>
> Index: VerifyElementText.java
> ===================================================================
> RCS file:
> /repositories/cvs/public/webtest/src/com/canoo/webtest/steps/verif
> y/VerifyEl
> ementText.java,v
> retrieving revision 1.4
> diff -r1.4 VerifyElementText.java
> 115a116,118
> > 		if (node.getChildNodes().getLength() == 0) {
> > 			throw new StepFailedException(getFailedMessage("<no
> text>"), this);
> > 		}
>
>
>
> VerifyElementText.java:
> ------------------
> // Copyright  2002 Canoo Engineering AG, Switzerland.
> package com.canoo.webtest.steps.verify;
>
> import com.canoo.webtest.engine.Context;
> import com.canoo.webtest.engine.StepFailedException;
> import com.canoo.webtest.engine.StepExecutionException;
> import com.canoo.webtest.steps.verify.Verify;
> import org.w3c.dom.Node;
> import org.w3c.dom.NodeList;
>
> import java.util.*;
>
> /**
>  * This step verifies if the specified text (CDATA) is enclosed
>  * by an HTML element (tag) of the specified type. A name can
>  * be specified in case that the element in question appears
>  * multiple times. The text can be specified as regular expression.
>  *
>  * Ant name: verifyelementtext
>  * Parameter:
>  *	name (String): The name of the test step.
>  *	type (String): The HTML element (tag) name.
>  *  text (String): The expected text.
>  *  name (String): The name of the element (optional).
>  *  regex (Boolean): Defines if the text specifies a regular expression
> (optional).
>  *
>  * Example 1:
>  *     HTML source:
>  *    	 <title>The page title</title>
>  *
>  *     Possible statements:
>  *       <verifyelementtext type="title" text="The page title"/>
>  *       <verifyelementtext type="title" text=".*page title.*"
> regex="true"/>
>  *
>  * Example 2:
>  *     HTML source:
>  *    	 <textarea name="Hugo">
>  *			The very large text area named hugo.
>  *		 </textarea>
>  *
>  *     Possible statements:
>  *       <verifyelementtext type="textarea" name="Hugo" text="The
> very large
> text area named hugo."/>
>  *       <verifyelementtext type="textarea" name="Hugo"
> text=".*text area.*"
> regex="true"/>
>  *
>  */
> public class VerifyElementText extends Verify
> {
>
> 	private String fType;
> 	private String fName;
>
> 	public VerifyElementText ()
> 	{
> 		super();
> 	}
>
> 	public void doExecute (Context context)
> 	{
> 		verifyParameters();
>
> 		NodeList allElements = getAllElementsOfType(context, fType);
> 		if (allElements.getLength() > 1 && fName == null)
> 		{
> 			throw new StepFailedException("More than 1 element
> with type \"" + fType +
> 				"\" found! No name is specified.", this);
> 		}
>
> 		String textToVerify;
> 		if (fName != null)
> 		{
> 			ArrayList namedElements =
> findNodesWithAttribute(allElements, ELEMENT_ATTRIBUTE_NAME, fName);
> 			if (namedElements.size() == 0)
> 			{
> 				throw new StepFailedException("No element of
> type \"" + fType +
> 					"\" and name \"" + fName + "\"
> found!", this);
> 			}
> 			if (namedElements.size() > 1)
> 			{
> 				throw new StepFailedException("More than 1
> element of type \"" + fType +
> 					"\" with name \"" + fName + "\"
> found!", this);
> 			}
>
> 			textToVerify = getTextFromNode((Node)
> namedElements.get(0));
> 		}
> 		else
> 		{
> 			textToVerify = getTextFromNode(allElements.item(0));
> 		}
>
>         if (!verifyStrings(getText(), textToVerify))
> 		{
> 			throw new
> StepFailedException(getFailedMessage(textToVerify), this);
> 		}
>
> 	}
>
>     protected String getFailedMessage (String actualText)
> 	{
>
> 		StringBuffer sb = new StringBuffer();
> 		sb.append("Wrong contents found in HTML element (type=\"");
> 		sb.append(fType);
> 		sb.append("\", name=\"");
> 		sb.append(fName);
> 		sb.append("\")! Expected \"");
> 		sb.append(getText());
> 		sb.append("\" but got \"");
> 		sb.append(actualText);
> 		sb.append("\"");
>
> 		return sb.toString();
> 	}
>
> 	private String getTextFromNode (Node node)
> 	{
> 		if (node.getChildNodes().getLength() == 0) {
> 			throw new StepFailedException(getFailedMessage("<no
> text>"), this);
> 		}
>
> 		// Watchout: Assume that the one and only child node is the
> text!!
> 		return node.getChildNodes().item(0).getNodeValue();
> 	}
>
> 	public void setName (String newName)
> 	{
> 		fName = newName;
> 	}
>
> 	public void setType (String newType)
> 	{
> 		fType = newType;
> 	}
>
> 	public String toString ()
> 	{
>
> 		StringBuffer sb = new StringBuffer();
> 		sb.append(super.toString());
> 		sb.append(", type=\"");
> 		sb.append(fType);
> 		sb.append("\", name=\"");
> 		sb.append(fName);
> 		sb.append("\")");
> 		return sb.toString();
> 	}
>
> 	protected void verifyParameters ()
> 	{
>
> 		super.verifyParameters();
>
> 		if (fType == null)
> 			throw new StepExecutionException("Required parameter
> type not set!");
> 	}
>
>     public HashMap getParameterDictionary()
>     {
>         HashMap map = super.getParameterDictionary();
>         map.put("type", fType);
>         if(fName!=null)
>         {
>             map.put("name", fName);
>         }
>         return map;
>     }
>
>     public void expandProperties()
>     {
>         super.expandProperties();
>         fName = expandDynamicProperties(fName);
> 	    fType = expandDynamicProperties(fType);
>     }
>
>     public String getName()
>     {
>         return fName;
>     }
> }
>
>
> _______________________________________________
> WebTest mailing list
> WebTest@lists.canoo.com
> http://lists.canoo.com/mailman/listinfo/webtest
>