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

hazendaz / httpunit / 639

05 Dec 2025 03:41AM UTC coverage: 80.509%. Remained the same
639

push

github

hazendaz
[jautodoc] All the source package

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

0.0
/src/main/java/com/meterware/httpunit/dom/DomBasedScriptingEngineFactory.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.dom;
21

22
import com.meterware.httpunit.HTMLElement;
23
import com.meterware.httpunit.HttpUnitUtils;
24
import com.meterware.httpunit.WebResponse;
25
import com.meterware.httpunit.javascript.JavaScript;
26
import com.meterware.httpunit.javascript.ScriptingEngineImpl;
27
import com.meterware.httpunit.scripting.ScriptingEngineFactory;
28
import com.meterware.httpunit.scripting.ScriptingHandler;
29

30
import org.mozilla.javascript.Context;
31
import org.mozilla.javascript.EcmaError;
32
import org.mozilla.javascript.Function;
33
import org.mozilla.javascript.JavaScriptException;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
import org.w3c.dom.html.HTMLDocument;
37

38
/**
39
 * The scripting engine factory which relies directly on the DOM.
40
 */
41
public class DomBasedScriptingEngineFactory implements ScriptingEngineFactory {
×
42

43
    /** The Constant logger. */
44
    private static final Logger logger = LoggerFactory.getLogger(DomBasedScriptingEngineFactory.class);
×
45

46
    /**
47
     * check whether this ScriptingEngineFactory is enabled
48
     */
49
    @Override
50
    public boolean isEnabled() {
51
        try {
52
            Class.forName("org.mozilla.javascript.Context");
×
53
            return true;
×
54
        } catch (Exception e) {
×
55
            logger.warn("Rhino classes (js.jar) not found - Javascript disabled");
×
56
            return false;
×
57
        }
58
    }
59

60
    /**
61
     * associate me with a webresponse
62
     *
63
     * @param response
64
     *            - the WebResponse to use
65
     */
66
    @Override
67
    public void associate(WebResponse response) {
68
        try {
69
            // JavaScript.run( response ); // can't do this (yet?)
70
        } catch (RuntimeException e) {
71
            throw e;
72
        } catch (Exception e) {
73
            HttpUnitUtils.handleException(e);
74
            throw new RuntimeException(e.toString());
75
        }
76
    }
×
77

78
    /**
79
     * load
80
     *
81
     * @param response
82
     */
83
    @Override
84
    public void load(WebResponse response) {
85
        Function onLoadEvent = null;
×
86
        try {
87
            Context context = Context.enter();
×
88
            context.initStandardObjects(null);
×
89

90
            HTMLDocument htmlDocument = ((DomWindow) response.getScriptingHandler()).getDocument();
×
91
            if (!(htmlDocument instanceof HTMLDocumentImpl)) {
×
92
                return;
×
93
            }
94

95
            HTMLBodyElementImpl body = (HTMLBodyElementImpl) htmlDocument.getBody();
×
96
            if (body == null) {
×
97
                return;
×
98
            }
99
            onLoadEvent = body.getOnloadEvent();
×
100
            if (onLoadEvent == null) {
×
101
                return;
×
102
            }
103
            onLoadEvent.call(context, body, body, new Object[0]);
×
104
        } catch (JavaScriptException | EcmaError ee) {
×
105
            // throw ee;
106
            ScriptingEngineImpl.handleScriptException(ee, onLoadEvent.toString());
×
107
        } finally {
108
            Context.exit();
×
109
        }
110
    }
×
111

112
    /**
113
     * setter for the throwExceptions flag
114
     *
115
     * @param throwExceptions
116
     *            - true if Exceptions should be thrown
117
     */
118
    @Override
119
    public void setThrowExceptionsOnError(boolean throwExceptions) {
120
        JavaScript.setThrowExceptionsOnError(throwExceptions);
×
121
    }
×
122

123
    /**
124
     * getter for the throwExceptions flag
125
     *
126
     * @return - true if Exceptions should be thrown
127
     */
128
    @Override
129
    public boolean isThrowExceptionsOnError() {
130
        return JavaScript.isThrowExceptionsOnError();
×
131
    }
132

133
    @Override
134
    public String[] getErrorMessages() {
135
        return ScriptingEngineImpl.getErrorMessages();
×
136
    }
137

138
    @Override
139
    public void clearErrorMessages() {
140
        ScriptingEngineImpl.clearErrorMessages();
×
141
    }
×
142

143
    @Override
144
    public ScriptingHandler createHandler(HTMLElement elementBase) {
145
        return (ScriptingHandler) elementBase.getNode();
×
146
    }
147

148
    @Override
149
    public ScriptingHandler createHandler(WebResponse response) {
150
        return response.createDomScriptingHandler();
×
151
    }
152

153
    /**
154
     * handle Exceptions
155
     *
156
     * @param e
157
     *            - the exception to handle
158
     * @param badScript
159
     *            - the script that caused the problem
160
     */
161
    @Override
162
    public void handleScriptException(Exception e, String badScript) {
163
        ScriptingEngineImpl.handleScriptException(e, badScript);
×
164
    }
×
165
}
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