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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

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
 * 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;
21

22
import com.meterware.httpunit.dom.HTMLButtonElementImpl;
23
import com.meterware.httpunit.dom.HTMLControl;
24
import com.meterware.httpunit.dom.HTMLInputElementImpl;
25
import com.meterware.httpunit.protocol.ParameterProcessor;
26
import com.meterware.httpunit.scripting.ScriptableDelegate;
27

28
import java.io.IOException;
29

30
import org.xml.sax.SAXException;
31

32
/**
33
 * A button in a form.
34
 **/
35
public class Button extends FormControl {
36

37
    /** The Constant WITH_ID. */
38
    public static final HTMLElementPredicate WITH_ID;
39

40
    /** The Constant WITH_LABEL. */
41
    public static final HTMLElementPredicate WITH_LABEL;
42

43
    /** The base response. */
44
    private WebResponse _baseResponse;
45

46
    /** The was enabled. */
47
    // remember the last verifyEnabled result
48
    private boolean _wasEnabled = true;
1✔
49

50
    @Override
51
    public String getType() {
52
        return BUTTON_TYPE;
1✔
53
    }
54

55
    /**
56
     * Instantiates a new button.
57
     *
58
     * @param form
59
     *            the form
60
     */
61
    Button(WebForm form) {
62
        super(form);
1✔
63
    }
1✔
64

65
    /**
66
     * construct a button from the given html control and assign the event handlers for onclick, onmousedown and
67
     * onmouseup.
68
     *
69
     * @param form
70
     *            the form
71
     * @param control
72
     *            the control
73
     */
74
    Button(WebForm form, HTMLControl control) {
75
        super(form, control);
1✔
76
    }
1✔
77

78
    /**
79
     * Instantiates a new button.
80
     *
81
     * @param response
82
     *            the response
83
     * @param control
84
     *            the control
85
     */
86
    Button(WebResponse response, HTMLControl control) {
87
        super(null, control);
1✔
88
        _baseResponse = response;
1✔
89
    }
1✔
90

91
    /**
92
     * Returns the value associated with this button.
93
     *
94
     * @return the value
95
     */
96
    public String getValue() {
97
        return emptyIfNull(getNode() instanceof HTMLInputElementImpl ? ((HTMLInputElementImpl) getNode()).getValue()
1✔
98
                : ((HTMLButtonElementImpl) getNode()).getValue());
1✔
99
    }
100

101
    /**
102
     * the onClickSequence for this button.
103
     *
104
     * @param x
105
     *            the x
106
     * @param y
107
     *            the y
108
     *
109
     * @return true if the even was handled
110
     *
111
     * @throws IOException
112
     *             Signals that an I/O exception has occurred.
113
     * @throws SAXException
114
     *             the SAX exception
115
     */
116
    protected boolean doOnClickSequence(int x, int y) throws IOException, SAXException {
117
        verifyButtonEnabled();
1✔
118
        boolean result = doOnClickEvent();
1✔
119
        if (result) {
1✔
120
            doButtonAction(x, y);
1✔
121
        }
122
        this.rememberEnableState();
1✔
123
        return result;
1✔
124
    }
125

126
    /**
127
     * Performs the action associated with clicking this button after running any 'onClick' script. For a submit button
128
     * this typically submits the form.
129
     *
130
     * @throws IOException
131
     *             Signals that an I/O exception has occurred.
132
     * @throws SAXException
133
     *             the SAX exception
134
     */
135
    public void click() throws IOException, SAXException {
136
        doOnClickSequence(0, 0);
1✔
137
    }
1✔
138

139
    /**
140
     * Was enabled.
141
     *
142
     * @return true, if successful
143
     */
144
    boolean wasEnabled() {
145
        return _wasEnabled;
1✔
146
    }
147

148
    /**
149
     * remember wether the button was enabled.
150
     */
151
    public void rememberEnableState() {
152
        _wasEnabled = !isDisabled();
1✔
153
    }
1✔
154

155
    /**
156
     * verifyButtonEnabled.
157
     */
158
    protected void verifyButtonEnabled() {
159
        rememberEnableState();
1✔
160
        if (isDisabled()) {
1✔
161
            throwDisabledException();
×
162
        }
163
    }
1✔
164

165
    /**
166
     * throw an exception that I'm disbled.
167
     */
168
    public void throwDisabledException() {
169
        throw new DisabledButtonException(this);
1✔
170
    }
171

172
    /**
173
     * This exception is thrown on an attempt to click on a button disabled button.
174
     */
175
    class DisabledButtonException extends IllegalStateException {
176

177
        /** The Constant serialVersionUID. */
178
        private static final long serialVersionUID = 1L;
179

180
        /**
181
         * Instantiates a new disabled button exception.
182
         *
183
         * @param button
184
         *            the button
185
         */
186
        DisabledButtonException(Button button) {
1✔
187
            _name = button.getName();
1✔
188
            _value = button.getValue();
1✔
189
        }
1✔
190

191
        @Override
192
        public String getMessage() {
193
            return "Button" + (getName().isEmpty() ? "" : " '" + getName() + "'")
1!
194
                    + " is disabled and may not be clicked.";
195
        }
196

197
        /** The name. */
198
        protected String _name;
199

200
        /** The value. */
201
        protected String _value;
202

203
    }
204

205
    /**
206
     * Returns true if this button is disabled, meaning that it cannot be clicked.
207
     **/
208
    @Override
209
    public boolean isDisabled() {
210
        return super.isDisabled();
1✔
211
    }
212

213
    /**
214
     * Perform the normal action of this button.
215
     *
216
     * @param x
217
     *            - the x coordinate
218
     * @param y
219
     *            - the y coordinate
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(int x, int y) throws IOException, SAXException {
227
        // pseudo - abstract
228
    }
1✔
229

230
    /**
231
     * Perform the normal action of this button. delegate to the x-y version
232
     *
233
     * @throws IOException
234
     *             Signals that an I/O exception has occurred.
235
     * @throws SAXException
236
     *             the SAX exception
237
     */
238
    protected void doButtonAction() throws IOException, SAXException {
239
        doButtonAction(0, 0);
1✔
240
    }
1✔
241

242
    // -------------------------------------------------- FormControl methods
243
    // -----------------------------------------------
244

245
    @Override
246
    protected String[] getValues() {
247
        return new String[0];
×
248
    }
249

250
    @Override
251
    protected void addValues(ParameterProcessor processor, String characterSet) throws IOException {
252
    }
1✔
253

254
    @Override
255
    public ScriptableDelegate newScriptable() {
256
        return new Scriptable();
1✔
257
    }
258

259
    @Override
260
    public ScriptableDelegate getParentDelegate() {
261
        if (getForm() != null) {
1✔
262
            return super.getParentDelegate();
1✔
263
        }
264
        return _baseResponse.getDocumentScriptable();
1✔
265
    }
266

267
    /**
268
     * The Class Scriptable.
269
     */
270
    class Scriptable extends FormControl.Scriptable {
1✔
271

272
        @Override
273
        public void click() throws IOException, SAXException {
274
            doButtonAction();
1✔
275
        }
1✔
276
    }
277

278
    static {
279
        WITH_ID = (button, id) -> ((Button) button).getID().equals(id);
1✔
280

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

283
    }
1✔
284
}
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