forked from DevExpress/testcafe
/
click-command.js
159 lines (119 loc) · 5.38 KB
/
click-command.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import hammerhead from '../../deps/hammerhead';
import testCafeCore from '../../deps/testcafe-core';
import testCafeUI from '../../deps/testcafe-ui';
import { focusByRelatedElement, getElementBoundToLabel } from '../../utils/utils';
const browserUtils = hammerhead.utils.browser;
const eventSimulator = hammerhead.eventSandbox.eventSimulator;
const listeners = hammerhead.eventSandbox.listeners;
const nativeMethods = hammerhead.nativeMethods;
const domUtils = testCafeCore.domUtils;
const styleUtils = testCafeCore.styleUtils;
const selectElementUI = testCafeUI.selectElement;
class ElementClickCommand {
constructor (eventState, eventArgs) {
this.eventState = eventState;
this.eventArgs = eventArgs;
}
run () {
if (this.eventState.clickElement)
eventSimulator.click(this.eventState.clickElement, this.eventArgs.options);
if (!domUtils.isElementFocusable(this.eventArgs.element))
focusByRelatedElement(this.eventArgs.element);
}
}
class LabelElementClickCommand extends ElementClickCommand {
constructor (eventState, eventArgs) {
super(eventState, eventArgs);
this.label = this.eventArgs.element;
this.input = getElementBoundToLabel(this.eventArgs.element);
}
run () {
let focusRaised = false;
const ensureFocusRaised = e => {
focusRaised = nativeMethods.eventTargetGetter.call(e) === this.input;
};
listeners.addInternalEventBeforeListener(window, ['focus'], ensureFocusRaised);
super.run();
listeners.removeInternalEventBeforeListener(window, ['focus'], ensureFocusRaised);
if (domUtils.isElementFocusable(this.label) && !focusRaised)
this._ensureBoundElementFocusRaised();
}
_ensureBoundElementFocusRaised () {
eventSimulator.focus(this.input);
}
}
class SelectElementClickCommand extends ElementClickCommand {
constructor (eventState, eventArgs) {
super(eventState, eventArgs);
}
run () {
super.run();
this._toggleSelectOptionList();
}
_toggleSelectOptionList () {
// NOTE: Emulating the click event on the 'select' element doesn't expand the
// dropdown with options (except chrome), therefore we should emulate it.
const element = this.eventArgs.element;
const isSelectWithDropDown = styleUtils.getSelectElementSize(element) === 1;
if (isSelectWithDropDown && this.eventState.simulateDefaultBehavior !== false) {
if (selectElementUI.isOptionListExpanded(element))
selectElementUI.collapseOptionList();
else
selectElementUI.expandOptionList(element);
}
}
}
class OptionElementClickCommand extends ElementClickCommand {
constructor (eventState, eventArgs) {
super(eventState, eventArgs);
}
run () {
return this.eventArgs.element;
}
}
class LabelledCheckboxElementClickCommand extends LabelElementClickCommand {
constructor (eventState, eventArgs) {
super(eventState, eventArgs);
this.checkbox = this.input;
this.shouldPreventCheckedChangeInChrome = this._isClickableElementInsideLabel(eventArgs.element);
}
run () {
let changed = false;
const onChange = () => {
changed = true;
};
listeners.addInternalEventBeforeListener(window, ['change'], onChange);
super.run();
listeners.removeInternalEventBeforeListener(window, ['change'], onChange);
//NOTE: Two overlapping issues: https://github.com/DevExpress/testcafe/issues/3348 and https://github.com/DevExpress/testcafe/issues/6949
//When label contains <a href=any> or <button> element, clicking these elements will prevent checkbox from changing checked state.
//We should to leave the code for fixing .focus issue and add additional check for the clickable elements inside the label:
if (browserUtils.isChrome && !changed && !this.shouldPreventCheckedChangeInChrome)
this._ensureCheckboxStateChanged();
}
_ensureCheckboxStateChanged () {
this.checkbox.checked = !this.checkbox.checked;
eventSimulator.change(this.checkbox);
}
_isClickableElementInsideLabel (element) {
const isClickableLink = element.tagName === 'A' && element.getAttribute('href');
const isButton = element.tagName === 'BUTTON';
return isClickableLink || isButton;
}
}
export default function (eventState, eventArgs) {
const elementBoundToLabel = getElementBoundToLabel(eventArgs.element);
const isSelectElement = domUtils.isSelectElement(eventArgs.element);
const isOptionElement = domUtils.isOptionElement(eventArgs.element);
const isLabelElement = domUtils.isLabelElement(eventArgs.element) && elementBoundToLabel;
const isLabelledCheckbox = elementBoundToLabel && domUtils.isCheckboxElement(elementBoundToLabel);
if (isSelectElement)
return new SelectElementClickCommand(eventState, eventArgs);
if (isOptionElement)
return new OptionElementClickCommand(eventState, eventArgs);
if (isLabelledCheckbox)
return new LabelledCheckboxElementClickCommand(eventState, eventArgs);
if (isLabelElement)
return new LabelElementClickCommand(eventState, eventArgs);
return new ElementClickCommand(eventState, eventArgs);
}