forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overrides.ts
74 lines (59 loc) · 2.51 KB
/
overrides.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
import _ from 'lodash'
import { registerFetch } from 'unfetch'
import $selection from '../dom/selection'
import type { ICypress } from '../cypress'
import type { StateFunc } from '../cypress/state'
import type { IFocused } from './focused'
import type { ISnapshots } from './snapshots'
export const create = (state: StateFunc, config: ICypress['config'], focused: IFocused, snapshots: ISnapshots) => {
const wrapNativeMethods = function (contentWindow) {
try {
// return null to trick contentWindow into thinking
// its not been iframed if modifyObstructiveCode is true
if (config('modifyObstructiveCode')) {
Object.defineProperty(contentWindow, 'frameElement', {
get () {
return null
},
})
}
contentWindow.HTMLElement.prototype.focus = function (focusOption) {
return focused.interceptFocus(this, contentWindow, focusOption)
}
contentWindow.HTMLElement.prototype.blur = function () {
return focused.interceptBlur(this)
}
contentWindow.SVGElement.prototype.focus = function (focusOption) {
return focused.interceptFocus(this, contentWindow, focusOption)
}
contentWindow.SVGElement.prototype.blur = function () {
return focused.interceptBlur(this)
}
contentWindow.HTMLInputElement.prototype.select = function () {
return $selection.interceptSelect.call(this)
}
contentWindow.document.hasFocus = function () {
return focused.documentHasFocus.call(this)
}
const cssModificationSpy = function (original, ...args) {
snapshots.onCssModified(this.href)
return original.apply(this, args)
}
const { insertRule } = contentWindow.CSSStyleSheet.prototype
const { deleteRule } = contentWindow.CSSStyleSheet.prototype
contentWindow.CSSStyleSheet.prototype.insertRule = _.wrap(insertRule, cssModificationSpy)
contentWindow.CSSStyleSheet.prototype.deleteRule = _.wrap(deleteRule, cssModificationSpy)
if (config('experimentalFetchPolyfill')) {
// drop "fetch" polyfill that replaces it with XMLHttpRequest
// from the app iframe that we wrap for network stubbing
contentWindow.fetch = registerFetch(contentWindow)
// flag the polyfill to test this experimental feature easier
state('fetchPolyfilled', true)
}
} catch (error) {} // eslint-disable-line no-empty
}
return {
wrapNativeMethods,
}
}
export interface IOverrides extends ReturnType<typeof create> {}