Author: ozizka(a)redhat.com
Date: 2009-01-28 16:41:11 -0500 (Wed, 28 Jan 2009)
New Revision: 125
Modified:
trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java
Log:
- setFormInput() supports multi-radios (not only Yes / No )
- XPath helper methods changed - find... returns null, get... thows exception when no
element found
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-28
15:03:55 UTC (rev 124)
+++ trunk/jsfunit/src/test/java/org/jboss/jopr/jsfunit/EmbjoprTestCase.java 2009-01-28
21:41:11 UTC (rev 125)
@@ -23,15 +23,12 @@
package org.jboss.jopr.jsfunit;
import com.gargoylesoftware.htmlunit.BrowserVersion;
+import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import java.io.IOException;
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.jboss.logging.*;
import org.apache.cactus.ServletTestCase;
import org.jboss.jsfunit.framework.WebClientSpec;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
@@ -42,7 +39,7 @@
import org.jboss.mx.util.MBeanServerLocator;
//import org.jboss.jmx.adaptor.rmi.RMIAdaptor; // Needs dependency: jmx-adaptor-plugin
import java.util.regex.Pattern;
-import java.util.regex.Matcher;
+import javax.xml.xpath.XPathException;
@@ -256,19 +253,34 @@
* Set the given input box or "Yes/No" radio button to the given value.
*/
public void setFormInput(HtmlInput input, String propertyValue) {
+
boolean isRadioButton = input.getTypeAttribute().equals("radio");
- String id = input.getId();
-
if(isRadioButton) {
// Check the appropriate button
if(propertyValue.equals("false")) {
- // Get the "No" radio button
- input = (HtmlInput)client.getElement(id.substring(0,
- id.lastIndexOf(":"))
- + ":1");
+ // Get the "No" radio button.
+ String id = input.getId();
+ input = (HtmlInput)client.getElement(
+ id.substring(0, id.lastIndexOf(":")) + ":1");
}
+ else{
+
+ // Multiple choices - find the appropriate radio button.
+ String inputName = input.getNameAttribute();
+ DomNode node = input.getFirstByXPath(
+ "./ancestor::form//input[@name='"+inputName+"' and
@value='"+propertyValue+"']");
+ //DomNode node = input.getOneHtmlElementByAttribute("input",
"value", propertyValue);
+
+ if( null == node ){
+ throw new IllegalArgumentException(
+ "HTML Radio input of name '"+inputName+"' and value
'"+propertyValue+"' not found.");
+ }
+
+ input = (HtmlInput)node;
+
+ }
input.setChecked(Boolean.TRUE);
} else {
@@ -284,13 +296,13 @@
String expectedServerMsg,
boolean isErrorMsg)
{
- assertTrue("Expected message not found on page.",
- client.getPageAsText().contains(expectedClientMsg));
-
- assertTrue("Expected message not found in faces messages.",
+
+ // In Faces
+
+ assertTrue("Expected message not found in faces messages (no messages in
Faces).",
server.getFacesMessages().hasNext());
- FacesMessage message = server.getFacesMessages().next();
+ FacesMessage message = server.getFacesMessages().next();
if(isErrorMsg) {
assertTrue(FacesMessage.SEVERITY_ERROR.equals(message.getSeverity()));
} else {
@@ -299,9 +311,15 @@
assertTrue("Expected message: "+expectedServerMsg+" Actual:
"+message.getDetail(),
message.getDetail().contains(expectedServerMsg));
- }
+ // On page
+
+ assertTrue("Expected message not found on page: "+expectedClientMsg,
+ client.getPageAsText().contains(expectedClientMsg));
+ }
+
+
/**
* Check that the given messages occur on the client side and server side.
* Given strings are treated as regular expressions to match.
@@ -310,9 +328,7 @@
String expectedServerMsgRE,
boolean isErrorMsg)
{
- String pageText = client.getPageAsText();
- assertTrue( Pattern.matches(expectedClientMsgRE, pageText) );
-
+ // In Faces
assertTrue(server.getFacesMessages().hasNext());
FacesMessage message = server.getFacesMessages().next();
@@ -323,6 +339,12 @@
}
assertTrue( Pattern.matches( expectedServerMsgRE, message.getDetail() ) );
+
+ // On page
+
+ String pageText = client.getPageAsText();
+ assertTrue( Pattern.matches(expectedClientMsgRE, pageText) );
+
}
@@ -405,31 +427,49 @@
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
+ * @returns The first of elements found, or null when XPath expr. found none.
*/
- public HtmlElement getFirstElementByXPath( HtmlElement xPathContext, String sXPath )
throws AssertException {
+ public HtmlElement findFirstElementByXPath( 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 null;
}
return elementsByXPath.get(0);
}
/**
+ * The same as findFirstElementByXPath(), only throws an exception when no element was
found.
+ * Returns the first element in the list returned by getElementsByXPath(String sXPath
).
+ * @param sXPath
+ * @returns The first of elements found.
+ * @throws XPathException when the expression found no elements.
+ */
+ public HtmlElement getFirstElementByXPath( HtmlElement xPathContext, String sXPath )
+ throws AssertException
+ {
+ HtmlElement e = findFirstElementByXPath(xPathContext, sXPath);
+ if( null == e ){
+ throw new AssertException("XPath expression found no elements: "+sXPath);
+ }
+ return e;
+ }
+
+ /**
* Convenience method - calls getFirstElementByXPath() with content div as context.
* @param sXPath
* @return
@@ -462,6 +502,7 @@
}
}
+
/**
* 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.
@@ -471,16 +512,18 @@
* @throws org.jboss.jopr.jsfunit.AssertException
*/
public HtmlTableRow getRowByName(String sRowName) throws AssertException {
- HtmlTableRow row = (HtmlTableRow) getFirstElementByXPath(
- getTabMenuBoxElement(),
-
".//td[contains(text(),'"+sRowName+"')]/ancestor::tr"
- );
+
+ HtmlTableRow row = (HtmlTableRow) getFirstElementByXPath(
+ getTabMenuBoxElement(),
+ ".//td[contains(string(),'"+sRowName+"')]/ancestor::tr"
);
+
if( null == row ){
- throw new AssertException("Row for value "+sRowName+" not
found.");
+ throw new AssertException("Row with label "+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.
@@ -492,6 +535,7 @@
return getFirstElementByXPath(".//div[@class='tabmenubox' or
@class='notabmenubox']");
}
+
/**
* Returns an integer part of the beginning of the given string.
* @param s
@@ -517,6 +561,7 @@
}
}
+
}