Code Search for Developers
 
 
  

HtmlUnitSetSelectFieldTag.java from Jameleon at Krugle


Show HtmlUnitSetSelectFieldTag.java syntax highlighted

/*
    Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
    Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
    
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
*/
package net.sf.jameleon.plugin.htmlunit.tags;

import net.sf.jameleon.plugin.htmlunit.HtmlUnitFunctionTag;

/**
 * This tag is used to select or unselect options in a select field.
 * <br/>
 * <p>
 * To use XPath to select a select field named sf_1 of an option with the value attribute of <b>sfo_1</b> that exists in a form with the name <b>testform</b>:<br/>
 * <pre><source>
 *     &lt;htmlunit-set-select-field
 *            functionId="Select the select field option with the value 'sfo_1'"
 *            xpath="//form[@name='testform']/select[@name='sf_1']/option[@value='sfo_1']"
 *            selected="true"/&gt;
 * </source></pre>
 * </p>
 * <p>
 * To use XPath to unselect a sf_1 select field option with the value attribute of <b>sfo_1</b> that exists in a form with the name <b>testform</b>:
 * <pre><source>
 *     &lt;htmlunit-set-select-field
 *            functionId="Select the select field option with the value 'sfo_1'"
 *            xpath="//form[@name='testform']/select[@name='sf_1']/option[@value='sfo_1']"
 *            selected="false"/&gt;
 * </source></pre>
 * </p>
 * <p>
 * To use the form, field name and option value to select an option with the value <b>sfo_1</b> that exists in a form with the name <b>testform</b>:
 * <pre><source>
 *     &lt;htmlunit-set-select-field
 *            functionId="select the select field option with the value attribute of 'sfo_1'"
 *            form="testform"
 *            fieldName="sf_1"
 *            optionValue="sfo_1"
 *            selected="true"/&gt;
 * </source></pre>
 * </p>
 * <p>
 * To use the form, field name and option text to select an option by it's displayed text <b>Select Me</b> that exists in a form with the name <b>testform</b>:
 * <pre><source>
 *     &lt;htmlunit-set-select-field
 *            functionId="select the select field option with the displayed text 'Select Me'"
 *            form="testform"
 *            fieldName="sf_1"
 *            optionText="Select Me"
 *            selected="true"/&gt;
 * </source></pre>
 * </p>
 * <p>
 * To use the form, field name and option text to select the 3rd option that exists in a form with the name <b>testform</b>:
 * <pre><source>
 *     &lt;htmlunit-set-select-field
 *            functionId="select the third select field option"
 *            form="testform"
 *            fieldName="sf_1"
 *            optionIndex="2"
 *            selected="true"/&gt;
 * </source></pre>
 * </p>
 * @jameleon.function name="htmlunit-set-select-field" type="action"
 */
public class HtmlUnitSetSelectFieldTag extends HtmlUnitFunctionTag{

    /**
     * The identifier of the form.
     * Value indentifiers are, the name, id, index or XPath expression
     * Only the form or xpath attribute can be set at once
     * @jameleon.attribute
     */
    protected String form;
    /**
     * The field name in the form.
     * @jameleon.attribute
     */
    protected String fieldName;
    /**
     * The the value of the option's value attribute
     * @jameleon.attribute
     */
    protected String optionValue;
    /**
     * The position of the option to select. The first displayed option would be 1
     * @jameleon.attribute default='-1'
     */
    protected int optionIndex;
    /**
     * The the displayed text of the option's value attribute
     * @jameleon.attribute
     */
    protected String optionText;
    /**
     * The xpath that matches the desired <b>option</b> to select.
     * @jameleon.attribute
     */
    protected String xpath;
    /**
     * The xpath that matches the desired <b>option</b> to select.
     * @jameleon.attribute required="true"
     */
    protected Boolean selected;

    public void testBlock(){
        boolean select = selected.booleanValue();
        if (xpath != null) {
            setSelectFieldByXPath(xpath, select);
        }else if (form != null) {
            assertNotNull("If you specify a form, you must also set the fieldName to set the value to", fieldName);
            setWorkingForm(form);
            if (optionValue != null) {
                setSelectFieldByValue(fieldName, optionValue, select);
            }else if (optionText != null) {
                setSelectFieldByOptionText(fieldName, optionText, select);
            }else if (optionIndex > -1) {
                setSelectFieldByIndex(fieldName, optionIndex, select);
            }else{
                fail("If you specify a form and fieldName, you must specify optionValue, optionText or optionIndex!");
            }
        }else{
            fail("You must specify either an xpath or a form");
        }
    }

}




See more files for this project here

Jameleon

Jameleon is a data-driven automated testing tool that is easily extensible via plug-ins. Features of applications are automated in Java and tied together independently in XML, creating self-documenting automated test cases.

Project homepage: http://sourceforge.net/projects/jameleon
Programming language(s): Java,XML
License: other

  AbstractHtmlUnitCheckFieldTag.java
  AbstractHtmlUnitSetFormFieldTag.java
  AbstractHtmlUnitSetInputFieldTag.java
  HtmlUnitClickTag.java
  HtmlUnitNavigateTag.java
  HtmlUnitRecordAlertsTag.java
  HtmlUnitSetCheckBoxTag.java
  HtmlUnitSetFileFieldTag.java
  HtmlUnitSetHiddenFieldTag.java
  HtmlUnitSetPasswordFieldTag.java
  HtmlUnitSetRadioButtonTag.java
  HtmlUnitSetSelectFieldTag.java
  HtmlUnitSetTextAreaTag.java
  HtmlUnitSetTextFieldTag.java
  HtmlUnitValidateTag.java