-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathopen_project.ts
334 lines (260 loc) · 8.62 KB
/
open_project.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import _ from 'lodash'
import la from 'lazy-ass'
import Debug from 'debug'
import Bluebird from 'bluebird'
import assert from 'assert'
import { ProjectBase } from './project-base'
import browsers from './browsers'
import * as errors from './errors'
import preprocessor from './plugins/preprocessor'
import runEvents from './plugins/run_events'
import * as session from './session'
import { cookieJar } from './cookie-jar'
import { getSpecUrl } from './project_utils'
import type { LaunchOpts, OpenProjectLaunchOptions, InitializeProjectOptions } from '@packages/types'
import { DataContext, getCtx } from '@packages/data-context'
import { autoBindDebug } from '@packages/data-context/src/util'
const debug = Debug('cypress:server:open_project')
export class OpenProject {
projectBase: ProjectBase<any> | null = null
relaunchBrowser: ((...args: unknown[]) => Bluebird<void>) | null = null
constructor () {
return autoBindDebug(this)
}
resetOpenProject () {
this.projectBase?.__reset()
this.projectBase = null
this.relaunchBrowser = null
}
reset () {
this.resetOpenProject()
}
getConfig () {
return this.projectBase?.getConfig()
}
getRemoteStates () {
return this.projectBase?.remoteStates
}
getProject () {
return this.projectBase
}
async launch (browser, spec: Cypress.Cypress['spec'], options: LaunchOpts = {
onError: () => undefined,
}) {
this._ctx = getCtx()
assert(this.projectBase, 'Cannot launch runner if projectBase is undefined!')
debug('resetting project state, preparing to launch browser %s for spec %o options %o',
browser.name, spec, options)
la(_.isPlainObject(browser), 'expected browser object:', browser)
// reset to reset server and socket state because
// of potential domain changes, request buffers, etc
this.projectBase!.reset()
let url = getSpecUrl({
spec,
browserUrl: this.projectBase.cfg.browserUrl,
projectRoot: this.projectBase.projectRoot,
})
debug('open project url %s', url)
const cfg = this.projectBase.getConfig()
_.defaults(options, {
browsers: cfg.browsers,
userAgent: cfg.userAgent,
proxyUrl: cfg.proxyUrl,
proxyServer: cfg.proxyServer,
socketIoRoute: cfg.socketIoRoute,
chromeWebSecurity: cfg.chromeWebSecurity,
isTextTerminal: cfg.isTextTerminal,
downloadsFolder: cfg.downloadsFolder,
experimentalSessionAndOrigin: cfg.experimentalSessionAndOrigin,
})
// if we don't have the isHeaded property
// then we're in interactive mode and we
// can assume its a headed browser
// TODO: we should clean this up
if (!_.has(browser, 'isHeaded')) {
browser.isHeaded = true
browser.isHeadless = false
}
// set the current browser object on options
// so we can pass it down
options.browser = browser
if (!process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF) {
options.url = url
}
this.projectBase.setCurrentSpecAndBrowser(spec, browser)
const automation = this.projectBase.getAutomation()
// use automation middleware if its
// been defined here
let am = options.automationMiddleware
if (am) {
automation.use(am)
}
if (!am || !am.onBeforeRequest) {
automation.use({
onBeforeRequest (message, data) {
if (message === 'take:screenshot') {
data.specName = spec.name
return data
}
},
})
}
const afterSpec = () => {
if (!this.projectBase || cfg.isTextTerminal || !cfg.experimentalInteractiveRunEvents) {
return Bluebird.resolve()
}
return runEvents.execute('after:spec', cfg, spec)
}
const { onBrowserClose } = options
options.onBrowserClose = () => {
if (spec && spec.absolute) {
preprocessor.removeFile(spec.absolute, cfg)
}
afterSpec()
.catch((err) => {
this.projectBase?.options.onError?.(err)
})
if (onBrowserClose) {
return onBrowserClose()
}
}
options.onError = this.projectBase.options.onError
this.relaunchBrowser = () => {
debug(
'launching browser: %o, spec: %s',
browser,
spec.relative,
)
return Bluebird.try(() => {
if (!cfg.isTextTerminal && cfg.experimentalInteractiveRunEvents) {
return runEvents.execute('before:spec', cfg, spec)
}
// clear cookies and all session data before each spec
cookieJar.removeAllCookies()
session.clearSessions()
})
.then(() => {
// TODO: Stub this so we can detect it being called
if (process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF) {
return browsers.connectToExisting(browser, options, automation)
}
if (options.shouldLaunchNewTab) {
const onInitializeNewBrowserTab = async () => {
await this.resetBrowserState()
}
// If we do not launch the browser,
// we tell it that we are ready
// to receive the next spec
return browsers.connectToNewSpec(browser, { onInitializeNewBrowserTab, ...options }, automation)
}
return browsers.open(browser, options, automation, this._ctx)
})
}
return this.relaunchBrowser()
}
closeBrowser () {
return browsers.close()
}
async resetBrowserTabsForNextTest (shouldKeepTabOpen: boolean) {
return this.projectBase?.resetBrowserTabsForNextTest(shouldKeepTabOpen)
}
async resetBrowserState () {
return this.projectBase?.resetBrowserState()
}
closeOpenProjectAndBrowsers () {
this.projectBase?.close().catch((e) => {
this._ctx?.logTraceError(e)
})
this.resetOpenProject()
return this.closeBrowser()
}
close () {
debug('closing opened project')
this.closeOpenProjectAndBrowsers()
}
changeUrlToSpec (browserUrl: string, spec: Cypress.Spec) {
if (!this.projectBase) {
return
}
const newSpecUrl = getSpecUrl({
projectRoot: this.projectBase.projectRoot,
spec,
// absoluteSpecPath: spec.absolute,
// specType: spec.specType,
// browserUrl: this.openProject.cfg.browserUrl,
// integrationFolder: this.openProject.cfg.integrationFolder || 'integration',
// componentFolder: this.openProject.cfg.componentFolder || 'component',
// projectRoot: this.openProject.projectRoot,
})
console.log(`New url is ${newSpecUrl}`)
this.projectBase.server._socket.changeToUrl(newSpecUrl)
// this.openProject.changeToUrl(newSpecUrl)
}
// close existing open project if it exists, for example
// if you are switching from CT to E2E or vice versa.
// used by launchpad
async closeActiveProject () {
await this.closeOpenProjectAndBrowsers()
}
_ctx?: DataContext
async create (path: string, args: InitializeProjectOptions, options: OpenProjectLaunchOptions) {
this._ctx = getCtx()
debug('open_project create %s', path)
_.defaults(options, {
onReloadBrowser: () => {
if (this.relaunchBrowser) {
return this.relaunchBrowser()
}
return
},
})
if (!_.isUndefined(args.configFile) && !_.isNull(args.configFile)) {
options.configFile = args.configFile
}
options = _.extend({}, args.config, options, { args })
// open the project and return
// the config for the project instance
debug('opening project %s', path)
debug('and options %o', options)
assert(args.testingType)
const testingType = args.testingType === 'component' ? 'component' : 'e2e'
this._ctx.lifecycleManager.runModeExitEarly = options.onError ?? undefined
// store the currently open project
this.projectBase = new ProjectBase({
testingType,
projectRoot: path,
options: {
...options,
testingType,
},
})
try {
await this.projectBase.initializeConfig()
await this.projectBase.open()
} catch (err: any) {
if (err.isCypressErr && err.portInUse) {
errors.throwErr(err.type, err.port)
} else {
// rethrow and handle elsewhere
throw (err)
}
}
return this
}
// for testing purposes
__reset () {
this.resetOpenProject()
}
async sendFocusBrowserMessage () {
const isRunnerConnected = this.projectBase?.isRunnerSocketConnected()
// If the runner's socket is active and connected, we focus the active window
if (isRunnerConnected) {
return this.projectBase?.sendFocusBrowserMessage()
}
// Otherwise, we relaunch the app in the current browser
if (this.relaunchBrowser) {
return this.relaunchBrowser()
}
}
}
export const openProject = new OpenProject()