forked from bahmutov/cypress-watch-and-reload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.js
More file actions
51 lines (48 loc) · 1.63 KB
/
support.js
File metadata and controls
51 lines (48 loc) · 1.63 KB
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
/// <reference types="Cypress" />
if (Cypress.config('isInteractive')) {
const insertToggleButton = require('./ui/toggle-btn')
const waitUntil = require('async-wait-until')
const ws = new WebSocket('ws://localhost:8765')
let watchAndReloadEnabled = true
const button = insertToggleButton()
button.onclick = () => {
button.classList.toggle('auto-scrolling-enabled')
watchAndReloadEnabled = !watchAndReloadEnabled
}
beforeEach(() => {
if (!Cypress.env('cypressWatchAndReloadPluginInitialized')) {
throw new Error(
'Did you forget to initialize the cypress-watch-and-reload plugin ' +
'from your plugins file? See https://github.com/bahmutov/cypress-watch-and-reload#use',
)
}
cy.wrap(
waitUntil(() => ws.readyState === WebSocket.OPEN, 2000, 50),
{ log: false },
).then(() => {
ws.onmessage = (ev) => {
console.log('message from OS')
console.log(ev)
if (ev.type === 'message' && ev.data) {
try {
const data = JSON.parse(ev.data)
if (data.command === 'reload' && data.filename) {
if (watchAndReloadEnabled) {
console.log(
'reloading Cypress because "%s" has changed',
data.filename,
)
window.top.document.querySelector('.reporter .restart').click()
}
}
} catch (e) {
console.error('Could not parse message from plugin')
console.error(e.message)
console.error('original text')
console.error(ev.data)
}
}
}
})
})
}