-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
coordinates_spec.ts
314 lines (243 loc) · 9.15 KB
/
coordinates_spec.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import $coordinates from '../../../src/dom/coordinates'
import $elements from '../../../src/dom/elements'
const { $ } = Cypress
export {}
describe('src/dom/coordinates', () => {
let doc: Document
before(() => {
return cy
.visit('/fixtures/generic.html')
.then(function (win) {
this.body = win.document.body.outerHTML
})
})
beforeEach(function () {
doc = cy.state('document')
$(doc.body).empty().html(this.body)
this.$button = $('<button style=\'position: absolute; top: 25px; left: 50px; width: 100px; line-height: 50px; padding: 10px; margin: 10px; border: 10px solid black\'>foo</button>')
.appendTo(cy.$$('body'))
})
// this is necessary so that document.elementFromPoint
// does not miss elements
context('.getElementPositioning', () => {
it('returns the leftCenter and topCenter normalized', function () {
const win = Cypress.dom.getWindowByElement(this.$button.get(0))
const scrollY = Object.getOwnPropertyDescriptor(win, 'scrollY')!
const scrollX = Object.getOwnPropertyDescriptor(win, 'scrollX')!
Object.defineProperty(win, 'scrollY', {
value: 10,
})
Object.defineProperty(win, 'scrollX', {
value: 20,
})
cy.stub(this.$button.get(0), 'getClientRects').returns([{
top: 100.9,
left: 60.9,
width: 50,
height: 40,
}])
const { fromElViewport, fromElWindow } = Cypress.dom.getElementPositioning(this.$button)
expect(fromElViewport.topCenter).to.eq(120)
expect(fromElViewport.leftCenter).to.eq(85)
expect(fromElWindow.topCenter).to.eq(130)
expect(fromElWindow.leftCenter).to.eq(105)
Object.defineProperty(win, 'scrollY', scrollY)
Object.defineProperty(win, 'scrollX', scrollX)
})
it('accepts a native element as an argument', function () {
const setupGlobalOverrides = () => {
const win = Cypress.dom.getWindowByElement(this.$button.get(0))
const scrollY = Object.getOwnPropertyDescriptor(win, 'scrollY')!
const scrollX = Object.getOwnPropertyDescriptor(win, 'scrollX')!
Object.defineProperty(win, 'scrollY', {
value: 10,
})
Object.defineProperty(win, 'scrollX', {
value: 20,
})
return () => {
Object.defineProperty(win, 'scrollY', scrollY)
Object.defineProperty(win, 'scrollX', scrollX)
}
}
const teardownGlobalOverrides = setupGlobalOverrides()
try {
cy.stub(this.$button.get(0), 'getClientRects').returns([{
top: 10,
left: 10,
width: 100,
height: 100,
}])
const { fromElViewport } = Cypress.dom.getElementPositioning(this.$button.get(0))
expect(fromElViewport.top).to.eq(10)
expect(fromElViewport.left).to.eq(10)
} finally {
teardownGlobalOverrides()
}
})
})
context('.getCoordsByPosition', () => {
it('rounds down x and y values to object', () => {
expect(Cypress.dom.getCoordsByPosition(5.96, 7.68)).to.deep.eq({ x: 5, y: 7 })
})
})
context('.getElementAtPointFromViewport', () => {
it('returns same element based on x/y coords', function () {
expect(Cypress.dom.getElementAtPointFromViewport(doc, 100, 60)).to.eq(this.$button.get(0))
})
it('does not return if element is hidden', function () {
this.$button.hide()
expect(Cypress.dom.getElementAtPointFromViewport(doc, 100, 60)).not.to.eq(this.$button.get(0))
})
it('returns null if no element was found', function () {
expect(Cypress.dom.getElementAtPointFromViewport(doc, 1e9, 1e9)).to.be.null
})
})
context('.getElementCoordinatesByPosition', () => {
beforeEach(function () {
this.fromElWindowPos = (pos) => {
return Cypress.dom.getElementCoordinatesByPosition(this.$button, pos)
.fromElWindow
}
})
describe('topLeft', () => {
it('returns top left x/y including padding + border', function () {
const obj = this.fromElWindowPos('topLeft')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(60)
expect(obj.y).to.eq(35)
})
})
describe('top', () => {
it('returns top center x/y including padding + border', function () {
const obj = this.fromElWindowPos('top')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(110)
expect(obj.y).to.eq(35)
})
})
describe('topRight', () => {
it('returns top right x/y including padding + border', function () {
const obj = this.fromElWindowPos('topRight')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(159)
expect(obj.y).to.eq(35)
})
})
describe('left', () => {
it('returns center left x/y including padding + border', function () {
const obj = this.fromElWindowPos('left')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(60)
expect(obj.y).to.eq(80)
})
})
describe('center', () => {
it('returns center x/y including padding + border', function () {
const obj = this.fromElWindowPos()
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(110)
expect(obj.y).to.eq(80)
})
it('returns width / height factoring in rotation transforms', function () {
// normally our outerWidth is 100 and outerHeight is 70
// after we've been rotated these are reversed and our previous
// calculation would be wrong. using getBoundingClientRect passes this test
this.$button.css({ transform: 'rotate(90deg)' })
const obj = this.fromElWindowPos()
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(110)
expect(obj.y).to.eq(80)
})
})
describe('right', () => {
it('returns center right x/y including padding + border', function () {
const obj = this.fromElWindowPos('right')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(159)
expect(obj.y).to.eq(80)
})
})
describe('bottomLeft', () => {
it('returns bottom left x/y including padding + border', function () {
const obj = this.fromElWindowPos('bottomLeft')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(60)
expect(obj.y).to.eq(124)
})
})
context('bottom', () => {
it('returns bottom center x/y including padding + border', function () {
const obj = this.fromElWindowPos('bottom')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(110)
expect(obj.y).to.eq(124)
})
})
context('bottomRight', () => {
it('returns bottom right x/y including padding + border', function () {
const obj = this.fromElWindowPos('bottomRight')
// padding is added to the line-height but width includes the padding
expect(obj.x).to.eq(159)
expect(obj.y).to.eq(124)
})
})
})
context('isAUTFrame', () => {
const { isAUTFrame } = $coordinates
// our test for a window is that it has a `window` that refers
// to itself
const getWindowLikeObject = () => {
const win = { parent: {} as any }
win.parent.window = win.parent
return win
}
it('returns true if parent is a window and not an iframe', () => {
const win = getWindowLikeObject()
expect(isAUTFrame(win)).to.be.true
})
it('returns true if parent is a window and getting its frameElement property throws an error', () => {
const win = getWindowLikeObject()
cy.stub($elements, 'getNativeProp').throws('cross-origin error')
expect(isAUTFrame(win)).to.be.true
})
it('returns false if parent is not a window', () => {
const win = { parent: {} }
expect(isAUTFrame(win)).to.be.false
})
it('returns false if parent is an iframe', () => {
const win = getWindowLikeObject()
cy.stub($elements, 'getNativeProp').returns(true)
expect(isAUTFrame(win)).to.be.false
})
})
context('span spanning multiple lines', () => {
it('gets first dom rect in multiline text', () => {
$(`\
<div style="width:150px; margin-top:100px"> \
this is some long text with a single <span id="multiple" style="color:darkblue"> span that spans lines</span> making it tricky to click \
</div>\
`).appendTo($('body'))
const $el = cy.$$('#multiple')
const el = $el[0]
cy.stub(el, 'getClientRects', () => {
return [
{
top: 100,
left: 100,
width: 50,
height: 40,
},
{
top: 200,
left: 50,
width: 10,
height: 10,
},
]
}).as('getClientRects')
const obj = Cypress.dom.getElementCoordinatesByPosition($el, 'center').fromElViewport
expect({ x: obj.x, y: obj.y }).to.deep.eq({ x: 125, y: 120 })
})
})
})