WebResponse.getElementsWithClassName(String)
Rick Huff <[email protected]> Tue, 23 Jun 2009 16:32:30 -0500
| Newsgroups | gmane.comp.web.httpunit.devel |
|---|---|
| Message-ID | <[email protected]> |
Here is a patch against rev1022 of HEAD to implement the getElementsWithClassName() method. This method returns those nodes where there are more than one class name on the element. -- Rick Huff Identity and Access Management Team ITS Applications The University of Texas at Austin ------------------------------------------------------------------------------ _______________________________________________ Httpunit-develop mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/httpunit-develop
classpatch.diff
(text/x-patch, 5.3 KB)
Index: test/com/meterware/httpunit/WebPageTest.java
===================================================================
--- test/com/meterware/httpunit/WebPageTest.java (revision 1022)
+++ test/com/meterware/httpunit/WebPageTest.java (working copy)
@@ -437,6 +437,21 @@
assertImplement( "elements with src '/images/arrow.gif'", simplePage.getElementsWithAttribute( "src", "/images/arrow.gif" ), WebImage.class );
}
+ public void testGetElementsWithClassName() throws Exception {
+ defineResource( "SimplePage.html",
+ "<html><head><title>A Sample Page</title></head>\n" +
+ "<body><form class='first colorsample' name='aForm'><input name=color></form>" +
+ "have <a id='link1' href='/other.html'>an <b>active</b> link</A>\n" +
+ "<img id='23' src='/images/arrow.gif' ALT='Next -->' WIDTH=1 HEIGHT=4>\n" +
+ "</body></html>\n" );
+ WebConversation wc = new WebConversation();
+ WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" );
+ WebResponse simplePage = wc.getResponse( request );
+ assertImplement( "elements with class attribute 'first colorsample'", simplePage.getElementsWithAttribute( "class", "first colorsample" ), WebForm.class );
+ assertImplement( "elements with class 'first'", simplePage.getElementsWithClassName( "first" ), WebForm.class );
+ assertImplement( "elements with class 'colorsample'", simplePage.getElementsWithClassName( "colorsample" ), WebForm.class );
+ }
+
/**
* Test the {@link WebResponse.ByteTagParser} to ensure that embedded JavaScript is skipped.
*/
Index: src/com/meterware/httpunit/WebResponse.java
===================================================================
--- src/com/meterware/httpunit/WebResponse.java (revision 1022)
+++ src/com/meterware/httpunit/WebResponse.java (working copy)
@@ -377,6 +377,14 @@
/**
+ * Returns the HTMLElements found in this segment with the specified class.
+ */
+ public HTMLElement[] getElementsWithClassName( String className ) throws SAXException {
+ return getReceivedPage().getElementsWithClassName( className );
+ }
+
+
+ /**
* Returns the HTMLElements found with the specified attribute value.
* @since 1.6
*/
Index: src/com/meterware/httpunit/ParsedHTML.java
===================================================================
--- src/com/meterware/httpunit/ParsedHTML.java (revision 1022)
+++ src/com/meterware/httpunit/ParsedHTML.java (working copy)
@@ -66,6 +66,9 @@
/** map of element names to lists of elements. **/
private HashMap _elementsByName = new HashMap();
+ /** map of element class to elements. **/
+ private HashMap _elementsByClass = new HashMap();
+
/** map of DOM elements to HTML elements **/
private ElementRegistry _registry = new ElementRegistry();
@@ -224,6 +227,16 @@
/**
+ * Returns the HTML elements with the specified class.
+ */
+ public HTMLElement[] getElementsWithClassName( String className ) {
+ loadElements();
+ ArrayList elements = (ArrayList) _elementsByClass.get( className );
+ return elements == null ? NO_ELEMENTS : (HTMLElement[]) elements.toArray( new HTMLElement[ elements.size() ] );
+ }
+
+
+ /**
* Returns the HTML elements with an attribute with the specified name and value.
* @param name - the name of the attribute to check
* @param value - the value of the attribute to check
@@ -234,8 +247,9 @@
for (Iterator i = _registry.iterator(); i.hasNext();) {
HTMLElement element = (HTMLElement) i.next();
String aValue=element.getAttribute( name );
+
if (value.equals(aValue )) {
- //System.err.println(element.getTagName()+"("+name+")="+aValue);
+ System.err.println(element.getTagName()+"("+name+")="+aValue);
elements.add( element );
}
}
@@ -857,6 +871,22 @@
_registry.registerElement( node, htmlElement );
if (htmlElement.getID() != null) _elementsByID.put( htmlElement.getID(), htmlElement );
if (htmlElement.getName() != null) addNamedElement( htmlElement.getName(), htmlElement );
+ if (htmlElement.getClassName() != null) {
+ StringTokenizer tokenizer = new StringTokenizer(htmlElement.getClassName());
+ String token;
+
+ while(tokenizer.hasMoreElements()) {
+ token = tokenizer.nextToken();
+
+ if ( _elementsByClass.containsKey( token )) {
+ ((ArrayList) _elementsByClass.get( token )).add( htmlElement );
+ } else {
+ ArrayList arrayList = new ArrayList();
+ arrayList.add(htmlElement);
+ _elementsByClass.put( token, arrayList );
+ }
+ }
+ }
}
Index: pom.xml
===================================================================
--- pom.xml (revision 1022)
+++ pom.xml (working copy)
@@ -87,7 +87,6 @@
<exclude>**/TestSuite$1.class</exclude>
<exclude>**/EventAwareTestCase.class</exclude>
<exclude>**/WebClientTest.class</exclude>
- <exclude>**/WebPageTest.class</exclude>
</excludes>
</configuration>
</plugin>