Skip to content

Commit d5d97b0

Browse files
authored
add cy-events recipe (#632)
1 parent 43ee04f commit d5d97b0

File tree

9 files changed

+123
-1
lines changed

9 files changed

+123
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Recipe | Description
2020
[Use Chrome Remote Interface](./examples/fundamentals__chrome-remote-debugging) | Use Chrome debugger protocol to trigger hover state and print media style
2121
[Out-of-the-box TypeScript](./examples/fundamentals__typescript) | Write tests in TypeScript without setting up preprocessors
2222
[Per-test timeout](./examples/fundamentals__timeout) | Fail a test if it runs longer than the specified time limit
23+
[Cypress events](./examples/fundamentals__cy-events) | Using `Cypress.on` and `cy.on` to listen to [Cypress events](https://on.cypress.io/catalog-of-events) like `before:window:load`
2324

2425
## Testing the DOM
2526

circle.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ jobs:
223223
<<: *defaults
224224
fundamentals__errors:
225225
<<: *defaults
226+
fundamentals__cy-events:
227+
<<: *defaults
226228
logging-in__csrf-tokens:
227229
<<: *defaults
228230
logging-in__html-web-forms:
@@ -433,6 +435,9 @@ all_jobs: &all_jobs
433435
- fundamentals__errors:
434436
requires:
435437
- build
438+
- fundamentals__cy-events:
439+
requires:
440+
- build
436441
- logging-in__csrf-tokens:
437442
requires:
438443
- build
@@ -615,8 +620,9 @@ all_jobs: &all_jobs
615620
- fundamentals__module-api-wrap
616621
- fundamentals__add-custom-command
617622
- fundamentals__add-custom-command-ts
618-
- fundamentals__errors
619623
- fundamentals__typescript
624+
- fundamentals__errors
625+
- fundamentals__cy-events
620626
- logging-in__csrf-tokens
621627
- logging-in__html-web-forms
622628
- logging-in__single-sign-on
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Cypress events
2+
3+
For more details, see [Cypress catalogue of events](https://on.cypress.io/catalog-of-events)
4+
5+
- [cypress-on-spec.js]('./cypress/integration/cypress-on-spec.js) shows how we can add a property to every `window` object before the app load using `Cypress.on('window:before:load', ...)` event listener
6+
- [cy-on-spec.js]('./cypress/integration/cy-on-spec.js) shows how to add a property to every `window` object before the app loads using `cy.on('window:before:load', ...)` event listener.
7+
8+
## See also
9+
10+
- recipe "Handling errors" from the [Fundamentals recipes](https://github.com/cypress-io/cypress-example-recipes#fundamentals)
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-env browser */
2+
document
3+
.getElementById('click-me')
4+
.addEventListener('click', () => {
5+
/* global Analytics */
6+
Analytics.sendEvent('click', 'button#click-me')
7+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"pluginsFile": false,
3+
"fixturesFolder": false,
4+
"supportFile": false
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/// <reference types="cypress" />
2+
3+
describe('cy.on window:before:load', () => {
4+
// @see https://on.cypress.io/catalog-of-events
5+
// let's use "window:before:load" to automatically
6+
// attach mock Analytics method to the application's "window" object
7+
// we will use "cy.on" which requires a test or a hook to work
8+
beforeEach(() => {
9+
cy.on('window:before:load', (win) => {
10+
win.Analytics = {
11+
sendEvent: cy.stub().as('sendEvent'),
12+
}
13+
})
14+
})
15+
16+
it('sends events', () => {
17+
cy.visit('index.html')
18+
cy.get('button#click-me').click().click()
19+
cy.get('@sendEvent').should('be.calledTwice')
20+
.invoke('reset') // reset the stub counter
21+
22+
cy.get('button#click-me').click()
23+
cy.get('@sendEvent').should('be.calledOnceWithExactly', 'click', 'button#click-me')
24+
})
25+
26+
it('sends more events', () => {
27+
cy.visit('index.html')
28+
cy.get('button#click-me').click()
29+
// if we do not precise value for one of the arguments
30+
// we can "skip" it but make sure it is still a string
31+
// using Sinon matchers
32+
//
33+
cy.get('@sendEvent').should('be.calledOnceWith',
34+
Cypress.sinon.match.string, 'button#click-me')
35+
})
36+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/// <reference types="cypress" />
2+
3+
describe('Cypress.on window:before:load', () => {
4+
// @see https://on.cypress.io/catalog-of-events
5+
// let's use "window:before:load" to automatically
6+
// attach mock Analytics method to the application's "window" object
7+
Cypress.on('window:before:load', (win) => {
8+
win.Analytics = {
9+
sendEvent: cy.stub().as('sendEvent'),
10+
}
11+
})
12+
13+
it('sends events', () => {
14+
cy.visit('index.html')
15+
cy.get('button#click-me').click().click()
16+
cy.get('@sendEvent').should('be.calledTwice')
17+
.invoke('reset') // reset the stub counter
18+
19+
cy.get('button#click-me').click()
20+
cy.get('@sendEvent').should('be.calledOnceWithExactly', 'click', 'button#click-me')
21+
})
22+
23+
it('sends more events', () => {
24+
cy.visit('index.html')
25+
cy.get('button#click-me').click()
26+
// if we do not precise value for one of the arguments
27+
// we can "skip" it but make sure it is still a string
28+
// using Sinon matchers
29+
//
30+
cy.get('@sendEvent').should('be.calledOnceWith',
31+
Cypress.sinon.match.string, 'button#click-me')
32+
})
33+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html lang="en">
2+
<body>
3+
<main>
4+
<h1>Register events</h1>
5+
<p>
6+
Click on the button to file "analytics" event
7+
<button id="click-me">Click me!</button>
8+
</p>
9+
</main>
10+
<script src="app.js"></script>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "cy-events",
3+
"version": "1.0.0",
4+
"description": "Examples of Cypress events",
5+
"private": true,
6+
"scripts": {
7+
"cypress:open": "../../node_modules/.bin/cypress open",
8+
"cypress:run": "../../node_modules/.bin/cypress run",
9+
"test:ci": "../../node_modules/.bin/cypress run",
10+
"test:ci:record": "../../node_modules/.bin/cypress run --record"
11+
}
12+
}

0 commit comments

Comments
 (0)