001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.form;
003:
004: import com.canoo.webtest.engine.Context;
005: import com.canoo.webtest.engine.StepFailedException;
006: import com.canoo.webtest.steps.Step;
007: import com.canoo.webtest.util.FormUtil;
008: import com.gargoylesoftware.htmlunit.html.HtmlForm;
009: import com.gargoylesoftware.htmlunit.html.HtmlPage;
010: import org.apache.log4j.Logger;
011: import org.apache.commons.lang.StringUtils;
012:
013: /**
014: * Provides the ability to select a form as the current form for setting fields in this form
015: * in subsequent steps.
016: *
017: * @author Marc Guillemot
018: * @webtest.step
019: * category="Core"
020: * name="selectForm"
021: * alias="new_selectform,selectform"
022: * description="This step can be used to specify a form in the document as the <em>current form</em>. The <em>current form</em> is given precedence when locating the form to use in form-related steps."
023: */
024: public final class SelectForm extends Step {
025: private static final Logger LOG = Logger
026: .getLogger(SelectForm.class);
027: private String fIndex;
028: private String fName;
029:
030: /**
031: * Select a form as the current one.
032: *
033: * @throws com.canoo.webtest.engine.StepExecutionException if no form is found
034: */
035: public void doExecute() throws Exception {
036: nullResponseCheck();
037: final Context context = getContext();
038: final HtmlPage currentResp = (HtmlPage) context
039: .getCurrentResponse();
040: LOG.debug("Selecting current form in response from "
041: + currentResp.getWebResponse().getUrl());
042: HtmlForm form;
043: if (fName != null) {
044: form = FormUtil.findFormByName(currentResp, fName);
045: } else {
046: form = FormUtil.findFormByIndex(currentResp, fIndex);
047: }
048: if (form == null) {
049: throw new StepFailedException("Form not found in doc", this );
050: }
051: context.setCurrentForm(form);
052: }
053:
054: /**
055: * Set the index.
056: *
057: * @param index
058: * @webtest.parameter
059: * required="yes/no"
060: * description="The <em>index</em> of the form to select (starting at 0). Either <em>name</em> or <em>index</em> is required."
061: */
062: public void setIndex(final String index) {
063: fIndex = index;
064: }
065:
066: public String getIndex() {
067: return fIndex;
068: }
069:
070: /**
071: * Set the name.
072: *
073: * @param name
074: * @webtest.parameter
075: * required="yes/no"
076: * description="The <em>name</em> of the form to select (i.e. the value of the NAME attribute in <form name='foo' ... >). Either <em>name</em> or <em>index</em> is required."
077: */
078: public void setName(final String name) {
079: fName = name;
080: }
081:
082: public String getName() {
083: return fName;
084: }
085:
086: protected void verifyParameters() {
087: super .verifyParameters();
088: paramCheck(StringUtils.isEmpty(fName)
089: && StringUtils.isEmpty(fIndex),
090: "Either \"name\" or \"index\" must be set");
091: paramCheck(!StringUtils.isEmpty(fName)
092: && !StringUtils.isEmpty(fIndex),
093: "Only one of \"name\" and \"index\" must be set");
094: optionalIntegerParamCheck(fIndex, "index", false); // if exists, must be integer
095: }
096:
097: /**
098: * Called by Ant to set the text nested between opening and closing tags.
099: * @param text the text to set
100: * @webtest.nested.parameter
101: * required="no"
102: * description="Alternative way to set the 'name' attribute."
103: */
104: public void addText(final String text) {
105: setName(text);
106: }
107: }
|