Skip to content

Commit

Permalink
fix: Use post message instead of web sockets for spec bridges. (#24243)
Browse files Browse the repository at this point in the history
* fix: spec bridges no-longer create their own spec bridge but instead use post message to communicate with the primary instance.

* fix test errors

* fix tests, ignore change to cookie

* Move patch code into injection

* Code cleanup

* Fix most failing tests

* Enable patching for both spec bridges and the primary cypress instance.

* clean up

* Updates from self PR review

* Remove before each limiting spec bridges.

* rename attach function

* whoops, better call the function with the correct parameters

* Apply suggestions from code review

Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>

* Update packages/driver/src/cross-origin/communicator.ts

Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Bill Glesias <bglesias@gmail.com>

* updated test name

Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>
Co-authored-by: Bill Glesias <bglesias@gmail.com>
  • Loading branch information
3 people committed Oct 19, 2022
1 parent 8b12ec9 commit 208efbf
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 247 deletions.
40 changes: 32 additions & 8 deletions packages/app/src/runner/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ export class EventManager {
})

// Reflect back to the requesting origin the status of the 'duringUserTestExecution' state
Cypress.primaryOriginCommunicator.on('sync:during:user:test:execution', ({ specBridgeResponseEvent }, origin) => {
Cypress.primaryOriginCommunicator.toSpecBridge(origin, specBridgeResponseEvent, cy.state('duringUserTestExecution'))
Cypress.primaryOriginCommunicator.on('sync:during:user:test:execution', (_data, { origin, responseEvent }) => {
Cypress.primaryOriginCommunicator.toSpecBridge(origin, responseEvent, cy.state('duringUserTestExecution'))
})

Cypress.on('request:snapshot:from:spec:bridge', ({ log, name, options, specBridge, addSnapshot }: {
Expand All @@ -624,9 +624,13 @@ export class EventManager {
const eventID = log.get('id')

const requestSnapshot = () => {
return Cypress.primaryOriginCommunicator.toSpecBridgePromise(specBridge, 'snapshot:generate:for:log', {
name,
id: eventID,
return Cypress.primaryOriginCommunicator.toSpecBridgePromise({
origin: specBridge,
event: 'snapshot:generate:for:log',
data: {
name,
id: eventID,
},
}).then((crossOriginSnapshot) => {
const snapshot = crossOriginSnapshot.body ? crossOriginSnapshot : null

Expand Down Expand Up @@ -656,7 +660,7 @@ export class EventManager {
this.localBus.emit('expect:origin', origin)
})

Cypress.primaryOriginCommunicator.on('viewport:changed', (viewport, origin) => {
Cypress.primaryOriginCommunicator.on('viewport:changed', (viewport, { origin }) => {
const callback = () => {
Cypress.primaryOriginCommunicator.toSpecBridge(origin, 'viewport:changed:end')
}
Expand All @@ -665,7 +669,7 @@ export class EventManager {
this.localBus.emit('viewport:changed', viewport, callback)
})

Cypress.primaryOriginCommunicator.on('before:screenshot', (config, origin) => {
Cypress.primaryOriginCommunicator.on('before:screenshot', (config, { origin }) => {
const callback = () => {
Cypress.primaryOriginCommunicator.toSpecBridge(origin, 'before:screenshot:end')
}
Expand Down Expand Up @@ -710,6 +714,26 @@ export class EventManager {
},
)

/**
* Call a backend request for the requesting spec bridge since we cannot have websockets in the spec bridges.
* Return it's response.
*/
Cypress.primaryOriginCommunicator.on('backend:request', async ({ args }, { source, responseEvent }) => {
const response = await Cypress.backend(...args)

Cypress.primaryOriginCommunicator.toSource(source, responseEvent, response)
})

/**
* Call an automation request for the requesting spec bridge since we cannot have websockets in the spec bridges.
* Return it's response.
*/
Cypress.primaryOriginCommunicator.on('automation:request', async ({ args }, { source, responseEvent }) => {
const response = await Cypress.automation(...args)

Cypress.primaryOriginCommunicator.toSource(source, responseEvent, response)
})

// The window.top should not change between test reloads, and we only need to bind the message event when Cypress is recreated
// Forward all message events to the current instance of the multi-origin communicator
if (!window.top) throw new Error('missing window.top in event-manager')
Expand Down Expand Up @@ -848,7 +872,7 @@ export class EventManager {

notifyCrossOriginBridgeReady (origin) {
// Any multi-origin event appends the origin as the third parameter and we do the same here for this short circuit
Cypress.primaryOriginCommunicator.emit('bridge:ready', undefined, origin)
Cypress.primaryOriginCommunicator.emit('bridge:ready', undefined, { origin })
}

snapshotUnpinned () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ context('cy.origin navigation', { browser: '!webkit' }, () => {
})
})

it('.go()', () => {
// TODO: Investigate this flaky test.
it('.go()', { retries: 15 }, () => {
cy.visit('/fixtures/primary-origin.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()

Expand Down
11 changes: 11 additions & 0 deletions packages/driver/cypress/e2e/e2e/origin/origin.cy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
describe('cy.origin', { browser: '!webkit' }, () => {
it('successfully visits after creating 30 spec bridges', () => {
// Make ~30 spec bridges
for (let index = 0; index < 30; index++) {
cy.origin(`http://www.${index}.com:3500`, () => undefined)
}

cy.origin('http://www.app.foobar.com:3500', () => {
cy.visit('/fixtures/primary-origin.html')
})
})

it('passes viewportWidth/Height state to the secondary origin', () => {
const expectedViewport = [320, 480]

Expand Down
Loading

5 comments on commit 208efbf

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 208efbf Oct 19, 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.0/linux-x64/develop-208efbfb4e7111cc7f1043a7d1c6a3ffc582c6fb/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 208efbf Oct 19, 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.0/linux-arm64/develop-208efbfb4e7111cc7f1043a7d1c6a3ffc582c6fb/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 208efbf Oct 19, 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.0/darwin-arm64/develop-208efbfb4e7111cc7f1043a7d1c6a3ffc582c6fb/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 208efbf Oct 19, 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.0/darwin-x64/develop-208efbfb4e7111cc7f1043a7d1c6a3ffc582c6fb/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 208efbf Oct 19, 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.0/win32-x64/develop-208efbfb4e7111cc7f1043a7d1c6a3ffc582c6fb/cypress.tgz

Please sign in to comment.