• 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

85.16
/src/main/java/com/meterware/httpunit/HTMLPage.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.parsing.DocumentAdapter;
11
import com.meterware.httpunit.parsing.HTMLParserFactory;
12
import com.meterware.httpunit.scripting.NamedDelegate;
13
import com.meterware.httpunit.scripting.ScriptableDelegate;
14
import com.meterware.httpunit.scripting.ScriptingHandler;
15

16
import java.io.IOException;
17
import java.net.URL;
18
import java.util.ArrayList;
19
import java.util.List;
20

21
import org.w3c.dom.Document;
22
import org.w3c.dom.Element;
23
import org.w3c.dom.NodeList;
24
import org.w3c.dom.html.HTMLDocument;
25
import org.xml.sax.SAXException;
26

27
/**
28
 * This class represents an HTML page returned from a request.
29
 **/
30
public class HTMLPage extends ParsedHTML {
31

32
    /** The scriptable. */
33
    private Scriptable _scriptable;
34

35
    /**
36
     * Instantiates a new HTML page.
37
     *
38
     * @param response
39
     *            the response
40
     * @param frame
41
     *            the frame
42
     * @param baseURL
43
     *            the base URL
44
     * @param baseTarget
45
     *            the base target
46
     * @param characterSet
47
     *            the character set
48
     */
49
    HTMLPage(WebResponse response, FrameSelector frame, URL baseURL, String baseTarget, String characterSet) {
50
        super(response, frame, baseURL, baseTarget, null, characterSet);
1✔
51
    }
1✔
52

53
    /**
54
     * Returns the title of the page.
55
     *
56
     * @return the title
57
     *
58
     * @throws SAXException
59
     *             the SAX exception
60
     */
61
    public String getTitle() throws SAXException {
62
        NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName("title");
1✔
63
        if (nl.getLength() == 0 || !nl.item(0).hasChildNodes()) {
1!
64
            return "";
×
65
        }
66
        return nl.item(0).getFirstChild().getNodeValue();
1✔
67
    }
68

69
    /**
70
     * Returns the onLoad event script.
71
     *
72
     * @return the on load event
73
     *
74
     * @throws SAXException
75
     *             the SAX exception
76
     */
77
    public String getOnLoadEvent() throws SAXException {
78
        Element mainElement = getMainElement((Document) getOriginalDOM());
1✔
79
        return mainElement == null ? "" : mainElement.getAttribute("onload");
1!
80
    }
81

82
    /**
83
     * Gets the main element.
84
     *
85
     * @param document
86
     *            the document
87
     *
88
     * @return the main element
89
     */
90
    private Element getMainElement(Document document) {
91
        NodeList nl = document.getElementsByTagName("frameset");
1✔
92
        if (nl.getLength() == 0) {
1✔
93
            nl = document.getElementsByTagName("body");
1✔
94
        }
95
        return nl.getLength() == 0 ? null : (Element) nl.item(0);
1!
96
    }
97

98
    /**
99
     * Returns the location of the linked stylesheet in the head <code> <link type="text/css" rel="stylesheet"
100
     * href="/mystyle.css" /> </code>
101
     *
102
     * @return the external style sheet
103
     *
104
     * @throws SAXException
105
     *             the SAX exception
106
     */
107
    public String getExternalStyleSheet() throws SAXException {
108
        NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName("link");
1✔
109
        int length = nl.getLength();
1✔
110
        if (length == 0) {
1!
111
            return "";
×
112
        }
113

114
        for (int i = 0; i < length; i++) {
1!
115
            if ("stylesheet".equalsIgnoreCase(NodeUtils.getNodeAttribute(nl.item(i), "rel"))) {
1✔
116
                return NodeUtils.getNodeAttribute(nl.item(i), "href");
1✔
117
            }
118
        }
119
        return "";
×
120
    }
121

122
    /**
123
     * Retrieves the "content" of the meta tags for a key pair attribute-attributeValue. &lt;code&gt; &lt;meta
124
     * name="robots" content="index" /&gt; &lt;meta name="robots" content="follow" /&gt; &lt;meta http-equiv="Expires"
125
     * content="now" /&gt; &lt;/code&gt; this can be used like this &lt;code&gt; getMetaTagContent("name","robots") will
126
     * return { "index","follow" } getMetaTagContent("http-equiv","Expires") will return { "now" } &lt;/code&gt;
127
     *
128
     * @param attribute
129
     *            the attribute
130
     * @param attributeValue
131
     *            the attribute value
132
     *
133
     * @return the meta tag content
134
     */
135
    public String[] getMetaTagContent(String attribute, String attributeValue) {
136
        List<String> matches = new ArrayList<>();
1✔
137
        NodeList nl = ((Document) getOriginalDOM()).getElementsByTagName("meta");
1✔
138
        int length = nl.getLength();
1✔
139

140
        for (int i = 0; i < length; i++) {
1✔
141
            if (attributeValue.equalsIgnoreCase(NodeUtils.getNodeAttribute(nl.item(i), attribute))) {
1✔
142
                matches.add(NodeUtils.getNodeAttribute(nl.item(i), "content"));
1✔
143
            }
144
        }
145
        return matches.toArray(new String[0]);
1✔
146
    }
147

148
    /**
149
     * scriptable for HTML Page.
150
     */
151

152
    public class Scriptable extends ScriptableDelegate {
153

154
        /**
155
         * get the Object with the given propertyName
156
         *
157
         * @param propertyName
158
         *            - the name of the property
159
         */
160
        @Override
161
        public Object get(String propertyName) {
162
            NamedDelegate delegate = getNamedItem(getForms(), propertyName);
1✔
163
            if (delegate != null) {
1✔
164
                return delegate;
1✔
165
            }
166

167
            delegate = getNamedItem(getLinks(), propertyName);
1✔
168
            if (delegate != null) {
1✔
169
                return delegate;
1✔
170
            }
171

172
            return getNamedItem(getImages(), propertyName);
1✔
173
        }
174

175
        /**
176
         * Gets the named item.
177
         *
178
         * @param items
179
         *            the items
180
         * @param name
181
         *            the name
182
         *
183
         * @return the named item
184
         */
185
        private NamedDelegate getNamedItem(ScriptingHandler[] items, String name) {
186
            if (name == null) {
1!
187
                return null;
×
188
            }
189
            for (ScriptingHandler item : items) {
1✔
190
                if (item instanceof NamedDelegate && name.equals(((NamedDelegate) item).getName())) {
1!
191
                    return (NamedDelegate) item;
1✔
192
                }
193
            }
194
            return null;
1✔
195
        }
196

197
        /**
198
         * Sets the value of the named property. Will throw a runtime exception if the property does not exist or cannot
199
         * accept the specified value.
200
         **/
201
        @Override
202
        public void set(String propertyName, Object value) {
203
            if (propertyName.equalsIgnoreCase("location")) {
×
204
                getResponse().getScriptableObject().set("location", value);
×
205
            } else {
206
                super.set(propertyName, value);
×
207
            }
208
        }
×
209

210
        /**
211
         * Gets the parent.
212
         *
213
         * @return the parent
214
         */
215
        public WebResponse.Scriptable getParent() {
216
            return getResponse().getScriptableObject();
×
217
        }
218

219
        /**
220
         * Gets the title.
221
         *
222
         * @return the title
223
         *
224
         * @throws SAXException
225
         *             the SAX exception
226
         */
227
        public String getTitle() throws SAXException {
228
            return HTMLPage.this.getTitle();
1✔
229
        }
230

231
        /**
232
         * Gets the links.
233
         *
234
         * @return the links
235
         */
236
        public ScriptingHandler[] getLinks() {
237
            WebLink[] links = HTMLPage.this.getLinks();
1✔
238
            ScriptingHandler[] result = new WebLink.Scriptable[links.length];
1✔
239
            for (int i = 0; i < links.length; i++) {
1✔
240
                result[i] = links[i].getScriptingHandler();
1✔
241
            }
242
            return result;
1✔
243
        }
244

245
        /**
246
         * Gets the forms.
247
         *
248
         * @return the forms
249
         */
250
        public ScriptingHandler[] getForms() {
251
            WebForm[] forms = HTMLPage.this.getForms();
1✔
252
            ScriptingHandler[] result = new ScriptingHandler[forms.length];
1✔
253
            for (int i = 0; i < forms.length; i++) {
1✔
254
                result[i] = forms[i].getScriptingHandler();
1✔
255
            }
256
            return result;
1✔
257
        }
258

259
        /**
260
         * Gets the images.
261
         *
262
         * @return the images
263
         */
264
        public ScriptingHandler[] getImages() {
265
            WebImage[] images = HTMLPage.this.getImages();
1✔
266
            ScriptingHandler[] result = new WebImage.Scriptable[images.length];
1✔
267
            for (int i = 0; i < images.length; i++) {
1✔
268
                result[i] = images[i].getScriptingHandler();
1✔
269
            }
270
            return result;
1✔
271
        }
272

273
        /**
274
         * Instantiates a new scriptable.
275
         */
276
        Scriptable() {
1✔
277
        }
1✔
278

279
        /**
280
         * Replace text.
281
         *
282
         * @param text
283
         *            the text
284
         * @param contentType
285
         *            the content type
286
         *
287
         * @return true, if successful
288
         */
289
        public boolean replaceText(String text, String contentType) {
290
            return getResponse().replaceText(text, contentType);
1✔
291
        }
292

293
        /**
294
         * Sets the cookie.
295
         *
296
         * @param name
297
         *            the name
298
         * @param value
299
         *            the value
300
         */
301
        public void setCookie(String name, String value) {
302
            getResponse().setCookie(name, value);
1✔
303
        }
1✔
304

305
        /**
306
         * Gets the cookie.
307
         *
308
         * @return the cookie
309
         */
310
        public String getCookie() {
311
            return emptyIfNull(getResponse().getCookieHeader());
1✔
312
        }
313

314
        /**
315
         * Empty if null.
316
         *
317
         * @param string
318
         *            the string
319
         *
320
         * @return the string
321
         */
322
        private String emptyIfNull(String string) {
323
            return string == null ? "" : string;
1✔
324
        }
325

326
        /**
327
         * Gets the element with ID.
328
         *
329
         * @param id
330
         *            the id
331
         *
332
         * @return the element with ID
333
         */
334
        public ScriptableDelegate getElementWithID(String id) {
335
            final HTMLElement elementWithID = HTMLPage.this.getElementWithID(id);
1✔
336
            return elementWithID == null ? null : (ScriptableDelegate) elementWithID.getScriptingHandler();
1✔
337
        }
338

339
        /**
340
         * Gets the elements by name.
341
         *
342
         * @param name
343
         *            the name
344
         *
345
         * @return the elements by name
346
         */
347
        public ScriptableDelegate[] getElementsByName(String name) {
348
            return getDelegates(HTMLPage.this.getElementsWithName(name));
1✔
349
        }
350

351
        /**
352
         * Gets the elements by tag name.
353
         *
354
         * @param name
355
         *            the name
356
         *
357
         * @return the elements by tag name
358
         */
359
        public ScriptableDelegate[] getElementsByTagName(String name) {
360
            return getDelegates(HTMLPage.this.getElementsByTagName(HTMLPage.this.getRootNode(), name));
1✔
361
        }
362
    }
363

364
    /**
365
     * Gets the scriptable object.
366
     *
367
     * @return the scriptable object
368
     */
369
    Scriptable getScriptableObject() {
370
        if (_scriptable == null) {
1✔
371
            _scriptable = new Scriptable();
1✔
372
            _scriptable.setScriptEngine(getResponse().getScriptableObject().getScriptEngine(_scriptable));
1✔
373
        }
374
        return _scriptable;
1✔
375
    }
376

377
    /**
378
     * parse the given test with the given URL.
379
     *
380
     * @param text
381
     *            the text
382
     * @param pageURL
383
     *            the page URL
384
     *
385
     * @throws SAXException
386
     *             the SAX exception
387
     * @throws IOException
388
     *             Signals that an I/O exception has occurred.
389
     */
390
    public void parse(String text, URL pageURL) throws SAXException, IOException {
391
        HTMLParserFactory.getHTMLParser().parse(pageURL, text, new DocumentAdapter() {
1✔
392
            @Override
393
            public void setDocument(HTMLDocument document) {
394
                HTMLPage.this.setRootNode(document);
1✔
395
            }
1✔
396

397
            @Override
398
            public String getIncludedScript(String srcAttribute) throws IOException {
399
                return HTMLPage.this.getIncludedScript(srcAttribute);
1✔
400
            }
401

402
            @Override
403
            public ScriptingHandler getScriptingHandler() {
404
                return getResponse().getScriptingHandler();
1✔
405
            }
406
        });
407
    }
1✔
408

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