cypress-cdp
Advanced tools
Comparing version 1.5.5 to 1.6.0
{ | ||
"name": "cypress-cdp", | ||
"version": "1.5.5", | ||
"version": "1.6.0", | ||
"description": "A custom Cypress command to wrap the remote debugger protocol low level command", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -5,3 +5,3 @@ # cypress-cdp [](https://github.com/bahmutov/cypress-cdp/actions/workflows/ci.yml)  | ||
Read the blog posts: | ||
Read my blog posts that show this plugin in action: | ||
@@ -13,2 +13,3 @@ - [Cypress automation](https://glebbahmutov.com/blog/cypress-automation/) | ||
- [Emulate Media In Cypress Tests](https://glebbahmutov.com/blog/cypress-emulate-media/) | ||
- [Testing CSS Print Media Styles](https://glebbahmutov.com/blog/test-print-styles/) | ||
@@ -15,0 +16,0 @@ 🎓 Covered in my course [Cypress Plugins](https://cypress.tips/courses/cypress-plugins) and [Cypress Network Testing Exercises](https://cypress.tips/courses/network-testing). |
@@ -54,3 +54,23 @@ /// <reference types="cypress" /> | ||
let eventListenerStatus // track most recent failure mode | ||
let eventListenerRun | ||
let maxTimeout | ||
Cypress.Commands.add('hasEventListeners', (selector, options = {}) => { | ||
let retryOpts | ||
if (options.timeout === 0) { | ||
// is a retry | ||
retryOpts = options | ||
} else { | ||
// set up the timer | ||
eventListenerStatus = '' | ||
let runId = Date.now() | ||
eventListenerRun = runId // prevent failing the next test | ||
maxTimeout = options.timeout ?? Cypress.config().defaultCommandTimeout | ||
retryOpts = { ...options, timeout: 0, log: false } | ||
setTimeout(() => { | ||
if (eventListenerRun === runId && eventListenerStatus !== 'Passed') { | ||
throw new Error(eventListenerStatus) | ||
} | ||
}, maxTimeout) | ||
} | ||
const logOptions = { | ||
@@ -65,3 +85,3 @@ name: 'hasEventListeners', | ||
cy.get(selector, { log: false }).should(($el) => { | ||
cy.get(selector, { log: false, timeout: maxTimeout }).should(($el) => { | ||
if ($el.length !== 1) { | ||
@@ -78,3 +98,3 @@ throw new Error(`Need a single element with selector "${selector}`) | ||
}, | ||
{ log: false }, | ||
{ log: false, timeout: maxTimeout }, | ||
) | ||
@@ -97,28 +117,31 @@ .should((v) => { | ||
log: false, | ||
timeout: options.timeout, | ||
}, | ||
) | ||
.should((v) => { | ||
if (!v.listeners) { | ||
throw new Error('No listeners') | ||
).then((v) => { | ||
if (!v.listeners) { | ||
eventListenerStatus = 'No listeners' | ||
cy.hasEventListeners(selector, retryOpts) | ||
return | ||
} | ||
if (!v.listeners.length) { | ||
eventListenerStatus = 'Zero listeners' | ||
cy.hasEventListeners(selector, retryOpts) | ||
return | ||
} | ||
if (options.type) { | ||
const filtered = v.listeners.filter((l) => l.type === options.type) | ||
if (!filtered.length) { | ||
eventListenerStatus = `Zero listeners of type "${options.type}"` | ||
cy.hasEventListeners(selector, retryOpts) | ||
return | ||
} | ||
if (!v.listeners.length) { | ||
throw new Error('Zero listeners') | ||
} | ||
if (options.type) { | ||
const filtered = v.listeners.filter((l) => l.type === options.type) | ||
if (!filtered.length) { | ||
throw new Error(`Zero listeners of type "${options.type}"`) | ||
} | ||
eventListenerStatus = 'Passed' | ||
if (options.log !== false) { | ||
logOptions.consoleProps = () => { | ||
return { | ||
result: v.listeners, | ||
} | ||
} | ||
}) | ||
.then(({ listeners }) => { | ||
if (options.log !== false) { | ||
logOptions.consoleProps = () => { | ||
return { | ||
result: listeners, | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
}) | ||
}) | ||
@@ -125,0 +148,0 @@ }) |
13522
244
146