Skip to content

Commit

Permalink
Add plugin events that mirror driver events (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding authored Dec 4, 2018
1 parent 18a87de commit aac89a1
Show file tree
Hide file tree
Showing 106 changed files with 1,278 additions and 873 deletions.
38 changes: 19 additions & 19 deletions cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2014,14 +2014,14 @@ declare namespace Cypress {
*
* @param {Window} contentWindow the remote page's window object
*/
onBeforeLoad(win: Window): void
onStart(win: Window): void

/**
* Called once your page has fired its load event.
*
* @param {Window} contentWindow the remote page's window object
*/
onLoad(win: Window): void
onReady(win: Window): void

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

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

/**
Expand All @@ -3680,7 +3680,7 @@ declare namespace Cypress {
* // stub "window.alert" in a single test
* it('shows alert', () => {
* const stub = cy.stub()
* cy.on('window:alert', stub)
* cy.on('page:alert', stub)
* // trigger application code that calls alert(...)
* .then(() => {
* expect(stub).to.have.been.calledOnce
Expand All @@ -3694,56 +3694,56 @@ declare namespace Cypress {
* Cypress will auto accept confirmations. Return `false` from this event and the confirmation will be cancelled.
* @see https://on.cypress.io/catalog-of-events#App-Events
* @example
* cy.on('window:confirm', (str) => {
* cy.on('page:confirm', (str) => {
* console.log(str)
* return false // simulate "Cancel"
* })
*/
(action: 'window:confirm', fn: ((text: string) => false | void) | Agent<sinon.SinonSpy>): void
(action: 'page:confirm', fn: ((text: string) => false | void) | Agent<sinon.SinonSpy>): void
/**
* Fires when your app calls the global `window.alert()` method.
* Cypress will auto accept alerts. You cannot change this behavior.
* @example
* const stub = cy.stub()
* cy.on('window:alert', stub)
* cy.on('page:alert', stub)
* // assume the button calls window.alert()
* cy.get('.my-button').click()
* .then(() => {
* expect(stub).to.have.been.calledOnce
* })
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:alert', fn: ((text: string) => void) | Agent<sinon.SinonSpy>): void
(action: 'page:alert', fn: ((text: string) => void) | Agent<sinon.SinonSpy>): void
/**
* 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.
* 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.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:before:load', fn: (win: Window) => void): void
(action: 'page:start', fn: (win: Window) => void): void
/**
* 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.
* 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.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:load', fn: (win: Window) => void): void
(action: 'page:ready', fn: (win: Window) => void): void
/**
* 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.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:before:unload', fn: (event: BeforeUnloadEvent) => void): void
(action: 'before:window:unload', fn: (event: BeforeUnloadEvent) => void): void
/**
* Fires when your application is has unloaded and is navigating away. The real event object is provided to you. This event is not cancelable.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'window:unload', fn: (event: Event) => void): void
(action: 'page:end', fn: (event: Event) => void): void
/**
* Fires whenever Cypress detects that your application's URL has changed.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'url:changed', fn: (url: string) => void): void
(action: 'page:url:changed', fn: (url: string) => void): void
/**
* 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.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'fail', fn: (error: Error, mocha: Mocha.IRunnable) => void): void
(action: 'test:fail', fn: (error: Error, mocha: Mocha.IRunnable) => void): void
/**
* 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.
* @see https://on.cypress.io/catalog-of-events#App-Events
Expand All @@ -3753,7 +3753,7 @@ declare namespace Cypress {
* 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.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'scrolled', fn: ($el: JQuery) => void): void
(action: 'internal:scrolled', fn: ($el: JQuery) => void): void
/**
* 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.
* @see https://on.cypress.io/catalog-of-events#App-Events
Expand Down Expand Up @@ -3788,12 +3788,12 @@ declare namespace Cypress {
* Fires before the test and all **before** and **beforeEach** hooks run.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'test:before:run', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
(action: 'test:run:start', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
/**
* Fires after the test and all **afterEach** and **after** hooks run.
* @see https://on.cypress.io/catalog-of-events#App-Events
*/
(action: 'test:after:run', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
(action: 'test:run:end', fn: (attributes: ObjectLike, test: Mocha.ITest) => void): void
}

// $CommandQueue from `command_queue.coffee` - a lot to type. Might be more useful if it was written in TS
Expand Down
22 changes: 11 additions & 11 deletions cli/types/tests/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ Cypress.on('uncaught:exception', (error, runnable) => {
runnable // $ExpectType IRunnable
})

Cypress.on('window:confirm', (text) => {
Cypress.on('page:confirm', (text) => {
text // $ExpectType string
})

Cypress.on('window:alert', (text) => {
Cypress.on('page:alert', (text) => {
text // $ExpectType string
})

Cypress.on('window:before:load', (win) => {
Cypress.on('page:start', (win) => {
win // $ExpectType Window
})

Cypress.on('window:load', (win) => {
Cypress.on('page:ready', (win) => {
win // $ExpectType Window
})

Cypress.on('window:before:unload', (event) => {
Cypress.on('before:window:unload', (event) => {
event // $ExpectType BeforeUnloadEvent
})

Cypress.on('window:unload', (event) => {
Cypress.on('page:end', (event) => {
event // $ExpectType Event
})

Cypress.on('url:changed', (url) => {
Cypress.on('page:url:changed', (url) => {
url // $ExpectType string
})

Cypress.on('fail', (error, mocha) => {
Cypress.on('test:fail', (error, mocha) => {
error // $ExpectType Error
mocha // $ExpectType IRunnable
})
Expand All @@ -40,7 +40,7 @@ Cypress.on('viewport:changed', (viewport) => {
viewport // $ExpectType Viewport
})

Cypress.on('scrolled', ($el) => {
Cypress.on('internal:scrolled', ($el) => {
$el // $ExpectType JQuery<HTMLElement>
})

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

Cypress.on('test:before:run', (attributes , test) => {
Cypress.on('test:run:start', (attributes , test) => {
attributes // $ExpectType ObjectLike
test // $ExpectType ITest
})

Cypress.on('test:after:run', (attributes , test) => {
Cypress.on('test:run:end', (attributes , test) => {
attributes // $ExpectType ObjectLike
test // $ExpectType ITest
})
14 changes: 7 additions & 7 deletions cli/types/tests/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ Cypress.browser // $ExpectType Browser

// stubbing window.alert type on "Cypress" should
// work with plain function or with a Sinon stub
Cypress.on('window:alert', () => {})
Cypress.on('window:alert', cy.stub())
Cypress.on('page:alert', () => {})
Cypress.on('page:alert', cy.stub())
// same for a single test
cy.on('window:alert', () => {})
cy.on('window:alert', cy.stub())
cy.on('page:alert', () => {})
cy.on('page:alert', cy.stub())

// window:confirm stubbing
cy.on('window:confirm', () => {})
cy.on('window:confirm', cy.stub())
// page:confirm stubbing
cy.on('page:confirm', () => {})
cy.on('page:confirm', cy.stub())

// specifying HTTP method directly in the options object
cy.request({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe "Update Banner", ->
cy.fixture("specs").as("specs")

cy.visitIndex({
onBeforeLoad: (win) ->
onStart: (win) ->
cy.spy(win, "setInterval")
}).then (win) ->
{ @start, @ipc } = win.App
Expand Down
10 changes: 5 additions & 5 deletions packages/driver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ after:add | Runner | Anyone | when all runnables have been added to the UI
runnables:ready | Runner | Anyone | when all runnables have been reduced to basic objects
mocha:start | Mocha | Cypress | when mocha runner triggers its 'start' event
suite:start | Mocha | Cypress | when mocha runner fires its 'suite' event
test:before:run:async | Cypress | Anyone | before any code has run for a particular test
test:before:run:async | Cypress | Cypress | before any hooks for a test have started
test:run:start:async | Cypress | Anyone | before any code has run for a particular test
test:run:start:async | Cypress | Cypress | before any hooks for a test have started
hook:start | Mocha | Cypress | when mocha runner fires its 'hook' event
test:start | Mocha | Cypress | when mocha runner fires its 'test' event
suite:end | Mocha | Cypress | when mocha runner fires its 'suite end' event
Expand All @@ -88,8 +88,8 @@ mocha:pending | Mocha | Cypress | when mocha runner fires its 'pending' event
mocha:fail | Mocha | Cypress | when mocha runner fires its 'fail' event
test:end | Mocha | Cypress | when mocha runner fires its 'test end' event
test:results:ready | Runner | Anyone | when we receive the 'test:end' event
test:after:hooks | Cypress | Cypress | after all hooks have run for a test
test:after:run | Cypress | Anyone | after any code has run for a test
after:test:hooks | Cypress | Cypress | after all hooks have run for a test
test:run:end | Cypress | Anyone | after any code has run for a test
mocha:end | Mocha | Cypress | when mocha runner fires its 'end' event
after:run | Runner | Anyone | after run has finished
Expand Down Expand Up @@ -119,7 +119,7 @@ paused | Cypress | Runner | when pausing is being requested
Event | From | To | Description
--- | --- | --- | ---
url:changed | Cypress | Anyone | when aut app url is changed
page:url:changed | Cypress | Anyone | when aut app url is changed
page:loading | Cypress | Anyone | when aut app is currently loading a page
viewport | Cypress | Anyone | when viewport has changed
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/actions/type.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ weekRegex = /^\d{4}-W(0[1-9]|[1-4]\d|5[0-3])$/
timeRegex = /^([0-1]\d|2[0-3]):[0-5]\d(:[0-5]\d)?(\.[0-9]{1,3})?$/

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

Commands.addAll({ prevSubject: "element" }, {
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/agents.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->

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

wrap = (ctx, type, agent, obj, method, count) ->
if not count
Expand Down
4 changes: 2 additions & 2 deletions packages/driver/src/cy/commands/clock.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ module.exports = (Commands, Cypress, cy, state, config) ->
## this MUST be prepended else if we are stubbing or spying on
## global timers they will be reset in agents before this runs
## its reset function
Cypress.prependListener("test:before:run", reset)
Cypress.prependListener("test:run:start", reset)

Cypress.on "window:before:load", (contentWindow) ->
Cypress.on "page:start", (contentWindow) ->
## if a clock has been created before this event (likely before
## a cy.visit(), then bind that clock to the new window
if clock
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/cookies.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = (Commands, Cypress, cy, state, config) ->

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

Cypress.on "test:before:run:async", ->
Cypress.on "test:run:start:async", ->
## TODO: handle failure here somehow
## maybe by tapping into the Cypress reset
## stuff, or handling this in the runner itself?
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/local_storage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ clearLocalStorage = (state, keys) ->

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

0 comments on commit aac89a1

Please sign in to comment.