001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.verify;
003:
004: import com.canoo.webtest.engine.StepFailedException;
005: import com.canoo.webtest.util.ConversionUtil;
006: import com.canoo.webtest.util.FormUtil;
007: import com.canoo.webtest.util.HtmlConstants;
008: import com.gargoylesoftware.htmlunit.html.HtmlElement;
009: import com.gargoylesoftware.htmlunit.html.HtmlForm;
010: import com.gargoylesoftware.htmlunit.html.HtmlOption;
011: import com.gargoylesoftware.htmlunit.html.HtmlSelect;
012: import org.apache.log4j.Logger;
013:
014: import java.io.IOException;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: /**
019: * Verifies that a select form field meets particular criteria
020: *
021: * @author unknown
022: * @author Marc Guillemot
023: * @author Paul King
024: * @webtest.step
025: * category="Core"
026: * name="verifySelectField"
027: * alias="verifyselectfield"
028: * description="This step verifies whether a particular SELECT - Option Selector exists and is marked as selected. The option selector may be chosen by either its <em>value</em> attribute or the <em>text</em> associated with the selector."
029: */
030: public class VerifySelectField extends AbstractVerifyFormStep {
031: private static final Logger LOG = Logger
032: .getLogger(VerifySelectField.class);
033:
034: private String fText;
035: private String fRegex;
036:
037: public String getText() {
038: return fText;
039: }
040:
041: /**
042: * @param text
043: * @webtest.parameter
044: * required="yes/no"
045: * description="The option text. Specification of either <em>text</em> or <em>value</em> is mandatory."
046: */
047: public void setText(final String text) {
048: fText = text;
049: }
050:
051: public String getRegex() {
052: return fRegex;
053: }
054:
055: /**
056: * @param regex
057: * @webtest.parameter
058: * required="no"
059: * default="'false'"
060: * description="Specifies whether the text or value represents a <key>regex</key>."
061: */
062: public void setRegex(final String regex) {
063: fRegex = regex;
064: }
065:
066: /**
067: * Finds the relevant form.
068: *
069: */
070: protected HtmlForm findForm() {
071: return FormUtil.findFormForField(getContext(), getFormName(),
072: HtmlConstants.SELECT, null, getName(), this );
073: }
074:
075: protected List findFields(final HtmlForm form) {
076: return form.getSelectsByName(getName());
077: }
078:
079: protected void verifyField(final HtmlElement field)
080: throws IOException {
081: final HtmlSelect curSelect = (HtmlSelect) field;
082: for (final Iterator iter = curSelect.getOptions().iterator(); iter
083: .hasNext();) {
084: final HtmlOption curOption = (HtmlOption) iter.next();
085:
086: if ((getText() == null || verifyStrings(getText(),
087: curOption.asText()))
088: && (getValue() == null || verifyStrings(getValue(),
089: curOption.getValueAttribute()))) {
090: LOG.debug("Found corresponding option " + curOption);
091: if (curOption.isSelected()) {
092: return;
093: }
094: throw new StepFailedException(getStepLabel() + ": "
095: + buildFailMessage(getValue()), this );
096: }
097: }
098:
099: // if we reach here, we haven't found anything
100: throw new StepFailedException("Select option \"" + fText
101: + " : " + getValue() + "\" not found for inputfield: <"
102: + getName() + ">", this );
103: }
104:
105: private boolean verifyStrings(final String expectedValue,
106: final String actualValue) {
107: return getVerifier(
108: ConversionUtil.convertToBoolean(getRegex(), false))
109: .verifyStrings(expectedValue, actualValue);
110: }
111:
112: private String buildFailMessage(final String value) {
113: final StringBuffer sb = new StringBuffer();
114: sb.append("Select option <");
115: sb.append("value:").append(value);
116: sb.append(" / text:").append(getText());
117: sb.append("> is not selected!");
118: return sb.toString();
119: }
120:
121: protected void verifyParameters() {
122: super .verifyParameters();
123: paramCheck(isValueNull() && getText() == null,
124: "Required parameter \"text\" or \"value\" not set!");
125: }
126:
127: }
|