Author: ozizka(a)redhat.com
Date: 2009-01-19 10:13:51 -0500 (Mon, 19 Jan 2009)
New Revision: 108
Modified:
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java
Log:
Added: Various helper methods; some of them duplicate the functionality in Farah's
code - to be reduced later.
Modified: trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java
===================================================================
--- trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java 2009-01-14
12:33:04 UTC (rev 107)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java 2009-01-19
15:13:51 UTC (rev 108)
@@ -24,19 +24,12 @@
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
-import com.gargoylesoftware.htmlunit.html.ClickableElement;
-import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
-import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
-import com.gargoylesoftware.htmlunit.html.HtmlForm;
-import com.gargoylesoftware.htmlunit.html.HtmlImage;
-import com.gargoylesoftware.htmlunit.html.HtmlTable;
-import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
-import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
-import com.gargoylesoftware.htmlunit.html.HtmlInput;
+import com.gargoylesoftware.htmlunit.html.*;
import java.io.IOException;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.logging.Logger;
+import javax.naming.NamingException;
+import javax.naming.InitialContext;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.cactus.ServletTestCase;
@@ -45,7 +38,13 @@
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.jsfunit.jsfsession.JSFSession;
import javax.faces.application.FacesMessage;
+import javax.management.*;
+import org.jboss.mx.util.MBeanServerLocator;
+//import org.jboss.jmx.adaptor.rmi.RMIAdaptor; // Needs dependency: jmx-adaptor-plugin
+
+
+
/**
* This is the base test class for Embedded Jopr test cases.
* It supplies access to a JSFClientSession object and a JSFServerSession
@@ -53,15 +52,19 @@
* commonly used UI components.
*
* @author Farah Juma
+ * @author Ondrej Zizka
* @author Stan Silvert
*/
public class EmbjoprTestCase extends ServletTestCase {
+ protected final Logger log = Logger.getLogger(this.getClass().getName());
+
protected boolean isJBoss4;
protected JSFClientSession client;
protected JSFServerSession server;
+
/**
* Start a JSFUnit session by logging in to the main page. Note that
* because setUp() is called before each test, a new HttpSession will be
@@ -216,6 +219,8 @@
HtmlForm form =
(HtmlForm)client.getElement("resourceConfigurationForm");
HtmlInput input =
(HtmlInput)form.getFirstByXPath(".//input[@ondblclick='//"
+ propertyName + "']");
+
+ assertNotNull("Form input for property "+propertyName+" not
found.", input);
boolean isRadioButton = input.getTypeAttribute().equals("radio");
String id = input.getId();
@@ -304,5 +309,192 @@
public void testClientSessionCreated() {
assertTrue(client != null);
}
+
+
+
+
+
+
+
+
+
+
+ /**
+ * Finds a row in the "details table" using the text in the Name column
+ * and returns the text from the Value column.
+ * @param name
+ * @return
+ */
+ public String getDetailsRowValue(String name) throws AssertException {
+ HtmlDivision contentDiv = (HtmlDivision) client.getElement("content");
+ List<?> valueTDs = contentDiv.getByXPath(
+ ".//div[@class='tabmenubox']//td[contains(text(),'"+name+"']/following-sibling::td[1]");
+ //assertTrue( name + " row not found.", valueTDs.size() == 1 );
+ if( valueTDs.size() == 1 )
+ throw new AssertException( name + " row not found." );
+ String value = ((HtmlTableCell) valueTDs).getTextContent();
+ return value;
+ }
+
+
+ /**
+ * Returns the content element - <div id="content"> - which
contains whole page.
+ * Good as a start point for XPath queries - other elements usually have generated
IDs.
+ * Assertion fails if the element is not found.
+ * @return
+ */
+ public HtmlElement getContentElement() throws AssertException {
+ HtmlElement contentElement = (HtmlElement)client.getElement("content");
+ //assertNotNull("Content <div> not found.", contentElement);
+ if( null == contentElement )
+ throw new AssertException( "Content <div> not found." );
+ return contentElement;
+ }
+
+
+ /**
+ * Convenience method, calls getElementsByXPath() with content element.
+ * @param sXPath
+ * @return
+ */
+ public List<? extends HtmlElement> getElementsByXPath(String sXPath ) throws
AssertException {
+ //return (List<? extends HtmlElement>) getContentElement().getByXPath(sXPath);
+ return getElementsByXPath( getContentElement(), sXPath );
+ }
+
+
+ /**
+ * Retuns a list of elements chosen by XPath, with the given element as context node.
+ * @param xPathContextElement
+ * @param sXPath
+ * @return
+ */
+ public List<? extends HtmlElement> getElementsByXPath(
+ HtmlElement xPathContextElement, String sXPath )
+ throws AssertException
+ {
+ if( null == xPathContextElement ){
+ throw new AssertException("Given XPath context element is null.");
+ }
+
+ return (List<? extends HtmlElement>) xPathContextElement.getByXPath(sXPath);
+ }
+
+
+ /**
+ * Returns the first element in the list returned by getElementsByXPath(String sXPath
).
+ * @param sXPath
+ * @return
+ */
+ public HtmlElement getFirstElementByXPath( HtmlElement xPathContext, String sXPath )
throws AssertException {
+
+ List<? extends HtmlElement> elementsByXPath = getElementsByXPath(xPathContext,
sXPath);
+ if( elementsByXPath.size() == 0 ){
+ throw new RuntimeException("XPath expression found no elements: "+sXPath);
+ // Exception is better - will get the stack trace.
+ //fail("XPath expression found no elements: "+sXPath);
+ }
+ return elementsByXPath.get(0);
+ }
+
+
+ /**
+ * Convenience method - calls getFirstElementByXPath() with content div as context.
+ * @param sXPath
+ * @return
+ */
+ public HtmlElement getFirstElementByXPath( String sXPath ) throws AssertException{
+ return getFirstElementByXPath( getContentElement(), sXPath );
+ }
+
+
+ /**
+ * Retrieves an input element from a named row of a table in a form.
+ * Supposes that the form is in the "tabmenubox" or "notabmenubox"
div.
+ * @param sRowName
+ * @return
+ * @throws org.jboss.jopr.jsfunit.AssertException
+ */
+ public HtmlInput getFormInputByRowName(String sRowName) throws AssertException {
+ try {
+ HtmlInput input = (HtmlInput) getFirstElementByXPath(
+ getTabMenuBoxElement(),
+ ".//td[contains(text(),'"+sRowName+"')]/following-sibling::td[@class='property-value-cell']//input"
+ );
+ if( null == input ){
+ throw new AssertException("Input for value "+sRowName+" not
found.");
+ }
+ return input;
+ }
+ catch(AssertException ex){
+ //throw new AssertException("Row with name \""+sRowName+"\"
not found.", ex);
+ throw ex;
+ }
+ }
+
+
+ /**
+ * Retrieves a named row of a table in the "right side of the page".
+ * Supposes that the table is in the "tabmenubox" or "notabmenubox"
div.
+ * Uses getTabMenuBoxElement() to get that div.
+ * @param sRowName
+ * @return
+ * @throws org.jboss.jopr.jsfunit.AssertException
+ */
+ public HtmlTableRow getRowByName(String sRowName) throws AssertException {
+ HtmlTableRow row = (HtmlTableRow) getFirstElementByXPath(
+ getTabMenuBoxElement(),
+ ".//td[contains(text(),'"+sRowName+"')]/ancestor::tr"
+ );
+ if( null == row ){
+ throw new AssertException("Row for value "+sRowName+" not
found.");
+ }
+ return row;
+ }
+
+
+ /**
+ * Returns the element which contains the "right side of the page".
+ * <div class="tabmenubox"> element exists on each page that shows
resource's details.
+ * <div class="notabmenubox"> element exists on each page with form for
resource's properties editation.
+ * @return
+ * @throws org.jboss.jopr.jsfunit.AssertException
+ */
+ public HtmlElement getTabMenuBoxElement() throws AssertException {
+ return getFirstElementByXPath(".//div[@class='tabmenubox' or
@class='notabmenubox']");
+ }
+
+
+
+
+ /**
+ * Returns an integer part of the beginning of the given string.
+ * @param s
+ * @return Integer with the value of numerical part of the string,
+ * or null if there are no digits at the beginning.
+ */
+ public Integer integerFromString(String s) {
+ int pos;
+ for( pos = 0; pos < s.length(); pos++ ){
+ char ch = s.charAt(0);
+ if( '0' > ch || ch > '9' )
+ break;
+ }
+
+ if( pos == 0 )
+ return null;
+
+ try{
+ int val = Integer.parseInt( s.substring( 0, pos ) );
+ return val;
+ }catch( NumberFormatException ex ){
+ return null;
+ }
+
+ }
+
+
}
+
+
Show replies by date