-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
choose-a-browser.cy.ts
351 lines (252 loc) · 11.2 KB
/
choose-a-browser.cy.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import type { FoundBrowser } from '@packages/types/src'
describe('Choose a Browser Page', () => {
beforeEach(() => {
cy.scaffoldProject('launchpad')
})
describe('System Browsers Detected', () => {
beforeEach(() => {
cy.findBrowsers({
filter: (browser) => {
return Cypress._.includes(['chrome', 'firefox', 'electron', 'edge'], browser.name) && browser.channel === 'stable'
},
})
})
it('preselects browser that is provided through the command line', () => {
cy.withCtx((ctx, o) => {
// stub launching project since we have `--browser --testingType --project` here
o.sinon.stub(ctx._apis.projectApi, 'launchProject').resolves()
})
cy.openProject('launchpad', ['--e2e', '--browser', 'edge'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.findByRole('radio', { name: 'Edge v8', checked: true })
cy.percySnapshot()
cy.withRetryableCtx((ctx, o) => {
expect(ctx._apis.projectApi.launchProject).to.be.calledOnce
})
})
it('shows warning when launched with --browser name that cannot be matched to found browsers', () => {
cy.openProject('launchpad', ['--e2e', '--browser', 'doesNotExist'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.get('[data-cy="alert-header"]').should('contain', 'Warning: Browser Not Found')
cy.get('[data-cy="alert-body"]')
.should('contain', 'Browser: doesNotExist was not found on your system or is not supported by Cypress.')
cy.get('[data-cy="alert-body"]').within(() => {
cy.validateExternalLink({
href: 'https://on.cypress.io/customize-browsers',
})
})
// Ensure warning can be dismissed
cy.get('[data-cy="alert-suffix-icon"]').click()
cy.get('[data-cy="alert-header"]').should('not.exist')
})
it('shows warning when launched with --browser path option that cannot be matched to found browsers', () => {
const path = '/path/does/not/exist'
cy.openProject('launchpad', ['--e2e', '--browser', path])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.get('[data-cy="alert-header"]').should('contain', 'Warning: Browser Not Found')
cy.get('[data-cy="alert-body"]').as('AlertBody')
.should('contain', `We could not identify a known browser at the path you provided: ${path}`)
.validateExternalLink({
href: 'https://on.cypress.io/troubleshooting-launching-browsers',
})
// The exception thrown is presented in the alert's second code element and
// varies depending on platform
if (Cypress.platform === 'win32') {
cy.get('@AlertBody').find('code').eq(1).should('have.text', `win-version-info is unable to access file: \\${path.replaceAll('/', '\\')}`)
} else {
cy.get('@AlertBody').find('code').eq(1).should('have.text', `spawn ${path} ENOENT`)
}
cy.percySnapshot()
// Ensure warning can be dismissed
cy.get('[data-cy="alert-suffix-icon"]').click()
cy.get('[data-cy="alert-header"]').should('not.exist')
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.get('[data-cy="alert-header"]').should('not.exist')
})
it('shows installed browsers with their relevant properties', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.findByRole('radio', { name: 'Chrome v1' })
cy.findByRole('radio', { name: 'Firefox v5' })
cy.findByRole('radio', { name: 'Electron v12' })
cy.findByRole('radio', { name: 'Edge v8' })
})
it('performs mutation to launch selected browser when launch button is pressed', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.contains('button', 'Start E2E Testing in Chrome').as('launchButton')
// Stub out response to prevent browser launch but not break internals
cy.intercept('mutation-OpenBrowser_LaunchProject', {
body: {
data: {
launchOpenProject: true,
setProjectPreferences: {
currentProject: {
id: 'test-id',
title: 'launchpad',
__typename: 'CurrentProject',
},
__typename: 'Query',
},
},
},
delay: 500,
}).as('launchProject')
cy.get('@launchButton').click()
cy.wait('@launchProject').then(({ request }) => {
expect(request?.body.variables.testingType).to.eq('e2e')
})
cy.withCtx((ctx) => {
ctx.browser.setBrowserStatus('opening')
})
cy.contains('button', 'Opening E2E Testing in Chrome')
cy.withCtx((ctx) => {
ctx.browser.setBrowserStatus('open')
})
cy.contains('button', 'Running Chrome')
cy.withCtx((ctx) => {
ctx.browser.setBrowserStatus('closed')
})
cy.contains('button', 'Start E2E Testing in Chrome')
})
it('performs mutation to change selected browser when browser item is clicked', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.findByRole('radio', { name: 'Chrome v1', checked: true }).as('chromeItem')
cy.findByRole('radio', { name: 'Firefox v5', checked: false }).as('firefoxItem')
cy.contains('button', 'Start E2E Testing in Chrome').should('be.visible')
cy.intercept('mutation-OpenBrowserList_SetBrowser').as('setBrowser')
cy.get('@firefoxItem').click().find('label').then(($label) => {
cy.wait('@setBrowser').its('request.body.variables.id').should('eq', $label.attr('for'))
})
cy.findByRole('radio', { name: 'Chrome v1', checked: false })
cy.findByRole('radio', { name: 'Firefox v5', checked: true })
cy.contains('button', 'Start E2E Testing in Firefox').should('be.visible')
})
it('prevents calling launchProject multiple times', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.withCtx((ctx, o) => {
o.sinon.stub(ctx.actions.project, 'launchProject')
})
cy.intercept('mutation-OpenBrowser_LaunchProject', cy.stub().as('launchProject'))
cy.contains('button', 'Start E2E Testing in Chrome').dblclick()
cy.get('@launchProject').its('callCount').should('equal', 1)
})
it('performs mutation to close browser', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.withCtx((ctx) => {
ctx.browser.setBrowserStatus('open')
})
cy.contains('button', 'Running Chrome')
cy.intercept('mutation-OpenBrowser_CloseBrowser').as('closeBrowser')
cy.contains('button', 'Close').click()
cy.wait('@closeBrowser')
})
it('performs mutation to focus open browser when focus button is pressed', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.withCtx((ctx) => {
ctx.browser.setBrowserStatus('open')
})
cy.get('h1').should('contain', 'Choose a Browser')
cy.contains('button', 'Running Chrome').as('launchButton')
cy.contains('button', 'Focus').as('focusButton')
cy.intercept('mutation-OpenBrowser_FocusActiveBrowserWindow').as('focusBrowser')
cy.withCtx((ctx, o) => {
o.sinon.stub(ctx.actions.browser, 'focusActiveBrowserWindow')
})
cy.get('@focusButton').click()
cy.wait('@focusBrowser').then(() => {
cy.withCtx((ctx) => {
expect(ctx.actions.browser.focusActiveBrowserWindow).to.be.called
})
})
})
it('should launch project if relaunchBrowser is set', () => {
cy.intercept('query-OpenBrowser', (req) => {
req.on('before:response', (res) => {
res.body.data.currentProject.browserStatus = 'open'
})
})
cy.openProject('launchpad', ['--e2e'])
cy.withCtx((ctx, o) => {
ctx.project.setRelaunchBrowser(true)
o.sinon.stub(ctx.actions.project, 'launchProject')
})
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.withCtx((ctx) => {
expect(ctx.actions.project.launchProject).to.have.been.called
})
})
it('subscribes to changes to browserStatus/activeBrowser through the browserStatusUpdated subscription', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.findByRole('radio', { name: 'Chrome v1', checked: true }).as('chromeItem')
cy.withCtx((ctx, o) => {
o.sinon.stub(ctx.actions.project, 'launchProject')
})
cy.contains('button', 'Start E2E Testing in Chrome').should('be.visible').click()
cy.withCtx((ctx, o) => {
ctx.browser.setBrowserStatus('open')
})
cy.contains('button', 'Running Chrome')
// Updating active browser in conjunction with the browser status to ensure that changes to
// both are reflected in the UI.
cy.withCtx(async (ctx) => {
await ctx.actions.browser.setActiveBrowser(ctx.lifecycleManager.browsers!.find((browser) => browser.name === 'firefox') as FoundBrowser)
ctx.browser.setBrowserStatus('closed')
})
cy.contains('button', 'Start E2E Testing in Firefox').should('be.visible')
cy.findByRole('radio', { name: 'Firefox v5', checked: true }).should('be.visible')
})
it('should return to welcome screen if user modifies the config file to not include the current testing type and recover', () => {
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.withCtx(async (ctx) => {
await ctx.actions.file.writeFileInProject('cypress.config.js', 'module.exports = {}')
})
cy.get('h1').should('contain', 'Welcome to Cypress!')
cy.contains('[data-cy-testingtype="e2e"]', 'Not Configured')
cy.withCtx(async (ctx) => {
await ctx.actions.file.writeFileInProject('cypress.config.js',
`module.exports = {
e2e: {
setupNodeEvents: (on, config) => config,
supportFile: false,
},
}`)
})
cy.get('h1').should('contain', 'Welcome to Cypress!')
cy.get('[data-cy-testingtype="e2e"]').should('not.contain', 'Not Configured')
})
})
describe('No System Browsers Detected', () => {
it('shows single electron browser option when no system browsers are detected and process is running in electron', () => {
cy.log('This test simulates the Electron browser injection performed by server.getBrowsers')
cy.findBrowsers({
filter: (browser) => {
return browser.name === 'electron'
},
})
cy.openProject('launchpad', ['--e2e'])
cy.visitLaunchpad()
cy.get('h1').should('contain', 'Choose a Browser')
cy.get('[data-cy="open-browser-list"]').children().should('have.length', 1)
cy.findByRole('radio', { name: 'Electron v12', checked: true })
cy.percySnapshot()
})
})
})