forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (83 loc) · 2.75 KB
/
index.js
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
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
require('cypress-wait-until')
const { isPlainObject, last } = Cypress._
/**
* Adds command "cy.waitForResources(name1, name2, ...)"
* that checks performance entries for resources that end with the given names.
* This command will be available in every spec file.
*
* @example cy.waitForResources('base.css', 'app.css')
*
* You can pass additional options, like "timeout"
*
* @example cy.waitForResources('base.css', 'app.css', { timeout: 3000 })
*/
Cypress.Commands.add('waitForResources', (...args) => {
if (Cypress.browser.family === 'firefox') {
cy.log('Skip waitForResource in Firefox')
return
}
let names
let options
if (isPlainObject(last(args))) {
names = args.slice(0, args.length - 1)
options = last(args)
} else {
names = args
options = {}
}
const log = false // let's not log inner commands
const timeout = options.timeout || Cypress.config('defaultCommandTimeout')
cy.log(`Waiting for resources ${names.join(', ')}`)
cy.window({ log }).then(
// note that ".then" method has options first, callback second
// https://on.cypress.io/then
{ log, timeout },
(win) => {
return new Cypress.Promise((resolve, reject) => {
// flag set when we find all names
let foundResources
// control how long we should try finding the resource
// and if it is still not found. An explicit "reject"
// allows us to show nice informative message
setTimeout(() => {
if (foundResources) {
// nothing needs to be done, successfully found the resource
return
}
clearInterval(interval)
reject(
new Error(`Timed out waiting for resources ${names.join(', ')}`)
)
}, timeout)
const interval = setInterval(() => {
foundResources = names.every((name) => {
return win.performance
.getEntriesByType('resource')
.find((item) => item.name.endsWith(name))
})
if (!foundResources) {
// some resource not found, will try again
return
}
cy.log('Found all resources')
clearInterval(interval)
resolve()
}, 100)
})
}
)
})