Skip to content
This repository was archived by the owner on Apr 7, 2022. It is now read-only.

Commit 2df95c6

Browse files
author
Jim Weaver
committed
Updated installation doc, added clickLinkWithImage to HttpUnitDialog
1 parent 40c9bf3 commit 2df95c6

3 files changed

Lines changed: 68 additions & 8 deletions

File tree

jWebUnit/doc/installation.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@
3030
<!-- Begin Main -->
3131
First download jWebUnit from the <a href="http://sourceforge.net/projects/jwebunit/files">files area</a>
3232
and expand to a local directory.<p>
33-
Next, copy the jars needed below to the project you wish to test, or include them in that
34-
project's classpath from where you expanded them too. There is a jWebUnit jar in the root directory. You can use this
35-
jar alone to run jWebUnit, but only if you already have the jars for jWebUnit's dependencies in your target project.
36-
jWebUnit includes jars for the projects it is dependent on in its lib directory, so if your project
37-
is not already using them you can get them there. The dependencies are as follows:
33+
There are some jars for jWebUnit dependencies in the lib directory of your installation that may need to be added
34+
to your project's classpath before you can write project tests with jWebUnit. The dependencies are as follows:
3835
<p>
3936
<h2>Dependencies</h2>
4037
<table class="default" cellspacing="2" border="1">

jWebUnit/src/net/sourceforge/jwebunit/HttpUnitDialog.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ public boolean isLinkPresent(String anId) {
459459
}
460460

461461
/**
462-
* Navigate by submitting a request based on the specified link. A
463-
* RuntimeException is thrown if no such link can be found.
462+
* Navigate by submitting a request based on a link containing the
463+
* specified text. A RuntimeException is thrown if no such link can be found.
464464
*
465465
* @param linkText text which link to be navigated should contain.
466466
*/
@@ -477,7 +477,7 @@ public void clickLinkWithText(String linkText) {
477477
}
478478

479479
/**
480-
* Navigate by submitting a request based on the specified link. A
480+
* Navigate by submitting a request based on a link with a given ID. A
481481
* RuntimeException is thrown if no such link can be found.
482482
*
483483
* @param anID id of link to be navigated.
@@ -495,6 +495,27 @@ public void clickLink(String anID) {
495495

496496
}
497497

498+
/**
499+
* Navigate by submitting a request based on a link with a given image file name.
500+
* A RuntimeException is thrown if no such link can be found.
501+
*
502+
*
503+
* @param imageFileName A suffix of the image's filename; for example, to match
504+
* <tt>"images/my_icon.png"<tt>, you could just pass in
505+
* <tt>"my_icon.png"<tt>.
506+
*/
507+
public void clickLinkWithImage(String imageFileName) {
508+
WebLink link = null;
509+
try {
510+
link = resp.getFirstMatchingLink(new LinkImagePredicate(), imageFileName);
511+
} catch (SAXException e) {
512+
throw new RuntimeException(ExceptionUtility.stackTraceToString(e));
513+
}
514+
if (link == null)
515+
throw new RuntimeException("No Link found with imageFileName \"" + imageFileName + "\"");
516+
submitRequest(link.getRequest());
517+
}
518+
498519
/**
499520
* Return true if a radio group contains the indicated option.
500521
*
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/********************************************************************************
2+
* Copyright (c) 2001, ThoughtWorks, Inc.
3+
* Distributed open-source, see full license under licenses/jwebunit_license.txt
4+
**********************************/
5+
package net.sourceforge.jwebunit;
6+
7+
import org.w3c.dom.Element;
8+
import org.w3c.dom.NodeList;
9+
import com.meterware.httpunit.WebLink;
10+
import com.meterware.httpunit.HTMLElementPredicate;
11+
12+
/**
13+
* Matches to a Link element containing an image with a specified filename string as a suffix.
14+
*
15+
* @author Justin Sampson
16+
*/
17+
public class LinkImagePredicate implements HTMLElementPredicate
18+
{
19+
20+
public
21+
boolean matchesCriteria(Object webLink, Object criteria)
22+
{
23+
Element a = (Element) ((WebLink) webLink).getDOMSubtree();
24+
Element img = getChildImageElement(a);
25+
26+
if (img == null)
27+
{
28+
return false;
29+
}
30+
31+
String src = img.getAttribute("src");
32+
return src.endsWith((String) criteria);
33+
}
34+
35+
private
36+
Element getChildImageElement(Element htmlElement)
37+
{
38+
NodeList nodes = htmlElement.getElementsByTagName("img");
39+
return nodes.getLength() == 0 ? null : (Element) nodes.item(0);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)