Skip to content

Commit

Permalink
fix: fix cross-origin cy.pause() (#24405)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Oct 26, 2022
1 parent 945c551 commit b1058d9
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
22 changes: 3 additions & 19 deletions packages/app/src/runner/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { automation, useRunnerUiStore } from '../store'
import { useScreenshotStore } from '../store/screenshot-store'
import { useStudioStore } from '../store/studio-store'
import { getAutIframeModel } from '.'
import { handlePausing } from './events/pausing'

export type CypressInCypressMochaEvent = Array<Array<string | Record<string, any>>>

Expand All @@ -33,7 +34,6 @@ interface AddGlobalListenerOptions {
randomString: string
}

const driverToReporterEvents = 'paused'.split(' ')
const driverToLocalAndReporterEvents = 'run:start run:end'.split(' ')
const driverToSocketEvents = 'backend:request automation:request mocha recorder:frame'.split(' ')
const driverTestEvents = 'test:before:run:async test:after:run'.split(' ')
Expand Down Expand Up @@ -225,18 +225,6 @@ export class EventManager {

this.reporterBus.on('runner:unpin:snapshot', this._unpinSnapshot.bind(this))

this.reporterBus.on('runner:resume', () => {
if (!Cypress) return

Cypress.emit('resume:all')
})

this.reporterBus.on('runner:next', () => {
if (!Cypress) return

Cypress.emit('resume:next')
})

this.reporterBus.on('runner:stop', () => {
if (!Cypress) return

Expand Down Expand Up @@ -538,12 +526,6 @@ export class EventManager {

Cypress.on('after:screenshot', handleAfterScreenshot)

driverToReporterEvents.forEach((event) => {
Cypress.on(event, (...args) => {
this.reporterBus.emit(event, ...args)
})
})

driverTestEvents.forEach((event) => {
Cypress.on(event, (test, cb) => {
this.reporterBus.emit(event, test, cb)
Expand Down Expand Up @@ -596,6 +578,8 @@ export class EventManager {
}
})

handlePausing(this.getCypress, this.reporterBus)

Cypress.on('test:before:run', (...args) => {
Cypress.primaryOriginCommunicator.toAllSpecBridges('test:before:run', ...args)
})
Expand Down
50 changes: 50 additions & 0 deletions packages/app/src/runner/events/pausing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export const handlePausing = (getCypress, reporterBus) => {
const Cypress = getCypress()
// tracks whether the cy.pause() was called from the primary driver
// (value === null) or from a cross-origin spec bridge (value is the origin
// matching that spec bridge)
let sendEventsToOrigin = null

reporterBus.on('runner:next', () => {
const Cypress = getCypress()

if (!Cypress) return

// if paused from within a cy.origin() callback, send the event to the
// corresponding spec bridge
if (sendEventsToOrigin) {
Cypress.primaryOriginCommunicator.toSpecBridge(sendEventsToOrigin, 'resume:next')
} else {
Cypress.emit('resume:next')
}
})

reporterBus.on('runner:resume', () => {
const Cypress = getCypress()

if (!Cypress) return

// if paused from within a cy.origin() callback, send the event to the
// corresponding spec bridge
if (sendEventsToOrigin) {
Cypress.primaryOriginCommunicator.toSpecBridge(sendEventsToOrigin, 'resume:all')
} else {
Cypress.emit('resume:all')
}

// pause sequence is over - reset this for subsequent pauses
sendEventsToOrigin = null
})

// from the primary driver
Cypress.on('paused', (nextCommandName) => {
reporterBus.emit('paused', nextCommandName)
})

// from a cross-origin spec bridge
Cypress.primaryOriginCommunicator.on('paused', ({ nextCommandName, origin }) => {
sendEventsToOrigin = origin

reporterBus.emit('paused', nextCommandName)
})
}
16 changes: 14 additions & 2 deletions packages/driver/cypress/e2e/e2e/origin/commands/misc.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,22 @@ context('cy.origin misc', { browser: '!webkit' }, () => {
})

it('.pause()', () => {
// ensures the 'paused' event makes it to the event-manager in the primary.
// if we get cross-origin cy-in-cy test working, we could potentially make
// this even more end-to-end: test out the reporter UI and click the
// resume buttons instead of sending the resume:all event
Cypress.primaryOriginCommunicator.once('paused', ({ nextCommandName, origin }) => {
expect(nextCommandName).to.equal('wrap')
expect(origin).to.equal('http://www.foobar.com:3500')

Cypress.primaryOriginCommunicator.toSpecBridge(origin, 'resume:all')
})

cy.origin('http://www.foobar.com:3500', () => {
const afterPaused = new Promise<void>((resolve) => {
cy.once('paused', () => {
Cypress.emit('resume:all')
// event is sent from the event listener in the primary above,
// ensuring that the pause sequence has come full circle
cy.once('resume:all', () => {
resolve()
})
})
Expand Down
12 changes: 12 additions & 0 deletions packages/driver/src/cross-origin/events/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ export const handleMiscEvents = (Cypress: Cypress.Cypress, cy: $Cy) => {
Cypress.action('app:window:load', undefined, url)
Cypress.emit('internal:window:load', { type: 'same:origin', url })
})

Cypress.on('paused', (nextCommandName: string) => {
Cypress.specBridgeCommunicator.toPrimary('paused', { nextCommandName, origin: window.origin })
})

Cypress.specBridgeCommunicator.on('resume:next', () => {
Cypress.emit('resume:next')
})

Cypress.specBridgeCommunicator.on('resume:all', () => {
Cypress.emit('resume:all')
})
}
1 change: 1 addition & 0 deletions packages/driver/src/cy/commands/origin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy, state: State
name: 'origin',
type: 'parent',
message: urlOrDomain,
timeout: 0,
// @ts-ignore TODO: revisit once log-grouping has more implementations
}, (_log) => {
log = _log
Expand Down
1 change: 1 addition & 0 deletions packages/driver/types/internal-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ declare namespace Cypress {
(action: 'clear:cookies', fn: () => void)
(action: 'cross:origin:cookies', fn: (cookies: AutomationCookie[]) => void)
(action: 'before:stability:release', fn: () => void)
(action: 'paused', fn: (nextCommandName: string) => void)
}

interface Backend {
Expand Down

5 comments on commit b1058d9

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1058d9 Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.11.1/linux-x64/develop-b1058d9695aa6eb1a5463fbb6039cb16b2e7a955/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1058d9 Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.11.1/linux-arm64/develop-b1058d9695aa6eb1a5463fbb6039cb16b2e7a955/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1058d9 Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.11.1/darwin-x64/develop-b1058d9695aa6eb1a5463fbb6039cb16b2e7a955/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1058d9 Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.11.1/darwin-arm64/develop-b1058d9695aa6eb1a5463fbb6039cb16b2e7a955/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1058d9 Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.11.1/win32-x64/develop-b1058d9695aa6eb1a5463fbb6039cb16b2e7a955/cypress.tgz

Please sign in to comment.