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

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

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

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
    public static final HTMLElementPredicate WITH_ID;
38
    public static final HTMLElementPredicate WITH_LABEL;
39

40
    private WebResponse _baseResponse;
41
    // remember the last verifyEnabled result
42
    private boolean _wasEnabled = true;
1✔
43

44
    @Override
45
    public String getType() {
46
        return BUTTON_TYPE;
1✔
47
    }
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
     * @param control
59
     */
60
    Button(WebForm form, HTMLControl control) {
61
        super(form, control);
1✔
62
    }
1✔
63

64
    Button(WebResponse response, HTMLControl control) {
65
        super(null, control);
1✔
66
        _baseResponse = response;
1✔
67
    }
1✔
68

69
    /**
70
     * Returns the value associated with this button.
71
     **/
72
    public String getValue() {
73
        return emptyIfNull(getNode() instanceof HTMLInputElementImpl ? ((HTMLInputElementImpl) getNode()).getValue()
1✔
74
                : ((HTMLButtonElementImpl) getNode()).getValue());
1✔
75
    }
76

77
    /**
78
     * the onClickSequence for this button
79
     *
80
     * @return true if the even was handled
81
     */
82
    protected boolean doOnClickSequence(int x, int y) throws IOException, SAXException {
83
        verifyButtonEnabled();
1✔
84
        boolean result = doOnClickEvent();
1✔
85
        if (result) {
1✔
86
            doButtonAction(x, y);
1✔
87
        }
88
        this.rememberEnableState();
1✔
89
        return result;
1✔
90
    }
91

92
    /**
93
     * Performs the action associated with clicking this button after running any 'onClick' script. For a submit button
94
     * this typically submits the form.
95
     */
96
    public void click() throws IOException, SAXException {
97
        doOnClickSequence(0, 0);
1✔
98
    }
1✔
99

100
    boolean wasEnabled() {
101
        return _wasEnabled;
1✔
102
    }
103

104
    /**
105
     * remember wether the button was enabled
106
     */
107
    public void rememberEnableState() {
108
        _wasEnabled = !isDisabled();
1✔
109
    }
1✔
110

111
    /**
112
     * verifyButtonEnabled
113
     */
114
    protected void verifyButtonEnabled() {
115
        rememberEnableState();
1✔
116
        if (isDisabled()) {
1✔
117
            throwDisabledException();
×
118
        }
119
    }
1✔
120

121
    /**
122
     * throw an exception that I'm disbled
123
     */
124
    public void throwDisabledException() {
125
        throw new DisabledButtonException(this);
1✔
126
    }
127

128
    /**
129
     * This exception is thrown on an attempt to click on a button disabled button
130
     **/
131
    class DisabledButtonException extends IllegalStateException {
132

133
        private static final long serialVersionUID = 1L;
134

135
        DisabledButtonException(Button button) {
1✔
136
            _name = button.getName();
1✔
137
            _value = button.getValue();
1✔
138
        }
1✔
139

140
        @Override
141
        public String getMessage() {
142
            return "Button" + (getName().isEmpty() ? "" : " '" + getName() + "'")
1!
143
                    + " is disabled and may not be clicked.";
144
        }
145

146
        protected String _name;
147
        protected String _value;
148

149
    }
150

151
    /**
152
     * Returns true if this button is disabled, meaning that it cannot be clicked.
153
     **/
154
    @Override
155
    public boolean isDisabled() {
156
        return super.isDisabled();
1✔
157
    }
158

159
    /**
160
     * Perform the normal action of this button.
161
     *
162
     * @param x
163
     *            - the x coordinate
164
     * @param y
165
     *            - the y coordinate
166
     *
167
     * @throws IOException
168
     * @throws SAXException
169
     */
170
    protected void doButtonAction(int x, int y) throws IOException, SAXException {
171
        // pseudo - abstract
172
    }
1✔
173

174
    /**
175
     * Perform the normal action of this button. delegate to the x-y version
176
     *
177
     * @throws IOException
178
     * @throws SAXException
179
     */
180
    protected void doButtonAction() throws IOException, SAXException {
181
        doButtonAction(0, 0);
1✔
182
    }
1✔
183

184
    // -------------------------------------------------- FormControl methods
185
    // -----------------------------------------------
186

187
    @Override
188
    protected String[] getValues() {
189
        return new String[0];
×
190
    }
191

192
    @Override
193
    protected void addValues(ParameterProcessor processor, String characterSet) throws IOException {
194
    }
1✔
195

196
    @Override
197
    public ScriptableDelegate newScriptable() {
198
        return new Scriptable();
1✔
199
    }
200

201
    @Override
202
    public ScriptableDelegate getParentDelegate() {
203
        if (getForm() != null) {
1✔
204
            return super.getParentDelegate();
1✔
205
        }
206
        return _baseResponse.getDocumentScriptable();
1✔
207
    }
208

209
    class Scriptable extends FormControl.Scriptable {
1✔
210

211
        @Override
212
        public void click() throws IOException, SAXException {
213
            doButtonAction();
1✔
214
        }
1✔
215
    }
216

217
    static {
218
        WITH_ID = (button, id) -> ((Button) button).getID().equals(id);
1✔
219

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

222
    }
1✔
223
}
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