• 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

95.0
/src/main/java/com/meterware/httpunit/Button.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.dom.HTMLButtonElementImpl;
11
import com.meterware.httpunit.dom.HTMLControl;
12
import com.meterware.httpunit.dom.HTMLInputElementImpl;
13
import com.meterware.httpunit.protocol.ParameterProcessor;
14
import com.meterware.httpunit.scripting.ScriptableDelegate;
15

16
import java.io.IOException;
17

18
import org.xml.sax.SAXException;
19

20
/**
21
 * A button in a form.
22
 **/
23
public class Button extends FormControl {
24

25
    /** The Constant WITH_ID. */
26
    public static final HTMLElementPredicate WITH_ID;
27

28
    /** The Constant WITH_LABEL. */
29
    public static final HTMLElementPredicate WITH_LABEL;
30

31
    /** The base response. */
32
    private WebResponse _baseResponse;
33

34
    /** The was enabled. */
35
    // remember the last verifyEnabled result
36
    private boolean _wasEnabled = true;
1✔
37

38
    @Override
39
    public String getType() {
40
        return BUTTON_TYPE;
1✔
41
    }
42

43
    /**
44
     * Instantiates a new button.
45
     *
46
     * @param form
47
     *            the form
48
     */
49
    Button(WebForm form) {
50
        super(form);
1✔
51
    }
1✔
52

53
    /**
54
     * construct a button from the given html control and assign the event handlers for onclick, onmousedown and
55
     * onmouseup.
56
     *
57
     * @param form
58
     *            the form
59
     * @param control
60
     *            the control
61
     */
62
    Button(WebForm form, HTMLControl control) {
63
        super(form, control);
1✔
64
    }
1✔
65

66
    /**
67
     * Instantiates a new button.
68
     *
69
     * @param response
70
     *            the response
71
     * @param control
72
     *            the control
73
     */
74
    Button(WebResponse response, HTMLControl control) {
75
        super(null, control);
1✔
76
        _baseResponse = response;
1✔
77
    }
1✔
78

79
    /**
80
     * Returns the value associated with this button.
81
     *
82
     * @return the value
83
     */
84
    public String getValue() {
85
        return emptyIfNull(getNode() instanceof HTMLInputElementImpl ? ((HTMLInputElementImpl) getNode()).getValue()
1✔
86
                : ((HTMLButtonElementImpl) getNode()).getValue());
1✔
87
    }
88

89
    /**
90
     * the onClickSequence for this button.
91
     *
92
     * @param x
93
     *            the x
94
     * @param y
95
     *            the y
96
     *
97
     * @return true if the even was handled
98
     *
99
     * @throws IOException
100
     *             Signals that an I/O exception has occurred.
101
     * @throws SAXException
102
     *             the SAX exception
103
     */
104
    protected boolean doOnClickSequence(int x, int y) throws IOException, SAXException {
105
        verifyButtonEnabled();
1✔
106
        boolean result = doOnClickEvent();
1✔
107
        if (result) {
1✔
108
            doButtonAction(x, y);
1✔
109
        }
110
        this.rememberEnableState();
1✔
111
        return result;
1✔
112
    }
113

114
    /**
115
     * Performs the action associated with clicking this button after running any 'onClick' script. For a submit button
116
     * this typically submits the form.
117
     *
118
     * @throws IOException
119
     *             Signals that an I/O exception has occurred.
120
     * @throws SAXException
121
     *             the SAX exception
122
     */
123
    public void click() throws IOException, SAXException {
124
        doOnClickSequence(0, 0);
1✔
125
    }
1✔
126

127
    /**
128
     * Was enabled.
129
     *
130
     * @return true, if successful
131
     */
132
    boolean wasEnabled() {
133
        return _wasEnabled;
1✔
134
    }
135

136
    /**
137
     * remember wether the button was enabled.
138
     */
139
    public void rememberEnableState() {
140
        _wasEnabled = !isDisabled();
1✔
141
    }
1✔
142

143
    /**
144
     * verifyButtonEnabled.
145
     */
146
    protected void verifyButtonEnabled() {
147
        rememberEnableState();
1✔
148
        if (isDisabled()) {
1✔
149
            throwDisabledException();
×
150
        }
151
    }
1✔
152

153
    /**
154
     * throw an exception that I'm disbled.
155
     */
156
    public void throwDisabledException() {
157
        throw new DisabledButtonException(this);
1✔
158
    }
159

160
    /**
161
     * This exception is thrown on an attempt to click on a button disabled button.
162
     */
163
    class DisabledButtonException extends IllegalStateException {
164

165
        /** The Constant serialVersionUID. */
166
        private static final long serialVersionUID = 1L;
167

168
        /**
169
         * Instantiates a new disabled button exception.
170
         *
171
         * @param button
172
         *            the button
173
         */
174
        DisabledButtonException(Button button) {
1✔
175
            _name = button.getName();
1✔
176
            _value = button.getValue();
1✔
177
        }
1✔
178

179
        @Override
180
        public String getMessage() {
181
            return "Button" + (getName().isEmpty() ? "" : " '" + getName() + "'")
1!
182
                    + " is disabled and may not be clicked.";
183
        }
184

185
        /** The name. */
186
        protected String _name;
187

188
        /** The value. */
189
        protected String _value;
190

191
    }
192

193
    /**
194
     * Returns true if this button is disabled, meaning that it cannot be clicked.
195
     **/
196
    @Override
197
    public boolean isDisabled() {
198
        return super.isDisabled();
1✔
199
    }
200

201
    /**
202
     * Perform the normal action of this button.
203
     *
204
     * @param x
205
     *            - the x coordinate
206
     * @param y
207
     *            - the y coordinate
208
     *
209
     * @throws IOException
210
     *             Signals that an I/O exception has occurred.
211
     * @throws SAXException
212
     *             the SAX exception
213
     */
214
    protected void doButtonAction(int x, int y) throws IOException, SAXException {
215
        // pseudo - abstract
216
    }
1✔
217

218
    /**
219
     * Perform the normal action of this button. delegate to the x-y version
220
     *
221
     * @throws IOException
222
     *             Signals that an I/O exception has occurred.
223
     * @throws SAXException
224
     *             the SAX exception
225
     */
226
    protected void doButtonAction() throws IOException, SAXException {
227
        doButtonAction(0, 0);
1✔
228
    }
1✔
229

230
    // -------------------------------------------------- FormControl methods
231
    // -----------------------------------------------
232

233
    @Override
234
    protected String[] getValues() {
235
        return new String[0];
×
236
    }
237

238
    @Override
239
    protected void addValues(ParameterProcessor processor, String characterSet) throws IOException {
240
    }
1✔
241

242
    @Override
243
    public ScriptableDelegate newScriptable() {
244
        return new Scriptable();
1✔
245
    }
246

247
    @Override
248
    public ScriptableDelegate getParentDelegate() {
249
        if (getForm() != null) {
1✔
250
            return super.getParentDelegate();
1✔
251
        }
252
        return _baseResponse.getDocumentScriptable();
1✔
253
    }
254

255
    /**
256
     * The Class Scriptable.
257
     */
258
    class Scriptable extends FormControl.Scriptable {
1✔
259

260
        @Override
261
        public void click() throws IOException, SAXException {
262
            doButtonAction();
1✔
263
        }
1✔
264
    }
265

266
    static {
267
        WITH_ID = (button, id) -> ((Button) button).getID().equals(id);
1✔
268

269
        WITH_LABEL = (button, label) -> ((Button) button).getValue().equals(label);
1✔
270

271
    }
1✔
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