-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
iframes.jsx
218 lines (180 loc) · 6.05 KB
/
iframes.jsx
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
import cs from 'classnames'
import { action, autorun } from 'mobx'
import { observer } from 'mobx-react'
import React, { Component } from 'react'
import $Cypress from '@packages/driver'
import {
SnapshotControls,
ScriptError,
IframeModel,
selectorPlaygroundModel,
AutIframe,
logger,
studioRecorder,
} from '@packages/runner-shared'
import util from '../lib/util'
const $ = $Cypress.$
@observer
export default class Iframes extends Component {
_disposers = []
render () {
const { width, height, scale, marginLeft, headerHeight, scriptError } = this.props.state
return (
<div
className={cs(
'iframes-container',
{
'has-error': !!scriptError,
'studio-is-open': studioRecorder.isOpen,
'studio-is-loading': studioRecorder.isLoading,
'studio-is-ready': studioRecorder.isReady,
'studio-is-failed': studioRecorder.isFailed,
},
)}
style={{
top: headerHeight,
left: this.props.state.absoluteReporterWidth + this.props.state.specListWidth,
}}
>
<div
ref='container'
className='size-container'
style={{
marginLeft,
height,
transform: `scale(${scale})`,
width,
}}
/>
<ScriptError error={scriptError} />
<div className='cover' />
{studioRecorder.isLoading && (
<div
className='studio-loading-cover'
style={{
marginLeft,
height,
transform: `scale(${scale})`,
width,
}}
>
<div><i className='fa fa-spinner fa-spin' /></div>
</div>
)}
</div>
)
}
componentDidMount () {
this.autIframe = new AutIframe(this.props.config)
this.props.eventManager.on('visit:failed', this.autIframe.showVisitFailure)
this.props.eventManager.on('before:screenshot', this.autIframe.beforeScreenshot)
this.props.eventManager.on('after:screenshot', this.autIframe.afterScreenshot)
this.props.eventManager.on('script:error', this._setScriptError)
this.props.eventManager.on('visit:blank', this.autIframe.visitBlank)
this.props.eventManager.on('run:end', this.autIframe.startStudio)
this.props.eventManager.on('page:loading', (isLoading) => {
if (!isLoading) {
this.autIframe.reattachStudio()
}
})
// TODO: need to take headless mode into account
// may need to not display reporter if more than 200 tests
this.props.eventManager.on('restart', () => {
this._run(this.props.config)
})
this.props.eventManager.on('print:selector:elements:to:console', this._printSelectorElementsToConsole)
this._disposers.push(autorun(() => {
this.autIframe.toggleSelectorPlayground(selectorPlaygroundModel.isEnabled)
}))
this._disposers.push(autorun(() => {
this.autIframe.toggleSelectorHighlight(selectorPlaygroundModel.isShowingHighlight)
}))
this.props.eventManager.start(this.props.config)
this.iframeModel = new IframeModel({
state: this.props.state,
restoreDom: this.autIframe.restoreDom,
highlightEl: this.autIframe.highlightEl,
detachDom: this.autIframe.detachDom,
snapshotControls: (snapshotProps) => (
<SnapshotControls
eventManager={this.props.eventManager}
snapshotProps={snapshotProps}
state={this.props.state}
onToggleHighlights={this._toggleSnapshotHighlights}
onStateChange={this._changeSnapshotState}
/>
),
})
this.iframeModel.listen()
this._run(this.props.config)
}
@action _setScriptError = (err) => {
if (err && 'error' in err) {
this.props.state.scriptError = err.error
}
if (!err) {
this.props.state.scriptError = null
}
}
_run = (config) => {
const specPath = util.specPath()
this.props.eventManager.notifyRunningSpec(specPath)
logger.clearLog()
this._setScriptError(null)
this.props.eventManager.setup(config)
const $autIframe = this._loadIframes(specPath)
this.props.eventManager.initialize($autIframe, config)
}
// jQuery is a better fit for managing these iframes, since they need to get
// wiped out and reset on re-runs and the snapshots are from dom we don't control
_loadIframes (specPath) {
const specSrc = `/${this.props.config.namespace}/iframes/${encodeURIComponent(specPath)}`
const $container = $(this.refs.container).empty()
const $autIframe = this.autIframe.create(this.props.config).appendTo($container)
this.autIframe.showInitialBlankContents()
const $specIframe = $('<iframe />', {
id: `Your Spec: '${specSrc}'`,
class: 'spec-iframe',
}).appendTo($container)
$specIframe.prop('src', specSrc)
return $autIframe
}
_toggleSnapshotHighlights = (snapshotProps) => {
this.props.state.snapshot.showingHighlights = !this.props.state.snapshot.showingHighlights
if (this.props.state.snapshot.showingHighlights) {
const snapshot = snapshotProps.snapshots[this.props.state.snapshot.stateIndex]
this.autIframe.highlightEl(snapshot, snapshotProps)
} else {
this.autIframe.removeHighlights()
}
}
_changeSnapshotState = (snapshotProps, index) => {
const snapshot = snapshotProps.snapshots[index]
this.props.state.snapshot.stateIndex = index
this.autIframe.restoreDom(snapshot)
if (this.props.state.snapshot.showingHighlights && snapshotProps.$el) {
this.autIframe.highlightEl(snapshot, snapshotProps)
} else {
this.autIframe.removeHighlights()
}
}
componentDidUpdate () {
const cb = this.props.state.callbackAfterUpdate
if (cb) {
cb()
}
}
_printSelectorElementsToConsole = () => {
this.autIframe.printSelectorElementsToConsole()
}
componentWillUnmount () {
this.props.eventManager.notifyRunningSpec(null)
this.props.eventManager.stop()
this._disposers.forEach((dispose) => {
dispose()
})
}
getSizeContainer () {
return this.refs.container
}
}