Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e46edab

Browse files
kucebbrian-mann
authored andcommitted
Fix form submission with type "button" (cypress-io#2278)
* gets type from button * remove extra whitespace * update tests broken by changing dom.html
1 parent 95be7a6 commit e46edab

File tree

9 files changed

+41
-32
lines changed

9 files changed

+41
-32
lines changed

packages/driver/src/cy/commands/actions/type.coffee

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ module.exports = (Commands, Cypress, cy, state, config) ->
8484

8585
isBody = options.$el.is("body")
8686
isTextLike = $dom.isTextLike(options.$el)
87-
isDate = $dom.isInputType(options.$el, "date")
88-
isTime = $dom.isInputType(options.$el, "time")
89-
isMonth = $dom.isInputType(options.$el, "month")
90-
isWeek = $dom.isInputType(options.$el, "week")
87+
isDate = $dom.isType(options.$el, "date")
88+
isTime = $dom.isType(options.$el, "time")
89+
isMonth = $dom.isType(options.$el, "month")
90+
isWeek = $dom.isType(options.$el, "week")
9191
hasTabIndex = $dom.isSelector(options.$el, "[tabindex]")
9292

9393
## TODO: tabindex can't be -1
@@ -161,8 +161,8 @@ module.exports = (Commands, Cypress, cy, state, config) ->
161161
getDefaultButtons = (form) ->
162162
form.find("input, button").filter (__, el) ->
163163
$el = $(el)
164-
($dom.isSelector($el, "input") and $dom.isInputType($el, "submit")) or
165-
($dom.isSelector($el, "button") and not $dom.isInputType($el, "button"))
164+
($dom.isSelector($el, "input") and $dom.isType($el, "submit")) or
165+
($dom.isSelector($el, "button") and not $dom.isType($el, "button"))
166166

167167
type = ->
168168
simulateSubmitHandler = ->

packages/driver/src/cypress/keyboard.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ $Keyboard = {
403403
@ensureKey el, key, options, ->
404404

405405
isDigit = isSingleDigitRe.test(key)
406-
isNumberInputType = $elements.isInput(el) and $elements.isInputType(el, 'number')
406+
isNumberInputType = $elements.isInput(el) and $elements.isType(el, 'number')
407407

408408
if isNumberInputType
409409
selectionStart = el.selectionStart

packages/driver/src/dom/elements.coffee

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ _getValue = ->
2727
descriptor("HTMLTextAreaElement", "value").get
2828
when isSelect(this)
2929
descriptor("HTMLSelectElement", "value").get
30+
when isButton(this)
31+
descriptor("HTMLButtonElement", "value").get
3032
else
3133
## is an option element
3234
descriptor("HTMLOptionElement", "value").get
@@ -39,6 +41,8 @@ _setValue = ->
3941
descriptor("HTMLTextAreaElement", "value").set
4042
when isSelect(this)
4143
descriptor("HTMLSelectElement", "value").set
44+
when isButton(this)
45+
descriptor("HTMLButtonElement", "value").set
4246
else
4347
## is an options element
4448
descriptor("HTMLOptionElement", "value").set
@@ -98,19 +102,34 @@ _isContentEditable = ->
98102
else
99103
descriptor("HTMLElement", "isContentEditable").get
100104

105+
_setType = ->
106+
switch
107+
when isInput(this)
108+
descriptor("HTMLInputElement", "type").set
109+
when isButton(this)
110+
descriptor("HTMLButtonElement", "type").set
111+
112+
113+
_getType = ->
114+
switch
115+
when isInput(this)
116+
descriptor("HTMLInputElement", "type").get
117+
when isButton(this)
118+
descriptor("HTMLButtonElement", "type").get
119+
101120
nativeGetters = {
102121
value: _getValue
103122
selectionStart: descriptor("HTMLInputElement", "selectionStart").get
104123
isContentEditable: _isContentEditable
105124
isCollapsed: descriptor("Selection", 'isCollapsed').get
106125
selectionStart: _getSelectionStart
107126
selectionEnd: _getSelectionEnd
108-
type: descriptor("HTMLInputElement", "type").get
127+
type: _getType
109128
}
110129

111130
nativeSetters = {
112131
value: _setValue
113-
type: descriptor("HTMLInputElement", "type").set
132+
type: _setType
114133
}
115134

116135
nativeMethods = {
@@ -199,6 +218,9 @@ isTextarea = (el) ->
199218
isInput = (el) ->
200219
getTagName(el) is 'input'
201220

221+
isButton = (el) ->
222+
getTagName(el) is 'button'
223+
202224
isSelect = (el) ->
203225
getTagName(el) is 'select'
204226

@@ -223,11 +245,11 @@ isElement = (obj) ->
223245
isFocusable = ($el) ->
224246
$el.is(focusable)
225247

226-
isInputType = ($el, type) ->
248+
isType = ($el, type) ->
227249
el = [].concat($jquery.unwrap($el))[0]
228250
## NOTE: use DOMElement.type instead of getAttribute('type') since
229251
## <input type="asdf"> will have type="text", and behaves like text type
230-
isInput(el) && (getNativeProp(el, 'type') or "").toLowerCase() is type
252+
(getNativeProp(el, 'type') or "").toLowerCase() is type
231253

232254
isScrollOrAuto = (prop) ->
233255
prop is "scroll" or prop is "auto"
@@ -287,7 +309,7 @@ isSame = ($el1, $el2) ->
287309

288310
isTextLike = ($el) ->
289311
sel = (selector) -> isSelector($el, selector)
290-
type = (type) -> isInputType($el, type)
312+
type = (type) -> isType($el, type)
291313

292314
isContentEditableElement = isContentEditable($el.get(0))
293315

@@ -544,7 +566,7 @@ module.exports = {
544566

545567
isTextarea
546568

547-
isInputType
569+
isType
548570

549571
isNeedSingleValueChangeInputElement
550572

packages/driver/src/dom/index.coffee

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
$utils = require("../cypress/utils")
21
$jquery = require("./jquery")
32
$window = require("./window")
43
$document = require("./document")
@@ -10,7 +9,7 @@ $coordinates = require("./coordinates")
109
{ isDocument } = $document
1110
{ wrap, unwrap, isJquery } = $jquery
1211
{ isVisible, isHidden, getReasonIsHidden } = $visibility
13-
{ isInputType, isFocusable, isElement, isScrollable, stringify, getElements, getContainsSelector, getFirstDeepestElement, isDetached, isAttached, isTextLike, isSelector, isDescendent, getFirstFixedOrStickyPositionParent, getFirstStickyPositionParent, getFirstScrollableParent } = $elements
12+
{ isType, isFocusable, isElement, isScrollable, stringify, getElements, getContainsSelector, getFirstDeepestElement, isDetached, isAttached, isTextLike, isSelector, isDescendent, getFirstFixedOrStickyPositionParent, getFirstStickyPositionParent, getFirstScrollableParent } = $elements
1413
{ getCoordsByPosition, getElementPositioning, getElementCoordinatesByPosition, getElementAtPointFromViewport, getElementCoordinatesByPositionRelativeToXY } = $coordinates
1514

1615
isDom = (obj) ->
@@ -28,11 +27,7 @@ module.exports = {
2827

2928
isDom
3029

31-
isInputType
32-
33-
isType: (args...) ->
34-
$utils.warning('Cypress.dom.isType is deprecated. Use Cypress.dom.isInputType instead.')
35-
isInputType(args...)
30+
isType
3631

3732
isVisible
3833

packages/driver/src/dom/selection.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ _moveCursorUpOrDown = (el, up) ->
266266
## on an input, instead of moving the cursor
267267
## we want to perform the native browser action
268268
## which is to increment the step/interval
269-
if $elements.isInputType(el, "number")
269+
if $elements.isType(el, "number")
270270
if up then el.stepUp?() else el.stepDown?()
271271
return
272272

packages/driver/test/cypress/fixtures/dom.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
<form id="multiple-inputs-and-button-submit">
245245
<input name="fname" />
246246
<input name="lname" />
247+
<button type="button">button</button>
247248
<button type="submit">submit me</button>
248249
</form>
249250

packages/driver/test/cypress/integration/commands/actions/click_spec.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ describe "src/cy/commands/actions/click", ->
11051105
num = cy.$$("button").length
11061106

11071107
cy.on "fail", (err) ->
1108-
expect(err.message).to.eq "cy.click() can only be called on a single element. Your subject contained 14 elements. Pass { multiple: true } if you want to serially click each element."
1108+
expect(err.message).to.eq "cy.click() can only be called on a single element. Your subject contained 15 elements. Pass { multiple: true } if you want to serially click each element."
11091109
done()
11101110

11111111
cy.get("button").click()

packages/driver/test/cypress/integration/commands/actions/trigger_spec.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ describe "src/cy/commands/actions/trigger", ->
602602

603603
it "throws when eventName is not a string", ->
604604
cy.on "fail", (err) ->
605-
expect(err.message).to.eq "cy.trigger() can only be called on a single element. Your subject contained 14 elements."
605+
expect(err.message).to.eq "cy.trigger() can only be called on a single element. Your subject contained 15 elements."
606606
done()
607607

608608
cy.get("button:first").trigger("cy.trigger() must be passed a non-empty string as its 1st argument. You passed: 'undefined'.")

packages/driver/test/cypress/integration/cypress/cypress_spec.coffee

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
$ = Cypress.$
22
Promise = Cypress.Promise
3-
$utils = Cypress.utils
4-
53

64
describe "driver/src/cypress/index", ->
75
beforeEach ->
@@ -39,10 +37,3 @@ describe "driver/src/cypress/index", ->
3937
Cypress.Log.command({})
4038

4139
expect(fn).to.throw(/has been renamed to Cypress.log/)
42-
43-
context "Deprecated", ->
44-
it "warns with deprecated Cypress.dom.isType", ->
45-
warnSpy = cy.spy($utils, "warning")
46-
isText = Cypress.dom.isType($('<input type="asdf"/>'), 'text')
47-
expect(isText).to.eq true
48-
expect(warnSpy.called).to.eq true

0 commit comments

Comments
 (0)