Skip to content

Commit aac89a1

Browse files
Add plugin events that mirror driver events (#2309)
* Closes #2283 Related: * Docs: cypress-io/cypress-documentation#967 * cypress-on: https://github.com/cypress-io/cypress-on/pull/96
1 parent 18a87de commit aac89a1

File tree

106 files changed

+1278
-873
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1278
-873
lines changed

cli/types/index.d.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -2014,14 +2014,14 @@ declare namespace Cypress {
20142014
*
20152015
* @param {Window} contentWindow the remote page's window object
20162016
*/
2017-
onBeforeLoad(win: Window): void
2017+
onStart(win: Window): void
20182018

20192019
/**
20202020
* Called once your page has fired its load event.
20212021
*
20222022
* @param {Window} contentWindow the remote page's window object
20232023
*/
2024-
onLoad(win: Window): void
2024+
onReady(win: Window): void
20252025

20262026
/**
20272027
* Whether to fail on response codes other than 2xx and 3xx
@@ -3654,7 +3654,7 @@ declare namespace Cypress {
36543654
(fn: (currentSubject: Subject) => void): Chainable<Subject>
36553655
}
36563656

3657-
// for just a few events like "window:alert" it makes sense to allow passing cy.stub() or
3657+
// for just a few events like "page:alert" it makes sense to allow passing cy.stub() or
36583658
// a user callback function. Others probably only need a callback function.
36593659

36603660
/**
@@ -3680,7 +3680,7 @@ declare namespace Cypress {
36803680
* // stub "window.alert" in a single test
36813681
* it('shows alert', () => {
36823682
* const stub = cy.stub()
3683-
* cy.on('window:alert', stub)
3683+
* cy.on('page:alert', stub)
36843684
* // trigger application code that calls alert(...)
36853685
* .then(() => {
36863686
* expect(stub).to.have.been.calledOnce
@@ -3694,56 +3694,56 @@ declare namespace Cypress {
36943694
* Cypress will auto accept confirmations. Return `false` from this event and the confirmation will be cancelled.
36953695
* @see https://on.cypress.io/catalog-of-events#App-Events
36963696
* @example
3697-
* cy.on('window:confirm', (str) => {
3697+
* cy.on('page:confirm', (str) => {
36983698
* console.log(str)
36993699
* return false // simulate "Cancel"
37003700
* })
37013701
*/
3702-
(action: 'window:confirm', fn: ((text: string) => false | void) | Agent<sinon.SinonSpy>): void
3702+
(action: 'page:confirm', fn: ((text: string) => false | void) | Agent<sinon.SinonSpy>): void
37033703
/**
37043704
* Fires when your app calls the global `window.alert()` method.
37053705
* Cypress will auto accept alerts. You cannot change this behavior.
37063706
* @example
37073707
* const stub = cy.stub()
3708-
* cy.on('window:alert', stub)
3708+
* cy.on('page:alert', stub)
37093709
* // assume the button calls window.alert()
37103710
* cy.get('.my-button').click()
37113711
* .then(() => {
37123712
* expect(stub).to.have.been.calledOnce
37133713
* })
37143714
* @see https://on.cypress.io/catalog-of-events#App-Events
37153715
*/
3716-
(action: 'window:alert', fn: ((text: string) => void) | Agent<sinon.SinonSpy>): void
3716+
(action: 'page:alert', fn: ((text: string) => void) | Agent<sinon.SinonSpy>): void
37173717
/**
3718-
* Fires as the page begins to load, but before any of your applications JavaScript has executed. This fires at the exact same time as `cy.visit()` `onBeforeLoad` callback. Useful to modify the window on a page transition.
3718+
* Fires as the page begins to load, but before any of your applications JavaScript has executed. This fires at the exact same time as `cy.visit()` `onStart` callback. Useful to modify the window on a page transition.
37193719
* @see https://on.cypress.io/catalog-of-events#App-Events
37203720
*/
3721-
(action: 'window:before:load', fn: (win: Window) => void): void
3721+
(action: 'page:start', fn: (win: Window) => void): void
37223722
/**
3723-
* Fires after all your resources have finished loading after a page transition. This fires at the exact same time as a `cy.visit()` `onLoad` callback.
3723+
* Fires after all your resources have finished loading after a page transition. This fires at the exact same time as a `cy.visit()` `onReady` callback.
37243724
* @see https://on.cypress.io/catalog-of-events#App-Events
37253725
*/
3726-
(action: 'window:load', fn: (win: Window) => void): void
3726+
(action: 'page:ready', fn: (win: Window) => void): void
37273727
/**
37283728
* Fires when your application is about to navigate away. The real event object is provided to you. Your app may have set a `returnValue` on the event, which is useful to assert on.
37293729
* @see https://on.cypress.io/catalog-of-events#App-Events
37303730
*/
3731-
(action: 'window:before:unload', fn: (event: BeforeUnloadEvent) => void): void
3731+
(action: 'before:window:unload', fn: (event: BeforeUnloadEvent) => void): void
37323732
/**
37333733
* Fires when your application is has unloaded and is navigating away. The real event object is provided to you. This event is not cancelable.
37343734
* @see https://on.cypress.io/catalog-of-events#App-Events
37353735
*/
3736-
(action: 'window:unload', fn: (event: Event) => void): void
3736+
(action: 'page:end', fn: (event: Event) => void): void
37373737
/**
37383738
* Fires whenever Cypress detects that your application's URL has changed.
37393739
* @see https://on.cypress.io/catalog-of-events#App-Events
37403740
*/
3741-
(action: 'url:changed', fn: (url: string) => void): void
3741+
(action: 'page:url:changed', fn: (url: string) => void): void
37423742
/**
37433743
* Fires when the test has failed. It is technically possible to prevent the test from actually failing by binding to this event and invoking an async `done` callback. However this is **strongly discouraged**. Tests should never legitimately fail. This event exists because it's extremely useful for debugging purposes.
37443744
* @see https://on.cypress.io/catalog-of-events#App-Events
37453745
*/
3746-
(action: 'fail', fn: (error: Error, mocha: Mocha.IRunnable) => void): void
3746+
(action: 'test:fail', fn: (error: Error, mocha: Mocha.IRunnable) => void): void
37473747
/**
37483748
* Fires whenever the viewport changes via a `cy.viewport()` or naturally when Cypress resets the viewport to the default between tests. Useful for debugging purposes.
37493749
* @see https://on.cypress.io/catalog-of-events#App-Events
@@ -3753,7 +3753,7 @@ declare namespace Cypress {
37533753
* Fires whenever **Cypress** is scrolling your application. This event is fired when Cypress is {% url 'waiting for and calculating actionability' interacting-with-elements %}. It will scroll to 'uncover' elements currently being covered. This event is extremely useful to debug why Cypress may think an element is not interactive.
37543754
* @see https://on.cypress.io/catalog-of-events#App-Events
37553755
*/
3756-
(action: 'scrolled', fn: ($el: JQuery) => void): void
3756+
(action: 'internal:scrolled', fn: ($el: JQuery) => void): void
37573757
/**
37583758
* Fires when a cy command is first invoked and enqueued to be run later. Useful for debugging purposes if you're confused about the order in which commands will execute.
37593759
* @see https://on.cypress.io/catalog-of-events#App-Events
@@ -3788,12 +3788,12 @@ declare namespace Cypress {
37883788
* Fires before the test and all **before** and **beforeEach** hooks run.
37893789
* @see https://on.cypress.io/catalog-of-events#App-Events
37903790
*/
3791-
(action: 'test:before:run', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
3791+
(action: 'test:run:start', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
37923792
/**
37933793
* Fires after the test and all **afterEach** and **after** hooks run.
37943794
* @see https://on.cypress.io/catalog-of-events#App-Events
37953795
*/
3796-
(action: 'test:after:run', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
3796+
(action: 'test:run:end', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
37973797
}
37983798

37993799
// $CommandQueue from `command_queue.coffee` - a lot to type. Might be more useful if it was written in TS

cli/types/tests/actions.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@ Cypress.on('uncaught:exception', (error, runnable) => {
33
runnable // $ExpectType IRunnable
44
})
55

6-
Cypress.on('window:confirm', (text) => {
6+
Cypress.on('page:confirm', (text) => {
77
text // $ExpectType string
88
})
99

10-
Cypress.on('window:alert', (text) => {
10+
Cypress.on('page:alert', (text) => {
1111
text // $ExpectType string
1212
})
1313

14-
Cypress.on('window:before:load', (win) => {
14+
Cypress.on('page:start', (win) => {
1515
win // $ExpectType Window
1616
})
1717

18-
Cypress.on('window:load', (win) => {
18+
Cypress.on('page:ready', (win) => {
1919
win // $ExpectType Window
2020
})
2121

22-
Cypress.on('window:before:unload', (event) => {
22+
Cypress.on('before:window:unload', (event) => {
2323
event // $ExpectType BeforeUnloadEvent
2424
})
2525

26-
Cypress.on('window:unload', (event) => {
26+
Cypress.on('page:end', (event) => {
2727
event // $ExpectType Event
2828
})
2929

30-
Cypress.on('url:changed', (url) => {
30+
Cypress.on('page:url:changed', (url) => {
3131
url // $ExpectType string
3232
})
3333

34-
Cypress.on('fail', (error, mocha) => {
34+
Cypress.on('test:fail', (error, mocha) => {
3535
error // $ExpectType Error
3636
mocha // $ExpectType IRunnable
3737
})
@@ -40,7 +40,7 @@ Cypress.on('viewport:changed', (viewport) => {
4040
viewport // $ExpectType Viewport
4141
})
4242

43-
Cypress.on('scrolled', ($el) => {
43+
Cypress.on('internal:scrolled', ($el) => {
4444
$el // $ExpectType JQuery<HTMLElement>
4545
})
4646

@@ -68,12 +68,12 @@ Cypress.on('log:changed', (log, interactive: boolean) => {
6868
log // $ExpectTyped any
6969
})
7070

71-
Cypress.on('test:before:run', (attributes , test) => {
71+
Cypress.on('test:run:start', (attributes , test) => {
7272
attributes // $ExpectType ObjectLike
7373
test // $ExpectType ITest
7474
})
7575

76-
Cypress.on('test:after:run', (attributes , test) => {
76+
Cypress.on('test:run:end', (attributes , test) => {
7777
attributes // $ExpectType ObjectLike
7878
test // $ExpectType ITest
7979
})

cli/types/tests/kitchen-sink.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ Cypress.browser // $ExpectType Browser
3838

3939
// stubbing window.alert type on "Cypress" should
4040
// work with plain function or with a Sinon stub
41-
Cypress.on('window:alert', () => {})
42-
Cypress.on('window:alert', cy.stub())
41+
Cypress.on('page:alert', () => {})
42+
Cypress.on('page:alert', cy.stub())
4343
// same for a single test
44-
cy.on('window:alert', () => {})
45-
cy.on('window:alert', cy.stub())
44+
cy.on('page:alert', () => {})
45+
cy.on('page:alert', cy.stub())
4646

47-
// window:confirm stubbing
48-
cy.on('window:confirm', () => {})
49-
cy.on('window:confirm', cy.stub())
47+
// page:confirm stubbing
48+
cy.on('page:confirm', () => {})
49+
cy.on('page:confirm', cy.stub())
5050

5151
// specifying HTTP method directly in the options object
5252
cy.request({

packages/desktop-gui/cypress/integration/update_banner_spec.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe "Update Banner", ->
1212
cy.fixture("specs").as("specs")
1313

1414
cy.visitIndex({
15-
onBeforeLoad: (win) ->
15+
onStart: (win) ->
1616
cy.spy(win, "setInterval")
1717
}).then (win) ->
1818
{ @start, @ipc } = win.App

packages/driver/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ after:add | Runner | Anyone | when all runnables have been added to the UI
7777
runnables:ready | Runner | Anyone | when all runnables have been reduced to basic objects
7878
mocha:start | Mocha | Cypress | when mocha runner triggers its 'start' event
7979
suite:start | Mocha | Cypress | when mocha runner fires its 'suite' event
80-
test:before:run:async | Cypress | Anyone | before any code has run for a particular test
81-
test:before:run:async | Cypress | Cypress | before any hooks for a test have started
80+
test:run:start:async | Cypress | Anyone | before any code has run for a particular test
81+
test:run:start:async | Cypress | Cypress | before any hooks for a test have started
8282
hook:start | Mocha | Cypress | when mocha runner fires its 'hook' event
8383
test:start | Mocha | Cypress | when mocha runner fires its 'test' event
8484
suite:end | Mocha | Cypress | when mocha runner fires its 'suite end' event
@@ -88,8 +88,8 @@ mocha:pending | Mocha | Cypress | when mocha runner fires its 'pending' event
8888
mocha:fail | Mocha | Cypress | when mocha runner fires its 'fail' event
8989
test:end | Mocha | Cypress | when mocha runner fires its 'test end' event
9090
test:results:ready | Runner | Anyone | when we receive the 'test:end' event
91-
test:after:hooks | Cypress | Cypress | after all hooks have run for a test
92-
test:after:run | Cypress | Anyone | after any code has run for a test
91+
after:test:hooks | Cypress | Cypress | after all hooks have run for a test
92+
test:run:end | Cypress | Anyone | after any code has run for a test
9393
mocha:end | Mocha | Cypress | when mocha runner fires its 'end' event
9494
after:run | Runner | Anyone | after run has finished
9595
@@ -119,7 +119,7 @@ paused | Cypress | Runner | when pausing is being requested
119119
120120
Event | From | To | Description
121121
--- | --- | --- | ---
122-
url:changed | Cypress | Anyone | when aut app url is changed
122+
page:url:changed | Cypress | Anyone | when aut app url is changed
123123
page:loading | Cypress | Anyone | when aut app is currently loading a page
124124
viewport | Cypress | Anyone | when viewport has changed
125125

packages/driver/src/cy/commands/actions/type.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ weekRegex = /^\d{4}-W(0[1-9]|[1-4]\d|5[0-3])$/
1717
timeRegex = /^([0-1]\d|2[0-3]):[0-5]\d(:[0-5]\d)?(\.[0-9]{1,3})?$/
1818

1919
module.exports = (Commands, Cypress, cy, state, config) ->
20-
Cypress.on "test:before:run", ->
20+
Cypress.on "test:run:start", ->
2121
$Keyboard.resetModifiers(state("document"), state("window"))
2222

2323
Commands.addAll({ prevSubject: "element" }, {

packages/driver/src/cy/commands/agents.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->
119119

120120
## before each of our tests we always want
121121
## to reset the counts + the sandbox
122-
Cypress.on("test:before:run", resetAndSetSandbox)
122+
Cypress.on("test:run:start", resetAndSetSandbox)
123123

124124
wrap = (ctx, type, agent, obj, method, count) ->
125125
if not count

packages/driver/src/cy/commands/clock.coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ module.exports = (Commands, Cypress, cy, state, config) ->
2222
## this MUST be prepended else if we are stubbing or spying on
2323
## global timers they will be reset in agents before this runs
2424
## its reset function
25-
Cypress.prependListener("test:before:run", reset)
25+
Cypress.prependListener("test:run:start", reset)
2626

27-
Cypress.on "window:before:load", (contentWindow) ->
27+
Cypress.on "page:start", (contentWindow) ->
2828
## if a clock has been created before this event (likely before
2929
## a cy.visit(), then bind that clock to the new window
3030
if clock

packages/driver/src/cy/commands/cookies.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->
7070

7171
automateCookies("clear:cookies", cookies, log, timeout)
7272

73-
Cypress.on "test:before:run:async", ->
73+
Cypress.on "test:run:start:async", ->
7474
## TODO: handle failure here somehow
7575
## maybe by tapping into the Cypress reset
7676
## stuff, or handling this in the runner itself?

packages/driver/src/cy/commands/local_storage.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ clearLocalStorage = (state, keys) ->
2121

2222
module.exports = (Commands, Cypress, cy, state, config) ->
2323
## this MUST be prepended before anything else
24-
Cypress.prependListener "test:before:run", ->
24+
Cypress.prependListener "test:run:start", ->
2525
try
2626
## this may fail if the current
2727
## window is bound to another origin

0 commit comments

Comments
 (0)