• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.37
/src/main/java/com/meterware/httpunit/WebLink.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit;
9

10
import com.meterware.httpunit.dom.HTMLElementImpl;
11
import com.meterware.httpunit.scripting.NamedDelegate;
12
import com.meterware.httpunit.scripting.ScriptableDelegate;
13

14
import java.io.IOException;
15
import java.net.MalformedURLException;
16
import java.net.URL;
17

18
import org.w3c.dom.Node;
19
import org.xml.sax.SAXException;
20

21
/**
22
 * This class represents a link in an HTML page. Users of this class may examine the structure of the link (as a DOM),
23
 * or create a {@link WebRequest} to simulate clicking on the link.
24
 **/
25
public class WebLink extends FixedURLWebRequestSource {
26

27
    /** Predicate to match part or all of a link's URL string. **/
28
    public static final HTMLElementPredicate MATCH_URL_STRING;
29

30
    /** Predicate to match a link's text exactly. **/
31
    public static final HTMLElementPredicate MATCH_TEXT;
32

33
    /** Predicate to match part or all of a link's contained text. **/
34
    public static final HTMLElementPredicate MATCH_CONTAINED_TEXT;
35

36
    /** Predicate to match a link's ID. **/
37
    public static final HTMLElementPredicate MATCH_ID;
38

39
    /** Predicate to match a link's name. **/
40
    public static final HTMLElementPredicate MATCH_NAME;
41

42
    /**
43
     * Returns the URL referenced by this link. This may be a relative URL. It will not include any fragment identifier.
44
     *
45
     * @return the URL string
46
     */
47
    public String getURLString() {
48
        return getRelativeURL();
1✔
49
    }
50

51
    /**
52
     * Returns the text value of this link.
53
     **/
54
    @Override
55
    public String getText() {
56
        if (getElement().getNodeName().equalsIgnoreCase("area")) {
1✔
57
            return getAttribute("alt");
1✔
58
        }
59
        return super.getText();
1✔
60
    }
61

62
    /**
63
     * Returns the text value of this link.
64
     *
65
     * @return the string
66
     *
67
     * @deprecated as of 1.6, use #getText instead
68
     */
69
    @Deprecated
70
    public String asText() {
71
        return getText();
×
72
    }
73

74
    /**
75
     * Submits a request as though the user had clicked on this link. Will also fire the 'onClick', 'onMouseDown' and
76
     * 'onMouseUp' event if defined. Returns the updated contents of the frame containing the link. Note that if the
77
     * click updates a different frame, that frame will not be returned by this method.
78
     *
79
     * @return the web response
80
     *
81
     * @throws IOException
82
     *             Signals that an I/O exception has occurred.
83
     * @throws SAXException
84
     *             the SAX exception
85
     */
86
    public WebResponse click() throws IOException, SAXException {
87
        if (handleEvent("onclick")) {
1✔
88
            ((HTMLElementImpl) getNode()).doClickAction();
1✔
89
        }
90
        return getCurrentFrameContents();
1✔
91
    }
92

93
    /**
94
     * Simulates moving the mouse over the link. Will fire the 'onMouseOver' event if defined.
95
     **/
96
    public void mouseOver() {
97
        handleEvent("onmouseover");
1✔
98
    }
1✔
99

100
    /**
101
     * The Class Scriptable.
102
     */
103
    public class Scriptable extends HTMLElementScriptable implements NamedDelegate {
104

105
        /**
106
         * Instantiates a new scriptable.
107
         */
108
        public Scriptable() {
1✔
109
            super(WebLink.this);
1✔
110
        }
1✔
111

112
        @Override
113
        public String getName() {
114
            return WebLink.this.getID().length() != 0 ? WebLink.this.getID() : WebLink.this.getName();
1✔
115
        }
116

117
        @Override
118
        public Object get(String propertyName) {
119
            if (propertyName.equalsIgnoreCase("href")) {
1✔
120
                return getReference().toExternalForm();
1✔
121
            }
122
            return super.get(propertyName);
1✔
123
        }
124

125
        @Override
126
        public void set(String propertyName, Object value) {
127
            if (propertyName.equals("href")) {
1!
128
                setDestination((String) value);
1✔
129
            } else {
130
                super.set(propertyName, value);
×
131
            }
132
        }
1✔
133

134
        /**
135
         * Gets the reference.
136
         *
137
         * @return the reference
138
         */
139
        private URL getReference() {
140
            try {
141
                return getRequest().getURL();
1✔
142
            } catch (MalformedURLException e) {
×
143
                return WebLink.this.getBaseURL();
×
144
            }
145
        }
146
    }
147

148
    // ----------------------------------------- WebRequestSource methods
149
    // ---------------------------------------------------
150

151
    @Override
152
    public ScriptableDelegate newScriptable() {
153
        return new Scriptable();
1✔
154
    }
155

156
    // --------------------------------------------------- package members
157
    // --------------------------------------------------
158

159
    /**
160
     * Contructs a web link given the URL of its source page and the DOM extracted from that page.
161
     *
162
     * @param response
163
     *            the response
164
     * @param baseURL
165
     *            the base URL
166
     * @param node
167
     *            the node
168
     * @param sourceFrame
169
     *            the source frame
170
     * @param defaultTarget
171
     *            the default target
172
     * @param characterSet
173
     *            the character set
174
     */
175
    WebLink(WebResponse response, URL baseURL, Node node, FrameSelector sourceFrame, String defaultTarget,
176
            String characterSet) {
177
        super(response, node, baseURL, "href", sourceFrame, defaultTarget, characterSet);
1✔
178
    }
1✔
179

180
    static {
181
        MATCH_URL_STRING = (htmlElement, criteria) -> HttpUnitUtils.contains(((WebLink) htmlElement).getURLString(),
1✔
182
                (String) criteria);
183

184
        MATCH_TEXT = (htmlElement, criteria) -> HttpUnitUtils.matches(((WebLink) htmlElement).getText(),
1✔
185
                (String) criteria);
186

187
        MATCH_CONTAINED_TEXT = (htmlElement, criteria) -> HttpUnitUtils.contains(((WebLink) htmlElement).getText(),
1✔
188
                (String) criteria);
189

190
        MATCH_ID = (htmlElement, criteria) -> HttpUnitUtils.matches(((WebLink) htmlElement).getID(), (String) criteria);
1✔
191

192
        MATCH_NAME = (htmlElement, criteria) -> HttpUnitUtils.matches(((WebLink) htmlElement).getName(),
1✔
193
                (String) criteria);
194

195
    }
1✔
196

197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc