Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2009, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This 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 software 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 software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.jsfunit.jsfsession;
 
 import java.util.List;
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.UISelectItem;
 import  javax.faces.component.html.HtmlSelectOneRadio;
 import  javax.faces.context.FacesContext;
This class provides a wrapper for HtmlUnit that imitates browser interaction with a JSF application.

Author(s):
Stan Silvert
Since:
1.0
 
 public class JSFClientSession
 {
    private WebClient webClient;
    
    JSFClientSession(WebClient webClientJSFServerSession jsfServerSession)
    {
       this. = webClient;
       this. = jsfServerSession;
    }
   
   
Get the latest content page returned from the server. This page may have been changed by javascript or direct manipulation of the DOM.

Returns:
The Page.
 
    public Page getContentPage()
    {
       return .getCurrentWindow().getEnclosedPage();
    }
   
   
Get the content page as a text String.

Returns:
the text
 
    public String getPageAsText()
    {
       if (getContentPage() instanceof HtmlPagereturn ((HtmlPage)getContentPage()).asXml();
       if (getContentPage() instanceof TextPagereturn ((TextPage)getContentPage()).getContent();
       if (getContentPage() instanceof XmlPagereturn ((XmlPage)getContentPage()).asXml();
       if (getContentPage() instanceof JavaScriptPagereturn ((JavaScriptPage)getContentPage()).getContent();
       
       throw new IllegalStateException("This page can not be converted to text.  Page type is " + getContentPage().getClass().getName());
    }
   
   
Get a DOM Element on the current page that has the given JSF componentID.

Parameters:
componentID The JSF component id (or a suffix of the client ID)
Returns:
The Element, or null if not found.
Throws:
DuplicateClientIDException if more than one client ID matches the suffix
ClassCastException if the current page is not an HtmlPage.
   public Element getElement(String componentID)
   {
      DomNode domPage = (DomNode)getContentPage();
      String xpathQuery = buildXPathQuery(componentID);
      List elements = domPage.getByXPath(xpathQuery);
      if (elements.size() == 0) return null;
      if (elements.size() == 1) return (Element)elements.get(0);
      Element exactMatch = findExactMatch(elementscomponentID);
      if (exactMatch != nullreturn exactMatch;
      throw new DuplicateClientIDException(elementscomponentID);
   }
   
   // JSFUNIT-178
   private Element findExactMatch(List elementsString componentID)
   {
      for (Iterator i = elements.iterator(); i.hasNext();)
      {
         Element element = (Element)i.next();
         String id = element.getAttribute("id");
         if (id.equals(componentID)) return element;
      }
      
      return null;
   }
   
   private String buildXPathQuery(String componentID)
   {
      return "//*[" + endsWith("ID"componentID) + " or " + endsWith("id"componentID) + "]";
   }
   // XPath 1.0 doesn't have the ends-with function, so I have to make it myself
   private String endsWith(String attributeString string)
   {
      return "('" + string + "' = substring(@" + attribute + ",string-length(@" + attribute + ") - string-length('" + string + "') + 1))";
   }
   
   
Set the value attribute of a JSF component.

Parameters:
componentID The JSF component id (or a suffix of the client ID) of a component rendered as an HtmlInput component.
Throws:
ComponentIDNotFoundException if no client ID matches the suffix
DuplicateClientIDException if more than one client ID matches the suffix
ClassCastException if the current page is not an HtmlPage or the specified component is not an HtmlInput.
   public void setValue(String componentIDString value)
   {
      Element input = getElement(componentID);
      if (input == nullthrow new ComponentIDNotFoundException(componentID);
      
      if (input instanceof HtmlInput
      {
         ((HtmlInput)input).setValueAttribute(value);
         return;
      }
      
      if (input instanceof HtmlTextArea)
      {
         ((HtmlTextArea)input).setText(value);
         return;
      }
      
      if (input instanceof HtmlIsIndex)
      {
         ((HtmlIsIndex)input).setValue(value);
         return;
      }
      
      throw new IllegalArgumentException("This method can not be used on components of type " + input.getClass().getName());
   }
   
   
Simulates typing a character while this JSF component has focus.

Parameters:
componentID The JSF component id (or a suffix of the client ID) of a component rendered as an HtmlElement.
Throws:
ComponentIDNotFoundException if no client ID matches the suffix
DuplicateClientIDException if more than one client ID matches the suffix
ClassCastException if the current page is not an HtmlPage or the specified component is not an HtmlElement.
IOExceptioin if typing causes a failed request to the server.
   public void type(String componentIDchar cthrows IOException
   {
      HtmlElement element = (HtmlElement)getElement(componentID);
      if (element == nullthrow new ComponentIDNotFoundException(componentID);
      element.type(c);
   }
   
   
Click a JSF component.

Parameters:
componentID The JSF component id (or a suffix of the client ID) to be clicked.
Throws:
ComponentIDNotFoundException if no client ID matches the suffix
DuplicateClientIDException if more than one client ID matches the suffix
ClassCastException if the current page is not an HtmlPage or the specified component is not a ClickableElement.
IOException if clicking causes a failed request to the server.
   public void click(String componentIDthrows IOException
   {
      Element element = getElement(componentID);
      
      if ((element == null) && (parentIsHtmlSelect(componentID)))
      {
         clickSelect(componentID);
         return;
      }
      
      if ((element == null) && (parentIsSelectOneRadio(componentID)))
      {
         clickRadio(componentID);
         return;
      }
      
      if (element == nullthrow new ComponentIDNotFoundException(componentID);
      
      if (element instanceof ClickableElement)
      {
         ((ClickableElement)element).click();
         return;
      }
      
      throw new IllegalArgumentException("This method can not be used on components of type " + element.getClass().getName());
   }
   
   // return true if parent JSF component is HtmlSelectOneRadio
   private boolean parentIsSelectOneRadio(String componentID)
   {
      String parentClientID = parentElementClientID(componentID);
      UIComponent parentComponent = .findComponent(parentClientID);
      return (parentComponent instanceof HtmlSelectOneRadio);
   }
   
   // return true if parent html component is an HtmlSelect
   private boolean parentIsHtmlSelect(String componentID)
   {
      Element parentElement = getElement(parentElementClientID(componentID));
      return (parentElement instanceof HtmlSelect);
   }
   
   private String parentElementClientID(String componentID)
   {
      FacesContext facesContext = .getFacesContext();
      UIComponent component = .findComponent(componentID);
      return component.getParent().getClientId(facesContext);
   }
   
   private void clickRadio(String componentIDthrows IOException
   {
      String itemValue = getSelectItemValue(componentID);
      String parentID = parentElementClientID(componentID);
      HtmlRadioButtonInput radioInput = findRadioInput(parentIDitemValue);
      radioInput.click();
   }
   
   private String getSelectItemValue(String componentID)
   {
      UIComponent uiComponent = .findComponent(componentID);
      if (!(uiComponent instanceof UISelectItem)) 
      {
         throw new IllegalArgumentException(componentID + " is not a UISelectItem.");
      }
      
      return ((UISelectItem)uiComponent).getItemValue().toString();
   }
   
   private void clickSelect(String componentIDthrows IOException
   {
      String parentID = parentElementClientID(componentID);
      
      Element element = getElement(parentID);
      HtmlSelect htmlSelect = (HtmlSelect)element;
      
      String optionValue = getSelectItemValue(componentID);
      
      HtmlOption htmlOption = htmlSelect.getOptionByValue(optionValue);
      htmlOption.click();
   }
   
   private HtmlRadioButtonInput findRadioInput(String componentIDString optionToSelect)
   {
      String clientID = .getClientIDs().findClientID(componentID);
      HtmlPage htmlPage = (HtmlPage)getContentPage();
      List<HtmlElementelements = htmlPage.getHtmlElementsByName(clientID);
      for (Iterator<HtmlElementi = elements.iterator(); i.hasNext();)
      {
         HtmlElement htmlElement = i.next();
         if ((htmlElement instanceof HtmlRadioButtonInput) && 
             (htmlElement.getAttribute("value").equals(optionToSelect)))
         {
            return (HtmlRadioButtonInput)htmlElement;
         }
      }
      
      return null;
   }
   
New to GrepCode? Check out our FAQ X