001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.verify;
003:
004: import com.canoo.webtest.engine.Context;
005: import com.canoo.webtest.engine.StepFailedException;
006: import com.gargoylesoftware.htmlunit.html.HtmlElement;
007: import com.gargoylesoftware.htmlunit.html.HtmlPage;
008:
009: import java.util.Iterator;
010: import java.util.List;
011:
012: /**
013: * @author unknown
014: * @author Marc Guillemot
015: * @webtest.step category="Core"
016: * name="verifyElement"
017: * alias="verifyelement"
018: * description="This step verifies the existence of a particular <key>HTML</key> element (tag) in the result page. It is identified by its <key>HTML</key> element type (e.g. TITLE, HEAD, etc.) and the <em>text</em> value of the NAME attribute."
019: */
020: public class VerifyElement extends AbstractVerifyTextStep {
021: private String fType;
022:
023: public String getType() {
024: return fType;
025: }
026:
027: /**
028: * @webtest.parameter required="yes"
029: * description="The <key>HTML</key> element type, e.g. "INPUT" for a <INPUT.../> tag or "title" for <title>...</title>."
030: */
031: public void setType(final String newType) {
032: fType = newType;
033: }
034:
035: public void doExecute() {
036: int numberOfHits = getNumberOfHits();
037:
038: if (numberOfHits > 1) {
039: throw new StepFailedException(
040: "More than 1 element with type \"" + fType
041: + "\" and name \"" + getText()
042: + "\" found!", this );
043: }
044: if (numberOfHits == 0) {
045: throw new StepFailedException("No element of type \""
046: + fType + "\" and name \"" + getText()
047: + "\" found!", this );
048: }
049: }
050:
051: /**
052: * Gets the number of hits in the current response.
053: * @deprecated Use {@link #getNumberOfHits()}.
054: * @param context The execution context.
055: * @return The number of hits.
056: */
057: protected int getNumberOfHits(final Context context) {
058: return getNumberOfHits();
059: }
060:
061: protected int getNumberOfHits() {
062: Context context = getContext();
063: int numberOfHits = findElementAttributeValue(context,
064: getType(), ELEMENT_ATTRIBUTE_NAME, getText());
065:
066: if (numberOfHits == 0) {
067: numberOfHits = findElementAttributeValue(context,
068: getType(), ELEMENT_ATTRIBUTE_ID, getText());
069: }
070: return numberOfHits;
071: }
072:
073: protected void verifyParameters() {
074: super .verifyParameters();
075: nullParamCheck(getType(), "type");
076: }
077:
078: private int findElementAttributeValue(final Context context,
079: final String elementType, final String attributeName,
080: String value) {
081: final List li = getAllElementsOfType(context, elementType
082: .toLowerCase());
083: return getNumberOfOccurances(li, value, attributeName);
084: }
085:
086: /**
087: * Gets all elements of the given type from the current html response
088: *
089: * @param context
090: * @param type
091: * @return a list of {@link com.gargoylesoftware.htmlunit.html.HtmlElement}
092: */
093: protected static List getAllElementsOfType(final Context context,
094: final String type) {
095: return ((HtmlPage) context.getCurrentResponse())
096: .getDocumentHtmlElement()
097: .getHtmlElementsByTagName(type);
098: }
099:
100: /**
101: * Gets the number of elements having the value of the specified attribute matching the test
102: * @param li
103: * @param expectedString
104: * @param attributeName
105: */
106: protected int getNumberOfOccurances(final List li,
107: final String expectedString, final String attributeName) {
108: int numberOfHits = 0;
109: for (Iterator iter = li.iterator(); iter.hasNext();) {
110: HtmlElement elt = (HtmlElement) iter.next();
111: if (verifyStrings(expectedString, elt
112: .getAttributeValue(attributeName))) {
113: ++numberOfHits;
114: }
115: }
116: return numberOfHits;
117: }
118: }
|