Code Search for Developers
 
 
  

HtmlUnitFunctionTag.java from Jameleon at Krugle


Show HtmlUnitFunctionTag.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;


import java.io.IOException;

import junit.framework.Assert;
import net.sf.jameleon.function.FunctionTag;
import net.sf.jameleon.plugin.htmlunit.util.HtmlUnitHelper;
import net.sf.jameleon.util.AssertLevel;
import net.sf.jameleon.util.JameleonUtility;

import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public abstract class HtmlUnitFunctionTag extends FunctionTag{

    protected HtmlUnitHelper helper;
    protected HtmlUnitSessionTag sessionTag;
    protected HtmlForm workingForm;

    /**
     * Navigates to the provided URL
     * @param url - the url to navigate to.
     */
    public void navigate(String url){
        helper.navigate(url);
    }

    /**
     * Gets the references of the session and the test results from the parent TestCase
     * This method gets called once all attributes are set from the macro language. Any required
     * setup should go here.
     */
    public void setupEnvironment() {
        super.setupEnvironment();
        Object obj = findAncestorWithClass(HtmlUnitSessionTag.class);
        if (obj instanceof HtmlUnitSessionTag) {
            sessionTag = (HtmlUnitSessionTag) obj;
        } else {
            throw new ClassCastException("Can only execute an HtmlUnit function tag under the HtmlUnit session tag ( htmlunit-session )! " +
                    "Please change the session tag surrounding function point " + getClass().getName());
        }
        helper = new HtmlUnitHelper(sessionTag);
    }

    public void store(String fName, int event) throws IOException{
        getFunctionResults().setErrorFile(helper.store(fName));
    }

    // ASSERT METHODS


    /**
     * Validates that the provided text exists on the page.
     *
     * @param expectedText expected text
     */
    public void assertTextPresent(final String expectedText) {
        String msg = "assertTextPresent: ";
        int assertLevel = AssertLevel.NO_LEVEL;
        assertTextPresent(msg, expectedText, assertLevel);
    }                                                                       

    /**
     * Validates that the title of the current html page matches the expected title
     * If the test fails display the given <code>message</code>
     *
     * @param msg  - The message to display if <code>text</code> is not in the response.
     * @param expectedText expected text
     */
    public void assertTextPresent(final String msg, final String expectedText) {
        int assertLevel = AssertLevel.NO_LEVEL;
        assertTextPresent(msg, expectedText, assertLevel);
    }                                                                       

    /**
     * Validates that the title of the current html page matches the expected title
     *
     * @param expectedText expected text
     * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
     */
    public void assertTextPresent(final String expectedText, int assertLevel) {
        String msg = "assertTextPresent: ";
        assertTextPresent(msg, expectedText, assertLevel);
    }                                                                       

    /**
     * Validates that the title of the current html page matches the expected title
     * If the test fails display the given <code>message</code>
     *
     * @param msg  - The message to display if <code>text</code> is not in the response.
     * @param expectedText expected text
     * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
     */
    public void assertTextPresent(final String msg, final String expectedText, int assertLevel) {
        if ( expectedText == null ) {
            fail("assertTextPresent: expectedText was null!", assertLevel);
        }
        assertMethodWithLevel(new Runnable() {
                                  public void run() {
                                      String html = helper.getCurrentPageContent();
                                      if (html != null) {
                                          Assert.assertTrue(JameleonUtility.createErrMsg(msg), html.indexOf(expectedText) >= 0);
                                      }else{
                                          fail("There is no current page to validate");
                                      }
                                  }
                              }, assertLevel);
    }                                                                       

    /**
     * Validates that the title of the current html page matches the expected title
     *
     * @param expectedTitle expected title value
     */
    public void assertTitleEquals(final String expectedTitle) {
        String msg = "assertTitleEquals: ";
        int assertLevel = AssertLevel.NO_LEVEL;
        assertTitleEquals(msg, expectedTitle, assertLevel);
    }                                                                       

    /**
     * Validates that the title of the current html page matches the expected title
     * If the test fails display the given <code>message</code>
     *
     * @param msg  - The message to display if <code>text</code> is not in the response.
     * @param expectedTitle expected title value
     */
    public void assertTitleEquals(final String msg, final String expectedTitle) {
        int assertLevel = AssertLevel.NO_LEVEL;
        assertTitleEquals(msg, expectedTitle, assertLevel);
    }                                                                       

    /**
     * Validates that the title of the current html page matches the expected title
     *
     * @param expectedTitle expected title value
     * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
     */
    public void assertTitleEquals(final String expectedTitle, int assertLevel) {
        String msg = "assertTitleEquals: ";
        assertTitleEquals(msg, expectedTitle, assertLevel);
    }                                                                       

    /**
     * Validates that the title of the current html page matches the expected title
     * If the test fails display the given <code>message</code>
     *
     * @param msg  - The message to display if <code>text</code> is not in the response.
     * @param expectedTitle expected title value
     * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
     */
    public void assertTitleEquals(final String msg, final String expectedTitle, int assertLevel) {
        if ( expectedTitle == null ) {
            fail("assertTitleEquals: Title was null!", assertLevel);
        }
        
        assertMethodWithLevel(new Runnable() {
                                  public void run() {
                                      HtmlPage page = (HtmlPage)helper.getCurrentPage();
                                      if (page != null) {
                                          Assert.assertEquals(
                                              JameleonUtility.createErrMsg(msg),
                                              expectedTitle,
                                              page.getTitleText());
                                      }else{
                                          fail("There is no current page to validate");
                                      }
                                  }
                              }, assertLevel);
    }                                                                       

    /**
     * Validates that the provided xpath matches against the currently active page
     *
     * @param xpath The XPath to match
     */
    public void assertXPathMatches(final String xpath) {
        String msg = "assertXPathMatches: ";
        int assertLevel = AssertLevel.NO_LEVEL;
        assertXPathMatches(msg, xpath, assertLevel);
    }                                                                       

    /**
     * Validates that the provided xpath matches against the currently active page
     * If the test fails display the given <code>message</code>
     *
     * @param msg  - The message to display if <code>text</code> is not in the response.
     * @param xpath The XPath to match
     */
    public void assertXPathMatches(final String msg, final String xpath) {
        int assertLevel = AssertLevel.NO_LEVEL;
        assertXPathMatches(msg, xpath, assertLevel);
    }                                                                       

    /**
     * Validates that the provided xpath matches against the currently active page
     *
     * @param xpath The XPath to match
     * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
     */
    public void assertXPathMatches(final String xpath, int assertLevel) {
        String msg = "assertXPathMatches: ";
        assertXPathMatches(msg, xpath, assertLevel);
    }                                                                       

    /**
     * Validates that the provided xpath matches against the currently active page
     * If the test fails display the given <code>message</code>
     *
     * @param msg  - The message to display if <code>text</code> is not in the response.
     * @param xpath The XPath to match
     * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
     */
    public void assertXPathMatches(final String msg, final String xpath, int assertLevel) {
        if ( xpath == null ) {
            fail("assertXPathMatches: xpath was null!", assertLevel);
        }
        assertMethodWithLevel(new Runnable() {
                                  public void run() {
                                      Assert.assertTrue(msg, helper.xPathMatches(xpath));
                                  }
                              }, assertLevel);
    }  

    // Helper methods.

    /**
     *  Clicks on the HTML element matching the provided XPath 
     * @param xpath - the XPath expression that defines the HTML element to click.
     */
    public void clickElementWithXPath(String xpath){
        helper.clickElementWithXPath(xpath);
    }

    /**
     * This is a power method to get a form element by the following order.
     * <ol>
     *     <li>getHtmlFormById</li>
     *     <li>getHtmlFormByName</li>
     *     <li>getHtmlFormByIndex</li>
     *     <li>getHtmlElementByXPath</li>
     * </ol>
     * @param idNameIndexOrXpath - The id, name, index or XPath expression defining the location of the form
     * 
     * @return HtmlForm
     */
    public HtmlForm getHtmlForm(String idNameIndexOrXpath){
        HtmlForm form = null;
        if (idNameIndexOrXpath == null) {
            fail("idNameIndexOrXpath must not be null");
        }
        form = getHtmlFormById(idNameIndexOrXpath);
        if (form == null) {
            form = getHtmlFormByName(idNameIndexOrXpath);
            if (form == null) {
                try{
                    int index = Integer.parseInt(idNameIndexOrXpath);
                    form = getHtmlFormByIndex(index);
                }catch (NumberFormatException nfe){
                    //Do nothing. This just means a number wasn't passed.
                }
                if (form == null) {
                    form = getHtmlFormByXPath(idNameIndexOrXpath);
                }
            }
        }
        return form;
    }

    /**
     * Gets a form element back by its id attribute.
     * @param attributeValue - the value of the id attribute.
     * 
     * @return HtmlForm
     */
    public HtmlForm getHtmlFormById(String attributeValue){
        return helper.getHtmlFormById(attributeValue);
    }

    /**
     * Gets a form element back by its index or location on the page.
     * For the first form on the page, pass in '1'
     * @param index - the nth form on the page
     * 
     * @return HtmlForm
     */
    public HtmlForm getHtmlFormByIndex(int index){
        return helper.getHtmlFormByIndex(index);
    }

    /**
     * Gets a form element back by its name attribute.
     * @param attributeValue - the value of the name attribute.
     * 
     * @return HtmlForm
     */
    public HtmlForm getHtmlFormByName(String attributeValue){
        return helper.getHtmlFormByName(attributeValue);
    }

    /**
     * Gets a form element back by an XPath expression
     * @param xpath - The XPath expression matching the desired form
     * 
     * @return HtmlForm
     */
    public HtmlForm getHtmlFormByXPath(String xpath){
        return helper.getHtmlFormByXPath(xpath);
    }

    /**
     * Gets an HtmlElement matching the provided xpath expression
     * @param xpath - An XPath expression that matches the desired HtmlElement
     * 
     * @return The matching HtmlElement
     */
    public HtmlElement getHtmlElementByXPath(String xpath){
        return helper.getHtmlElementByXPath(xpath);
    }

    /**
     * Gets an HTMLElement by the tag name, attribute name and the attribute value.
     * This then in turn gets translated to an XPath expression.
     * An example of this might be to get a form with its id='ten'.
     * @param tagname - The name of the tag to get back. In the above example, the value would be 'form'
     * @param attributeName - The name of attribute to check against. In the above example, the value would be 'id'
     * @param attributeValue - The value of the attribute to check against. In the above example, the value would be 'ten'
     * 
     * @return HtmlElement - An element that matches the criteria.
     */
    public HtmlElement getHtmlElementByAttributeNameAndValue(String tagname, String attributeName, String attributeValue){
        return helper.getHtmlElementByAttributeNameAndValue(tagname, attributeName, attributeValue);
    }

    /**
     * Gets the current working form or null if one hasn't been set.
     * 
     * @return HtmlForm - The current working form
     */
    public HtmlForm getWorkingForm(){
        return workingForm;
    }

    /**
     * Checks or unchecks the checkbox that either exists in the workingForm or in the current page
     * @param fieldName - The name of the checkbox to check
     * @param checked - set to <code>true</code> to check the checkbox and to <code>false</code> to uncheck it
     */
    public void setCheckBox(String fieldName, boolean checked){
        helper.setCheckBox(fieldName, checked);
    }

    /**
     * Checks or unchecks the checkbox that either exists in the workingForm or in the current page
     * @param fieldName - The name of the checkbox to check
     * @param fieldValue - The value of the checkbox to check
     * @param checked - set to <code>true</code> to check the checkbox and to <code>false</code> to uncheck it
     */
    public void setCheckBox(String fieldName, String fieldValue, boolean checked){
        helper.setCheckBox(fieldName, fieldValue, checked);
    }

    /**
     * Sets the file field that either exists in the workingForm or in the current page
     * @param fieldName - The name of the input field to set the value of
     * @param value - The value to set the input field to
     */
    public void setFileField(String fieldName, String value){
        helper.setFileField(fieldName, value);
    }

    /**
     * Sets the hidden field that either exists in the workingForm or in the current page
     * @param fieldName - The name of the input field to set the value of
     * @param value - The value to set the input field to
     */
    public void setHiddenField(String fieldName, String value){
        helper.setHiddenField(fieldName, value);
    }

    /**
     * Sets the password field that either exists in the workingForm or in the current page
     * @param fieldName - The name of the input field to set the value of
     * @param value - The value to set the input field to
     */
    public void setPasswordField(String fieldName, String value){
        helper.setPasswordField(fieldName, value);
    }

    /**
     * Checks or unchecks the radio button that either exists in the workingForm or in the current page.
     * @param fieldName - The name of the radio button to check
     * @param fieldValue - The value of the radio button to check
     * @param checked - set to <code>true</code> to check the radio button and to <code>false</code> to uncheck it
     */
    public void setRadioButton(String fieldName, String fieldValue, boolean checked){
        helper.setRadioButton(fieldName, fieldValue, checked);
    }

    /**
     * Selects or unselects the option by its index order that either exists in the workingForm or in the current page.
     * @param fieldName - The name of the select field to select
     * @param index - The nth option in the list. 1st option = 0
     * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
     */
    public void setSelectFieldByIndex(String fieldName, int index, boolean selected){
        helper.setSelectFieldByIndex(fieldName, index, selected);
    }

    /**
     * Selects or unselects the option with the displayed text that either exists in the workingForm or in the current page.
     * @param fieldName - The name of the select field to select
     * @param optionText - The text displayed in the drop-down
     * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
     */
    public void setSelectFieldByOptionText(String fieldName, String optionText, boolean selected){
        helper.setSelectFieldByOptionText(fieldName, optionText, selected);
    }

    /**
     * Selects or unselects the option with the given attribute value that either exists in the workingForm or in the current page.
     * @param fieldName - The name of the select field to select
     * @param valueAttribute - The value of option field to select
     * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
     */
    public void setSelectFieldByValue(String fieldName, String valueAttribute, boolean selected){
        helper.setSelectFieldByValue(fieldName, valueAttribute, selected);
    }

    /**
     * Selects or unselects the option defined by XPath.
     * @param xpath - The XPath expression that matches an HtmlOption (not a select) to be selected
     * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
     */
    public void setSelectFieldByXPath(String xpath, boolean selected){
        helper.setSelectFieldByXPath(xpath, selected);
    }

    /**
     * Sets the text area that either exists in the workingForm or in the current page
     * @param fieldName - The name of the input field to set the value of
     * @param value - The value to set the input field to
     */
    public void setTextArea(String fieldName, String value){
        helper.setTextArea(fieldName, value);
    }

    /**
     * Sets the text field that either exists in the workingForm or in the current page
     * @param fieldName - The name of the input field to set the value of
     * @param value - The value to set the input field to
     */
    public void setTextField(String fieldName, String value){
        helper.setTextField(fieldName, value);
    }

    /**
     * Sets an input field type's value
     * @param input - The HtmlInput to set the value on
     * @param value - The value to set the input element to.
     * @param type - The input type. If this does not match the given field, then this method will fail
     */
    public void setHtmlInputValue(HtmlInput input, String value, String type){
        helper.setHtmlInputValue(input, value, type);
    }

    /**
     * Sets an input field type's value
     * @param xpath - An XPath expression that matches the desired HtmlInput Element.
     * @param value - The value to set the input element to.
     * @param type - The input type. If this does not match the given field, then this method will fail
     */
    public void setHtmlInputValueByXPath(String xpath, String value, String type){
        helper.setHtmlInputValueByXPath(xpath, value, type);
    }

    /**
     * Sets the working form to form with the given id
     * This is a power method to set a form element by the following order.
     * <ol>
     *     <li>getHtmlFormById</li>
     *     <li>getHtmlFormByName</li>
     *     <li>getHtmlFormByIndex</li>
     *     <li>getHtmlElementByXPath</li>
     * </ol>
     * @param idNameIndexOrXpath - The id, name, index or XPath expression defining the location of the form
     */
    public void setWorkingForm(String idNameIndexOrXpath){
        workingForm = getHtmlForm(idNameIndexOrXpath);
    }

    /**
     * Sets the working form to form with the given id
     * @param id - The value of the form's HTML id attribute
     */
    public void setWorkingFormById(String id){
        workingForm = getHtmlFormById(id);
    }

    /**
     * Sets the working form to nth form on the current page.
     * This method is useful when the form doesn't have a name or id attribute
     * @param index - The number of form on the page. The first form would be 1
     */
    public void setWorkingFormByIndex(int index){
        workingForm = getHtmlFormByIndex(index);
    }

    /**
     * Sets the working form to nth form on the current page.
     * @param name - The value of the name attribute in the form
     */
    public void setWorkingFormByName(String name){
        workingForm = getHtmlFormByName(name);
    }

    /**
     * Sets the working form by the matching XPath expression
     * @param xpath - The XPath expression that matches the form to use
     */
    public void setWorkingFormByXPath(String xpath){
        workingForm = getHtmlFormByXPath(xpath);
    }

}




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

  tags/
    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
  util/
    HtmlUnitDelegate.java
    HtmlUnitHelper.java
    TrustEverythingSSLProtocolSocketFactory.java
  HtmlUnitFunctionTag.java
  HtmlUnitSessionTag.java