[Webtest] Patch: Fix NPE on empty title

Brian O'Kelley webtest@lists.canoo.com
Mon, 9 Jun 2003 12:46:37 -0400


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:=20

Index: VerifyElementText.java
=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
RCS file:
/repositories/cvs/public/webtest/src/com/canoo/webtest/steps/verify/Veri=
fyEl
ementText.java,v
retrieving revision 1.4
diff -r1.4 VerifyElementText.java
115a116,118
> 		if (node.getChildNodes().getLength() =3D=3D 0) {
> 			throw new StepFailedException(getFailedMessage("<no
text>"), this);
> 		}



VerifyElementText.java:
------------------
// Copyright =A9 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=3D"title" text=3D"The page title"/>
 *       <verifyelementtext type=3D"title" text=3D".*page title.*"
regex=3D"true"/>
 *
 * Example 2:
 *     HTML source:
 *    	 <textarea name=3D"Hugo">
 *			The very large text area named hugo.
 *		 </textarea>
 *
 *     Possible statements:
 *       <verifyelementtext type=3D"textarea" name=3D"Hugo" text=3D"The =
very large
text area named hugo."/>
 *       <verifyelementtext type=3D"textarea" name=3D"Hugo" =
text=3D".*text area.*"
regex=3D"true"/>
 *
 */
public class VerifyElementText extends Verify
{

	private String fType;
	private String fName;

	public VerifyElementText ()
	{
		super();
	}

	public void doExecute (Context context)
	{
		verifyParameters();

		NodeList allElements =3D getAllElementsOfType(context, fType);
		if (allElements.getLength() > 1 && fName =3D=3D null)
		{
			throw new StepFailedException("More than 1 element
with type \"" + fType +
				"\" found! No name is specified.", this);
		}

		String textToVerify;
		if (fName !=3D null)
		{
			ArrayList namedElements =3D
findNodesWithAttribute(allElements, ELEMENT_ATTRIBUTE_NAME, fName);
			if (namedElements.size() =3D=3D 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 =3D getTextFromNode((Node)
namedElements.get(0));
		}
		else
		{
			textToVerify =3D getTextFromNode(allElements.item(0));
		}

        if (!verifyStrings(getText(), textToVerify))
		{
			throw new
StepFailedException(getFailedMessage(textToVerify), this);
		}

	}

    protected String getFailedMessage (String actualText)
	{

		StringBuffer sb =3D new StringBuffer();
		sb.append("Wrong contents found in HTML element (type=3D\"");
		sb.append(fType);
		sb.append("\", name=3D\"");
		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() =3D=3D 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 =3D newName;
	}

	public void setType (String newType)
	{
		fType =3D newType;
	}

	public String toString ()
	{

		StringBuffer sb =3D new StringBuffer();
		sb.append(super.toString());
		sb.append(", type=3D\"");
		sb.append(fType);
		sb.append("\", name=3D\"");
		sb.append(fName);
		sb.append("\")");
		return sb.toString();
	}

	protected void verifyParameters ()
	{

		super.verifyParameters();

		if (fType =3D=3D null)
			throw new StepExecutionException("Required parameter
type not set!");
	}

    public HashMap getParameterDictionary()
    {
        HashMap map =3D super.getParameterDictionary();
        map.put("type", fType);
        if(fName!=3Dnull)
        {
            map.put("name", fName);
        }
        return map;
    }

    public void expandProperties()
    {
        super.expandProperties();
        fName =3D expandDynamicProperties(fName);
	    fType =3D expandDynamicProperties(fType);
    }

    public String getName()
    {
        return fName;
    }
}