• 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

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

10
import com.meterware.httpunit.HTMLElement;
11
import com.meterware.httpunit.HttpUnitUtils;
12
import com.meterware.httpunit.WebResponse;
13
import com.meterware.httpunit.javascript.JavaScript;
14
import com.meterware.httpunit.javascript.ScriptingEngineImpl;
15
import com.meterware.httpunit.scripting.ScriptingEngineFactory;
16
import com.meterware.httpunit.scripting.ScriptingHandler;
17

18
import org.mozilla.javascript.Context;
19
import org.mozilla.javascript.EcmaError;
20
import org.mozilla.javascript.Function;
21
import org.mozilla.javascript.JavaScriptException;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24
import org.w3c.dom.html.HTMLDocument;
25

26
/**
27
 * The scripting engine factory which relies directly on the DOM.
28
 */
29
public class DomBasedScriptingEngineFactory implements ScriptingEngineFactory {
×
30

31
    /** The Constant logger. */
32
    private static final Logger logger = LoggerFactory.getLogger(DomBasedScriptingEngineFactory.class);
×
33

34
    /**
35
     * check whether this ScriptingEngineFactory is enabled
36
     */
37
    @Override
38
    public boolean isEnabled() {
39
        try {
40
            Class.forName("org.mozilla.javascript.Context");
×
41
            return true;
×
42
        } catch (Exception e) {
×
43
            logger.warn("Rhino classes (js.jar) not found - Javascript disabled");
×
44
            return false;
×
45
        }
46
    }
47

48
    /**
49
     * associate me with a webresponse
50
     *
51
     * @param response
52
     *            - the WebResponse to use
53
     */
54
    @Override
55
    public void associate(WebResponse response) {
56
        try {
57
            // JavaScript.run( response ); // can't do this (yet?)
58
        } catch (RuntimeException e) {
59
            throw e;
60
        } catch (Exception e) {
61
            HttpUnitUtils.handleException(e);
62
            throw new RuntimeException(e.toString());
63
        }
64
    }
×
65

66
    /**
67
     * load.
68
     *
69
     * @param response
70
     *            the response
71
     */
72
    @Override
73
    public void load(WebResponse response) {
74
        Function onLoadEvent = null;
×
75
        try {
76
            Context context = Context.enter();
×
77
            context.initStandardObjects(null);
×
78

79
            HTMLDocument htmlDocument = ((DomWindow) response.getScriptingHandler()).getDocument();
×
80
            if (!(htmlDocument instanceof HTMLDocumentImpl)) {
×
81
                return;
×
82
            }
83

84
            HTMLBodyElementImpl body = (HTMLBodyElementImpl) htmlDocument.getBody();
×
85
            if (body == null) {
×
86
                return;
×
87
            }
88
            onLoadEvent = body.getOnloadEvent();
×
89
            if (onLoadEvent == null) {
×
90
                return;
×
91
            }
92
            onLoadEvent.call(context, body, body, new Object[0]);
×
93
        } catch (JavaScriptException | EcmaError ee) {
×
94
            // throw ee;
95
            ScriptingEngineImpl.handleScriptException(ee, onLoadEvent.toString());
×
96
        } finally {
97
            Context.exit();
×
98
        }
99
    }
×
100

101
    /**
102
     * setter for the throwExceptions flag
103
     *
104
     * @param throwExceptions
105
     *            - true if Exceptions should be thrown
106
     */
107
    @Override
108
    public void setThrowExceptionsOnError(boolean throwExceptions) {
109
        JavaScript.setThrowExceptionsOnError(throwExceptions);
×
110
    }
×
111

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

122
    @Override
123
    public String[] getErrorMessages() {
124
        return ScriptingEngineImpl.getErrorMessages();
×
125
    }
126

127
    @Override
128
    public void clearErrorMessages() {
129
        ScriptingEngineImpl.clearErrorMessages();
×
130
    }
×
131

132
    @Override
133
    public ScriptingHandler createHandler(HTMLElement elementBase) {
134
        return (ScriptingHandler) elementBase.getNode();
×
135
    }
136

137
    @Override
138
    public ScriptingHandler createHandler(WebResponse response) {
139
        return response.createDomScriptingHandler();
×
140
    }
141

142
    /**
143
     * handle Exceptions
144
     *
145
     * @param e
146
     *            - the exception to handle
147
     * @param badScript
148
     *            - the script that caused the problem
149
     */
150
    @Override
151
    public void handleScriptException(Exception e, String badScript) {
152
        ScriptingEngineImpl.handleScriptException(e, badScript);
×
153
    }
×
154
}
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