⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webform.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ibm.staf.service.http.html;/*****************************************************************************//* Software Testing Automation Framework (STAF)                              *//* (C) Copyright IBM Corp. 2004                                              *//*                                                                           *//* This software is licensed under the Common Public License (CPL) V1.0.     *//*****************************************************************************/import com.ibm.staf.service.http.WebSession;import com.ibm.staf.service.http.InvalidElementIDException;import java.util.HashMap;import java.util.Vector;import java.util.Arrays;// xercesimport org.w3c.dom.html.HTMLInputElement;import org.w3c.dom.html.HTMLCollection;import org.w3c.dom.html.HTMLFormElement;/*****************************************************************************//*                                                                           *//* Class: WebForm                                                            *//* Description: This class provides the handle to manipulate html forms.     *//*                                                                           *//*****************************************************************************/public class WebForm extends WebElement{    public static final String CONTROLS="CONTROLS";    public static final String VISITED="VISITED";        Parameter[] params;    String method;    String action;    HTMLFormElement the_form;    /*****************************************************************************//*                                                                           *//* Method: Constructor                                                       *//* Description: Constructor method                                           *//* Parameter: element - HTMLFormElement that will be wrapped by this class   *//*                      to manipulate the form                               *//*            index - index of this element in the list of elements of this  *//*                    type                                                   *//*                                                                           *//*****************************************************************************/        public WebForm (HTMLFormElement element, int index)    {        super (element.getId(), element.getName(), index);                method = element.getMethod();        //set default if not specified        if (method == null || method.equals(""))            method = "GET";                    action = element.getAction();        // build params        HTMLCollection controls = element.getElements();        // group by name        Vector elements = groupParameters(controls);                params = new Parameter [elements.size()];                for (int i = 0; i < params.length; i++)            params[i] = ParameterFactory.newParameter(elements.elementAt(i));                the_form = element;                if (!the_form.hasAttribute(VISITED))            the_form.setAttribute(VISITED,"");    }        public String getMethod()    {        return method;    }    public String getAction()    {        return action;    }    /*****************************************************************************//*                                                                           *//* Method: findName                                                          *//* Description: get the index in the list that has a vector of               *//*              HTMLInputElements with the name attribute of name.           *//*              -1 indicates the name is not in a list                       *//* Parameters: name - name that is being searched for                        *//*             list - vector of vectors and HTMLElements being searched      *//* Returns: index of the list that has a list of elements with name name or  *//*          -1 if the name is not in a list                                  *//*                                                                           *//*****************************************************************************/            int findName(String name, Vector list)    {        for (int i = 1; i < list.size(); i++)            if (Vector.class == list.elementAt(i).getClass())            {                if (name.equals(((HTMLInputElement)                                ( (Vector)list.elementAt(i) ).elementAt(0))                                .getName()))                {                    return i;                }            }        return -1;    }    /*****************************************************************************//*                                                                           *//* Method: groupParameters                                                   *//* Description: organize the collection into objects to be turned into       *//*              control parameters.  All input type submits are grouped, all *//*              input type radio with the same attribute name are grouped,   *//*              all other elements are added seperately to the list          *//* Parameters: col - a list of HTMLElements                                  *//* Returns: a list of objects to be turned into control parameters           *//*                                                                           *//*****************************************************************************/            Vector groupParameters(HTMLCollection col)    {        Vector list = new Vector();                list.add(new Vector());                for (int i = 0; i < col.getLength(); i++)        {            if (col.item(i).getClass() ==                 org.apache.html.dom.HTMLSelectElementImpl.class)                                list.addElement(col.item(i));            else if (col.item(i).getClass() ==                      org.apache.html.dom.HTMLInputElementImpl.class)            {                HTMLInputElement element = (HTMLInputElement) col.item(i);                if (element.getType().equalsIgnoreCase("submit") ||                     element.getType().equalsIgnoreCase("image"))                    ((Vector)list.elementAt(0)).addElement(element);                                    else if(element.getType().equalsIgnoreCase("radio"))                {                    int indx = findName(element.getName(),list);                    if (indx == -1)                    {                        indx = list.size();                        list.addElement(new Vector());                    }                    ((Vector)list.elementAt(indx)).addElement(element);                }                else                {                    list.addElement(col.item(i));                }            }            else if (col.item(i).getClass() ==                       org.apache.html.dom.HTMLTextAreaElementImpl.class)                                      list.addElement(col.item(i));        }                    // remove submit group if it is empty        if (((Vector)list.elementAt(0)).size() == 0)            list.remove(0);                return list;    }    /*****************************************************************************//*                                                                           *//* Method: getRequest                                                        *//* Description: get the hash describing the http request associated with this*//*              html element.                                                *//* Parameters: none                                                          *//* Returns: description of a http request                                    *//*                                                                           *//*****************************************************************************/            public HashMap getRequest()    {        HashMap request = new HashMap();                request.put(WebSession.REQUEST_METHOD, getMethod());        request.put(WebSession.REQUEST_URL, getAction());        request.put(WebSession.REQUEST_PARAMETERS, getActiveParameters());        request.put(WebSession.REQUEST_HEADERS, getHeaders());        request.put(WebSession.REQUEST_FILES, getActiveFiles());        request.put(WebSession.REQUEST_CONTENT, null);                return request;        }    /*****************************************************************************//*                                                                           *//* Method: getActiveParameters                                               *//* Description: get the list of non-file parameters that will be added to the*//*              request.                                                     *//* Parameters: none                                                          *//* Returns: list of non-file parameters that will be added to the request    *//*                                                                           *//*****************************************************************************/            public Vector getActiveParameters()    {        Vector list = new Vector();        int j;        for (int i = 0; i < params.length; i++)        {            if (! params[i].getType().equals("input type=file"))            {                Vector[] subList = params[i].paramString();                                for (j = 0; j < subList.length; j++)                    list.addElement(subList[j]);            }        }        if (list.size() == 0)            return null;                    return list;    }    /*****************************************************************************//*                                                                           *//* Method: getActiveFiles                                                    */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -