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

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

0.81 hits per line

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

85.38
/src/main/java/com/meterware/httpunit/HTMLPage.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.parsing.DocumentAdapter;
23
import com.meterware.httpunit.parsing.HTMLParserFactory;
24
import com.meterware.httpunit.scripting.NamedDelegate;
25
import com.meterware.httpunit.scripting.ScriptableDelegate;
26
import com.meterware.httpunit.scripting.ScriptingHandler;
27

28
import java.io.IOException;
29
import java.net.URL;
30
import java.util.Vector;
31

32
import org.w3c.dom.Document;
33
import org.w3c.dom.Element;
34
import org.w3c.dom.NodeList;
35
import org.w3c.dom.html.HTMLDocument;
36
import org.xml.sax.SAXException;
37

38
/**
39
 * This class represents an HTML page returned from a request.
40
 **/
41
public class HTMLPage extends ParsedHTML {
42

43
    private Scriptable _scriptable;
44

45
    HTMLPage(WebResponse response, FrameSelector frame, URL baseURL, String baseTarget, String characterSet) {
46
        super(response, frame, baseURL, baseTarget, null, characterSet);
1✔
47
    }
1✔
48

49
    /**
50
     * Returns the title of the page.
51
     **/
52
    public String getTitle() throws SAXException {
53
        NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName("title");
1✔
54
        if (nl.getLength() == 0 || !nl.item(0).hasChildNodes()) {
1!
55
            return "";
×
56
        }
57
        return nl.item(0).getFirstChild().getNodeValue();
1✔
58
    }
59

60
    /**
61
     * Returns the onLoad event script.
62
     */
63
    public String getOnLoadEvent() throws SAXException {
64
        Element mainElement = getMainElement((Document) getOriginalDOM());
1✔
65
        return mainElement == null ? "" : mainElement.getAttribute("onload");
1!
66
    }
67

68
    private Element getMainElement(Document document) {
69
        NodeList nl = document.getElementsByTagName("frameset");
1✔
70
        if (nl.getLength() == 0) {
1✔
71
            nl = document.getElementsByTagName("body");
1✔
72
        }
73
        return nl.getLength() == 0 ? null : (Element) nl.item(0);
1!
74
    }
75

76
    /**
77
     * Returns the location of the linked stylesheet in the head <code> <link type="text/css" rel="stylesheet"
78
     * href="/mystyle.css" /> </code>
79
     **/
80
    public String getExternalStyleSheet() throws SAXException {
81
        NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName("link");
1✔
82
        int length = nl.getLength();
1✔
83
        if (length == 0) {
1!
84
            return "";
×
85
        }
86

87
        for (int i = 0; i < length; i++) {
1!
88
            if ("stylesheet".equalsIgnoreCase(NodeUtils.getNodeAttribute(nl.item(i), "rel"))) {
1✔
89
                return NodeUtils.getNodeAttribute(nl.item(i), "href");
1✔
90
            }
91
        }
92
        return "";
×
93
    }
94

95
    /**
96
     * Retrieves the "content" of the meta tags for a key pair attribute-attributeValue. &lt;code&gt; &lt;meta
97
     * name="robots" content="index" /&gt; &lt;meta name="robots" content="follow" /&gt; &lt;meta http-equiv="Expires"
98
     * content="now" /&gt; &lt;/code&gt; this can be used like this &lt;code&gt; getMetaTagContent("name","robots") will
99
     * return { "index","follow" } getMetaTagContent("http-equiv","Expires") will return { "now" } &lt;/code&gt;
100
     **/
101
    public String[] getMetaTagContent(String attribute, String attributeValue) {
102
        Vector matches = new Vector<>();
1✔
103
        NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName("meta");
1✔
104
        int length = nl.getLength();
1✔
105

106
        for (int i = 0; i < length; i++) {
1✔
107
            if (attributeValue.equalsIgnoreCase(NodeUtils.getNodeAttribute(nl.item(i), attribute))) {
1✔
108
                matches.addElement(NodeUtils.getNodeAttribute(nl.item(i), "content"));
1✔
109
            }
110
        }
111
        String[] result = new String[matches.size()];
1✔
112
        matches.copyInto(result);
1✔
113
        return result;
1✔
114
    }
115

116
    /**
117
     * scriptable for HTML Page
118
     */
119

120
    public class Scriptable extends ScriptableDelegate {
121

122
        /**
123
         * get the Object with the given propertyName
124
         *
125
         * @param propertyName
126
         *            - the name of the property
127
         */
128
        @Override
129
        public Object get(String propertyName) {
130
            NamedDelegate delegate = getNamedItem(getForms(), propertyName);
1✔
131
            if (delegate != null) {
1✔
132
                return delegate;
1✔
133
            }
134

135
            delegate = getNamedItem(getLinks(), propertyName);
1✔
136
            if (delegate != null) {
1✔
137
                return delegate;
1✔
138
            }
139

140
            return getNamedItem(getImages(), propertyName);
1✔
141
        }
142

143
        private NamedDelegate getNamedItem(ScriptingHandler[] items, String name) {
144
            if (name == null) {
1!
145
                return null;
×
146
            }
147
            for (ScriptingHandler item : items) {
1✔
148
                if (item instanceof NamedDelegate && name.equals(((NamedDelegate) item).getName())) {
1!
149
                    return (NamedDelegate) item;
1✔
150
                }
151
            }
152
            return null;
1✔
153
        }
154

155
        /**
156
         * Sets the value of the named property. Will throw a runtime exception if the property does not exist or cannot
157
         * accept the specified value.
158
         **/
159
        @Override
160
        public void set(String propertyName, Object value) {
161
            if (propertyName.equalsIgnoreCase("location")) {
×
162
                getResponse().getScriptableObject().set("location", value);
×
163
            } else {
164
                super.set(propertyName, value);
×
165
            }
166
        }
×
167

168
        public WebResponse.Scriptable getParent() {
169
            return getResponse().getScriptableObject();
×
170
        }
171

172
        public String getTitle() throws SAXException {
173
            return HTMLPage.this.getTitle();
1✔
174
        }
175

176
        public ScriptingHandler[] getLinks() {
177
            WebLink[] links = HTMLPage.this.getLinks();
1✔
178
            ScriptingHandler[] result = new WebLink.Scriptable[links.length];
1✔
179
            for (int i = 0; i < links.length; i++) {
1✔
180
                result[i] = links[i].getScriptingHandler();
1✔
181
            }
182
            return result;
1✔
183
        }
184

185
        public ScriptingHandler[] getForms() {
186
            WebForm[] forms = HTMLPage.this.getForms();
1✔
187
            ScriptingHandler[] result = new ScriptingHandler[forms.length];
1✔
188
            for (int i = 0; i < forms.length; i++) {
1✔
189
                result[i] = forms[i].getScriptingHandler();
1✔
190
            }
191
            return result;
1✔
192
        }
193

194
        public ScriptingHandler[] getImages() {
195
            WebImage[] images = HTMLPage.this.getImages();
1✔
196
            ScriptingHandler[] result = new WebImage.Scriptable[images.length];
1✔
197
            for (int i = 0; i < images.length; i++) {
1✔
198
                result[i] = images[i].getScriptingHandler();
1✔
199
            }
200
            return result;
1✔
201
        }
202

203
        Scriptable() {
1✔
204
        }
1✔
205

206
        public boolean replaceText(String text, String contentType) {
207
            return getResponse().replaceText(text, contentType);
1✔
208
        }
209

210
        public void setCookie(String name, String value) {
211
            getResponse().setCookie(name, value);
1✔
212
        }
1✔
213

214
        public String getCookie() {
215
            return emptyIfNull(getResponse().getCookieHeader());
1✔
216
        }
217

218
        private String emptyIfNull(String string) {
219
            return string == null ? "" : string;
1✔
220
        }
221

222
        public ScriptableDelegate getElementWithID(String id) {
223
            final HTMLElement elementWithID = HTMLPage.this.getElementWithID(id);
1✔
224
            return elementWithID == null ? null : (ScriptableDelegate) elementWithID.getScriptingHandler();
1✔
225
        }
226

227
        public ScriptableDelegate[] getElementsByName(String name) {
228
            return getDelegates(HTMLPage.this.getElementsWithName(name));
1✔
229
        }
230

231
        public ScriptableDelegate[] getElementsByTagName(String name) {
232
            return getDelegates(HTMLPage.this.getElementsByTagName(HTMLPage.this.getRootNode(), name));
1✔
233
        }
234
    }
235

236
    Scriptable getScriptableObject() {
237
        if (_scriptable == null) {
1✔
238
            _scriptable = new Scriptable();
1✔
239
            _scriptable.setScriptEngine(getResponse().getScriptableObject().getScriptEngine(_scriptable));
1✔
240
        }
241
        return _scriptable;
1✔
242
    }
243

244
    /**
245
     * parse the given test with the given URL
246
     *
247
     * @param text
248
     * @param pageURL
249
     *
250
     * @throws SAXException
251
     * @throws IOException
252
     */
253
    public void parse(String text, URL pageURL) throws SAXException, IOException {
254
        HTMLParserFactory.getHTMLParser().parse(pageURL, text, new DocumentAdapter() {
1✔
255
            @Override
256
            public void setDocument(HTMLDocument document) {
257
                HTMLPage.this.setRootNode(document);
1✔
258
            }
1✔
259

260
            @Override
261
            public String getIncludedScript(String srcAttribute) throws IOException {
262
                return HTMLPage.this.getIncludedScript(srcAttribute);
1✔
263
            }
264

265
            @Override
266
            public ScriptingHandler getScriptingHandler() {
267
                return getResponse().getScriptingHandler();
1✔
268
            }
269
        });
270
    }
1✔
271

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