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

hazendaz / httpunit / 389

12 Aug 2025 11:17PM UTC coverage: 80.48% (-0.02%) from 80.503%
389

push

github

hazendaz
Merge branch 'master' into javax

3216 of 4105 branches covered (78.34%)

Branch coverage included in aggregate %.

238 of 258 new or added lines in 68 files covered. (92.25%)

2 existing lines in 2 files now uncovered.

8254 of 10147 relevant lines covered (81.34%)

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

NEW
43
    private static final Logger logger = LoggerFactory.getLogger(DomBasedScriptingEngineFactory.class);
×
44

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

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

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

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

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

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

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

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

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

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

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

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