-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathcypress_api.spec.ts
254 lines (214 loc) · 8.17 KB
/
cypress_api.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
const { assertLogLength } = require('../../../support/utils')
describe('cy.origin Cypress API', () => {
beforeEach(() => {
cy.visit('/fixtures/primary-origin.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
})
afterEach(() => {
// FIXME: Tests that end with a cy.origin command and enqueue no further cy
// commands may have origin's unload event bleed into subsequent tests
// and prevent stability from being reached, causing those tests to hang.
// We enqueue another cy command after each test to ensure stability
// is reached for the next test. This additional command can be removed with the
// completion of: https://github.com/cypress-io/cypress/issues/21300
cy.then(() => { /* ensuring stability */ })
})
context('Commands', () => {
it('adds a custom command', () => {
cy.origin('http://foobar.com:3500', () => {
// @ts-ignore
Cypress.Commands.add('foo', () => 'bar')
// @ts-ignore
cy.foo().should('equal', 'bar')
})
// persists added command through spec bridge
cy.origin('http://foobar.com:3500', () => {
// @ts-ignore
cy.foo().should('equal', 'bar')
})
})
it('overwrites an existing command in the spec bridge', () => {
cy.origin('http://foobar.com:3500', () => {
// @ts-ignore
Cypress.Commands.overwrite('foo', () => 'baz')
// @ts-ignore
cy.foo().should('equal', 'baz')
})
// persists overwritten command through spec bridge
cy.origin('http://foobar.com:3500', () => {
// @ts-ignore
cy.foo().should('equal', 'baz')
})
})
})
context('Keyboard', () => {
it('does NOT sync defaults', () => {
const defaults = Cypress.Keyboard.defaults({
keystrokeDelay: 30,
})
cy.origin('http://foobar.com:3500', { args: defaults }, (primaryKeyboardDefaults) => {
const crossOriginKeyboardDefaults = Cypress.Keyboard.defaults({})
expect(crossOriginKeyboardDefaults).to.not.deep.equal(primaryKeyboardDefaults)
})
})
it('allows a user to configure defaults', () => {
cy.origin('http://foobar.com:3500', () => {
const crossOriginKeyboardDefaults = Cypress.Keyboard.defaults({
keystrokeDelay: 60,
})
expect(crossOriginKeyboardDefaults).to.deep.include({
keystrokeDelay: 60,
})
})
// persists default configuration changes through spec bridge
cy.origin('http://foobar.com:3500', () => {
const crossOriginKeyboardDefaults = Cypress.Keyboard.defaults({})
expect(crossOriginKeyboardDefaults).to.deep.include({
keystrokeDelay: 60,
})
})
})
})
context('Screenshot', () => {
it('does NOT sync defaults', () => {
Cypress.Screenshot.defaults({
blackout: ['foo'],
overwrite: true,
onBeforeScreenshot: () => undefined,
onAfterScreenshot: () => undefined,
})
cy.origin('http://foobar.com:3500', () => {
const crossOriginScreenshotDefaults = Cypress.Screenshot.defaults({})
expect(crossOriginScreenshotDefaults).to.not.deep.include({
blackout: ['foo'],
overwrite: true,
onBeforeScreenshot: () => undefined,
onAfterScreenshot: () => undefined,
})
})
})
it('allows a user to configure defaults', () => {
cy.origin('http://foobar.com:3500', () => {
const crossOriginScreenshotDefaults = Cypress.Screenshot.defaults({
blackout: ['foo'],
overwrite: true,
})
expect(crossOriginScreenshotDefaults).to.deep.include({
blackout: ['foo'],
overwrite: true,
})
})
// persists default configuration changes through spec bridge
cy.origin('http://foobar.com:3500', () => {
const crossOriginScreenshotDefaults = Cypress.Screenshot.defaults({})
expect(crossOriginScreenshotDefaults).to.deep.include({
blackout: ['foo'],
overwrite: true,
})
})
})
})
context('dom', () => {
it('provides a sanity check that the dom API exists on Cypress.*', () => {
cy.origin('http://foobar.com:3500', () => {
cy.get('[data-cy="dom-check"]').then(($el) => {
expect(Cypress.dom.isAttached($el)).to.be.true
})
})
})
})
context('properties', () => {
it('has arch property synced from primary', () => {
cy.origin('http://foobar.com:3500', { args: Cypress.arch }, (theArch) => {
expect(Cypress.arch).to.equal(theArch)
})
})
it('has browser property synced from primary', () => {
cy.origin('http://foobar.com:3500', { args: Cypress.browser }, (theBrowser) => {
expect(Cypress.browser).to.deep.equal(theBrowser)
})
})
it('has currentTest property synced from primary', () => {
cy.origin('http://foobar.com:3500', { args: Cypress.currentTest }, (theCurrentTest) => {
expect(Cypress.currentTest).to.deep.equal(theCurrentTest)
})
})
it('has platform property synced from primary', () => {
cy.origin('http://foobar.com:3500', { args: Cypress.platform }, (thePlatform) => {
expect(Cypress.platform).to.equal(thePlatform)
})
})
it('has testingType property synced from primary', () => {
cy.origin('http://foobar.com:3500', { args: Cypress.testingType }, (theTestingType) => {
expect(Cypress.testingType).to.deep.equal(theTestingType)
})
})
it('has spec property synced from primary', () => {
cy.origin('http://foobar.com:3500', { args: Cypress.spec }, (theSpec) => {
expect(Cypress.spec).to.deep.equal(theSpec)
})
})
})
context('methods', () => {
it('isCy()', () => {
cy.origin('http://foobar.com:3500', () => {
expect(Cypress.isCy(cy)).to.be.true
})
})
it('isBrowser()', () => {
cy.origin('http://foobar.com:3500', { args: Cypress.browser }, (theBrowser) => {
expect(Cypress.isBrowser(theBrowser.name)).to.equal(true)
})
})
it('log()', () => {
const logs: Cypress.Log[] = []
cy.on('log:added', (attrs, log: Cypress.Log) => {
logs.push(log)
})
cy.origin('http://foobar.com:3500', () => {
Cypress.log({
name: 'log',
message: 'test log',
})
})
.then(() => {
assertLogLength(logs, 3)
expect(logs[1].get('name')).to.equal('log')
expect(logs[1].get('message')).to.equal('test log')
})
})
})
context('not supported', () => {
it('throws an error when a user attempts to configure Cypress.Server.defaults() inside of cy.origin', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.equal('`Cypress.Server.*` has been deprecated and its use is not supported in the `cy.origin()` callback. Consider using `cy.intercept()` (outside of the callback) instead.')
expect(err.docsUrl).to.equal('https://on.cypress.io/intercept')
done()
})
cy.origin('http://foobar.com:3500', () => {
Cypress.Server.defaults({})
})
})
it('throws an error when a user attempts to configure Cypress.Cookies.preserveOnce() inside of cy.origin', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.equal('`Cypress.Cookies.preserveOnce` use is not supported in the `cy.origin()` callback. Consider using `cy.session()` (outside of the callback) instead.')
expect(err.docsUrl).to.equal('https://on.cypress.io/session')
done()
})
cy.origin('http://foobar.com:3500', () => {
// @ts-ignore
Cypress.Cookies.preserveOnce({})
})
})
it('throws an error when a user attempts to call Cypress.session.clearAllSavedSessions() inside of cy.origin', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.equal('`Cypress.session.*` methods are not supported in the `cy.switchToDomain()` callback. Consider using them outside of the callback instead.')
expect(err.docsUrl).to.equal('https://on.cypress.io/session-api')
done()
})
cy.origin('http://foobar.com:3500', () => {
Cypress.session.clearAllSavedSessions()
})
})
})
})