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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

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
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit;
21

22
import com.meterware.httpunit.dom.HTMLElementImpl;
23
import com.meterware.httpunit.scripting.NamedDelegate;
24
import com.meterware.httpunit.scripting.ScriptableDelegate;
25

26
import java.io.IOException;
27
import java.net.MalformedURLException;
28
import java.net.URL;
29

30
import org.w3c.dom.Node;
31
import org.xml.sax.SAXException;
32

33
/**
34
 * This class represents a link in an HTML page. Users of this class may examine the structure of the link (as a DOM),
35
 * or create a {@link WebRequest} to simulate clicking on the link.
36
 **/
37
public class WebLink extends FixedURLWebRequestSource {
38

39
    /** Predicate to match part or all of a link's URL string. **/
40
    public static final HTMLElementPredicate MATCH_URL_STRING;
41

42
    /** Predicate to match a link's text exactly. **/
43
    public static final HTMLElementPredicate MATCH_TEXT;
44

45
    /** Predicate to match part or all of a link's contained text. **/
46
    public static final HTMLElementPredicate MATCH_CONTAINED_TEXT;
47

48
    /** Predicate to match a link's ID. **/
49
    public static final HTMLElementPredicate MATCH_ID;
50

51
    /** Predicate to match a link's name. **/
52
    public static final HTMLElementPredicate MATCH_NAME;
53

54
    /**
55
     * Returns the URL referenced by this link. This may be a relative URL. It will not include any fragment identifier.
56
     *
57
     * @return the URL string
58
     */
59
    public String getURLString() {
60
        return getRelativeURL();
1✔
61
    }
62

63
    /**
64
     * Returns the text value of this link.
65
     **/
66
    @Override
67
    public String getText() {
68
        if (getElement().getNodeName().equalsIgnoreCase("area")) {
1✔
69
            return getAttribute("alt");
1✔
70
        }
71
        return super.getText();
1✔
72
    }
73

74
    /**
75
     * Returns the text value of this link.
76
     *
77
     * @return the string
78
     *
79
     * @deprecated as of 1.6, use #getText instead
80
     */
81
    @Deprecated
82
    public String asText() {
83
        return getText();
×
84
    }
85

86
    /**
87
     * Submits a request as though the user had clicked on this link. Will also fire the 'onClick', 'onMouseDown' and
88
     * 'onMouseUp' event if defined. Returns the updated contents of the frame containing the link. Note that if the
89
     * click updates a different frame, that frame will not be returned by this method.
90
     *
91
     * @return the web response
92
     *
93
     * @throws IOException
94
     *             Signals that an I/O exception has occurred.
95
     * @throws SAXException
96
     *             the SAX exception
97
     */
98
    public WebResponse click() throws IOException, SAXException {
99
        if (handleEvent("onclick")) {
1✔
100
            ((HTMLElementImpl) getNode()).doClickAction();
1✔
101
        }
102
        return getCurrentFrameContents();
1✔
103
    }
104

105
    /**
106
     * Simulates moving the mouse over the link. Will fire the 'onMouseOver' event if defined.
107
     **/
108
    public void mouseOver() {
109
        handleEvent("onmouseover");
1✔
110
    }
1✔
111

112
    /**
113
     * The Class Scriptable.
114
     */
115
    public class Scriptable extends HTMLElementScriptable implements NamedDelegate {
116

117
        /**
118
         * Instantiates a new scriptable.
119
         */
120
        public Scriptable() {
1✔
121
            super(WebLink.this);
1✔
122
        }
1✔
123

124
        @Override
125
        public String getName() {
126
            return WebLink.this.getID().length() != 0 ? WebLink.this.getID() : WebLink.this.getName();
1✔
127
        }
128

129
        @Override
130
        public Object get(String propertyName) {
131
            if (propertyName.equalsIgnoreCase("href")) {
1✔
132
                return getReference().toExternalForm();
1✔
133
            }
134
            return super.get(propertyName);
1✔
135
        }
136

137
        @Override
138
        public void set(String propertyName, Object value) {
139
            if (propertyName.equals("href")) {
1!
140
                setDestination((String) value);
1✔
141
            } else {
142
                super.set(propertyName, value);
×
143
            }
144
        }
1✔
145

146
        /**
147
         * Gets the reference.
148
         *
149
         * @return the reference
150
         */
151
        private URL getReference() {
152
            try {
153
                return getRequest().getURL();
1✔
154
            } catch (MalformedURLException e) {
×
155
                return WebLink.this.getBaseURL();
×
156
            }
157
        }
158
    }
159

160
    // ----------------------------------------- WebRequestSource methods
161
    // ---------------------------------------------------
162

163
    @Override
164
    public ScriptableDelegate newScriptable() {
165
        return new Scriptable();
1✔
166
    }
167

168
    // --------------------------------------------------- package members
169
    // --------------------------------------------------
170

171
    /**
172
     * Contructs a web link given the URL of its source page and the DOM extracted from that page.
173
     *
174
     * @param response
175
     *            the response
176
     * @param baseURL
177
     *            the base URL
178
     * @param node
179
     *            the node
180
     * @param sourceFrame
181
     *            the source frame
182
     * @param defaultTarget
183
     *            the default target
184
     * @param characterSet
185
     *            the character set
186
     */
187
    WebLink(WebResponse response, URL baseURL, Node node, FrameSelector sourceFrame, String defaultTarget,
188
            String characterSet) {
189
        super(response, node, baseURL, "href", sourceFrame, defaultTarget, characterSet);
1✔
190
    }
1✔
191

192
    static {
193
        MATCH_URL_STRING = (htmlElement, criteria) -> HttpUnitUtils.contains(((WebLink) htmlElement).getURLString(),
1✔
194
                (String) criteria);
195

196
        MATCH_TEXT = (htmlElement, criteria) -> HttpUnitUtils.matches(((WebLink) htmlElement).getText(),
1✔
197
                (String) criteria);
198

199
        MATCH_CONTAINED_TEXT = (htmlElement, criteria) -> HttpUnitUtils.contains(((WebLink) htmlElement).getText(),
1✔
200
                (String) criteria);
201

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

204
        MATCH_NAME = (htmlElement, criteria) -> HttpUnitUtils.matches(((WebLink) htmlElement).getName(),
1✔
205
                (String) criteria);
206

207
    }
1✔
208

209
}
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