[embjopr-commits] EMBJOPR SVN: r193 - trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit.

embjopr-commits at lists.jboss.org embjopr-commits at lists.jboss.org
Mon Mar 9 09:23:20 EDT 2009


Author: ozizka at redhat.com
Date: 2009-03-09 09:23:20 -0400 (Mon, 09 Mar 2009)
New Revision: 193

Added:
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/WebClientTools.java
Modified:
   trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/LoginTest.java
Log:
WebClientTools.java was missing

Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/LoginTest.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/LoginTest.java	2009-03-07 00:03:33 UTC (rev 192)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/LoginTest.java	2009-03-09 13:23:20 UTC (rev 193)
@@ -46,7 +46,6 @@
    public void testLoggedIn() throws IOException
    {
 
-		 // TODO: Move user name to a constant.
 		 assertTrue( client.getPageAsText().contains(LOGIN_USERNAME) );
 		 assertTrue( client.getPageAsText().contains("Logout") );
 

Added: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/WebClientTools.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/WebClientTools.java	                        (rev 0)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/WebClientTools.java	2009-03-09 13:23:20 UTC (rev 193)
@@ -0,0 +1,183 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.jopr.jsfunit;
+
+import com.gargoylesoftware.htmlunit.JavaScriptPage;
+import com.gargoylesoftware.htmlunit.Page;
+import com.gargoylesoftware.htmlunit.TextPage;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.html.*;
+import com.gargoylesoftware.htmlunit.xml.XmlPage;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+import org.jboss.jsfunit.jsfsession.ComponentIDNotFoundException;
+import org.jboss.jsfunit.jsfsession.DuplicateClientIDException;
+import org.w3c.dom.Element;
+
+/**
+ * Copied from JSFClientSession, only there's no JSFServersession reference.
+ * Contains some useful utils like getElement() etc.
+ *
+ * @author ozizka
+ * @since 1.0
+ */
+public class WebClientTools
+{
+   private WebClient webClient;
+   
+   public WebClientTools(WebClient webClient)
+   {
+      this.webClient = webClient;
+   }
+   
+   /**
+    * Get the latest content page returned from the server.  This page may
+    * have been changed by javascript or direct manipulation of the DOM.
+    *
+    * @return The Page.
+    */
+   public Page getContentPage()
+   {
+      return webClient.getCurrentWindow().getEnclosedPage();
+   }
+   
+   /**
+    * Get the content page as a text String.
+    *
+    * @return the text
+    */
+   public String getPageAsText()
+   {
+      if (getContentPage() instanceof HtmlPage) return ((HtmlPage)getContentPage()).asXml();
+      if (getContentPage() instanceof TextPage) return ((TextPage)getContentPage()).getContent();
+      if (getContentPage() instanceof XmlPage) return ((XmlPage)getContentPage()).asXml();
+      if (getContentPage() instanceof JavaScriptPage) return ((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.
+    *
+    * @param componentID The JSF component id (or a suffix of the client ID)
+    *
+    * @return The Element, or <code>null</code> if not found.
+    *
+    * @throws DuplicateClientIDException if more than one client ID matches the suffix
+    * @throws 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(elements, componentID);
+      if (exactMatch != null) return exactMatch;
+      throw new DuplicateClientIDException(elements, componentID);
+   }
+   
+   // JSFUNIT-178
+   private Element findExactMatch(List elements, String 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 attribute, String string)
+   {
+      return "('" + string + "' = substring(@" + attribute + ",string-length(@" + attribute + ") - string-length('" + string + "') + 1))";
+   }
+   
+   /**
+    * Set the value attribute of a JSF component.
+    * 
+    * @param 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
+    * @throws DuplicateClientIDException if more than one client ID matches the suffix
+    * @throws ClassCastException if the current page is not an HtmlPage or the 
+    *                            specified component is not an HtmlInput.
+    */
+   public void setValue(String componentID, String value)
+   {
+      Element input = getElement(componentID);
+      if (input == null) throw 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.
+    *
+    * @param 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
+    * @throws DuplicateClientIDException if more than one client ID matches the suffix
+    * @throws ClassCastException if the current page is not an HtmlPage or the 
+    *                            specified component is not an HtmlElement.
+    */
+   public void type(String componentID, char c) throws IOException
+   {
+      HtmlElement element = (HtmlElement)getElement(componentID);
+      if (element == null) throw new ComponentIDNotFoundException(componentID);
+      element.type(c);
+   }
+   
+  
+   
+}




More information about the embjopr-commits mailing list