Skip to content

Commit

Permalink
chore: adding viewport:changed to protocol (#26508)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile authored Apr 18, 2023
1 parent e9e46a3 commit a11a376
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 7 deletions.
8 changes: 2 additions & 6 deletions packages/app/src/runner/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getAutIframeModel } from '.'
import { handlePausing } from './events/pausing'
import { addTelemetryListeners } from './events/telemetry'
import { telemetry } from '@packages/telemetry/src/browser'
import { addCaptureProtocolListeners } from './events/capture-protocol'

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

Expand Down Expand Up @@ -462,6 +463,7 @@ export class EventManager {

_addListeners () {
addTelemetryListeners(Cypress)
addCaptureProtocolListeners(Cypress)

Cypress.on('message', (msg, data, cb) => {
this.ws.emit('client:request', msg, data, cb)
Expand Down Expand Up @@ -503,7 +505,6 @@ export class EventManager {
this._interceptStudio(displayProps)

this.reporterBus.emit('reporter:log:add', displayProps)
Cypress.backend('protocol:command:log:added', displayProps)
})

Cypress.on('log:changed', (log) => {
Expand All @@ -517,7 +518,6 @@ export class EventManager {
this._interceptStudio(displayProps)

this.reporterBus.emit('reporter:log:state:changed', displayProps)
Cypress.backend('protocol:command:log:changed', displayProps)
})

// TODO: MOVE BACK INTO useEventManager. Verify this works
Expand Down Expand Up @@ -620,8 +620,6 @@ export class EventManager {
})
}

await Cypress.backend('protocol:test:before:run:async', attributes)

Cypress.primaryOriginCommunicator.toAllSpecBridges('test:before:run:async', ...args)
})

Expand All @@ -631,8 +629,6 @@ export class EventManager {
if (this.studioStore.isOpen && attributes.state !== 'passed') {
this.studioStore.testFailed()
}

Cypress.backend('protocol:test:after:run', attributes)
})

handlePausing(this.getCypress, this.reporterBus)
Expand Down
33 changes: 33 additions & 0 deletions packages/app/src/runner/events/capture-protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const addCaptureProtocolListeners = (Cypress: Cypress.Cypress) => {
Cypress.on('log:added', (log) => {
const displayProps = Cypress.runner.getDisplayPropsForLog(log)

Cypress.backend('protocol:command:log:added', displayProps)
})

Cypress.on('log:changed', (log) => {
const displayProps = Cypress.runner.getDisplayPropsForLog(log)

Cypress.backend('protocol:command:log:changed', displayProps)
})

Cypress.on('viewport:changed', (viewport) => {
const timestamp = performance.timeOrigin + performance.now()

Cypress.backend('protocol:viewport:changed', {
viewport: {
width: viewport.viewportWidth,
height: viewport.viewportHeight,
},
timestamp,
})
})

Cypress.on('test:before:run:async', async (attributes) => {
await Cypress.backend('protocol:test:before:run:async', attributes)
})

Cypress.on('test:after:run', (attributes) => {
Cypress.backend('protocol:test:after:run', attributes)
})
}
2 changes: 1 addition & 1 deletion packages/app/src/runner/events/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { telemetry } from '@packages/telemetry/src/browser'

export const addTelemetryListeners = (Cypress) => {
export const addTelemetryListeners = (Cypress: Cypress.Cypress) => {
Cypress.on('test:before:run', (attributes, test) => {
// we emit the 'test:before:run' events within various driver tests
try {
Expand Down
12 changes: 12 additions & 0 deletions packages/driver/types/internal-types-lite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
// All of the types needed by packages/app, without any of the additional APIs used in the driver only

declare namespace Cypress {
interface Cypress {
runner: any
}

interface Actions {
(action: 'internal:window:load', fn: (details: InternalWindowLoadDetails) => void)
(action: 'net:stubbing:event', frame: any)
Expand All @@ -15,6 +19,14 @@ declare namespace Cypress {
(action: 'after:screenshot', config: {})
}

interface Backend {
(task: 'protocol:command:log:added', log: any): Promise<void>
(task: 'protocol:command:log:changed', log: any): Promise<void>
(task: 'protocol:viewport:changed', input: any): Promise<void>
(task: 'protocol:test:before:run:async', attributes: any): Promise<void>
(task: 'protocol:test:after:run', attributes: any): Promise<void>
}

interface cy {
/**
* If `as` is chained to the current command, return the alias name used.
Expand Down
8 changes: 8 additions & 0 deletions packages/server/lib/cloud/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ class ProtocolManagerImpl implements ProtocolManager {

this.protocol?.commandLogChanged(log)
}

viewportChanged (input: any): void {
if (!this.protocolEnabled()) {
return
}

this.protocol?.viewportChanged(input)
}
}

export default ProtocolManagerImpl
2 changes: 2 additions & 0 deletions packages/server/lib/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ export class SocketBase {
return this.protocolManager?.commandLogAdded(args[0])
case 'protocol:command:log:changed':
return this.protocolManager?.commandLogChanged(args[0])
case 'protocol:viewport:changed':
return this.protocolManager?.viewportChanged(args[0])
case 'telemetry':
return (telemetry.exporter() as OTLPTraceExporterCloud)?.send(args[0], () => {}, (err) => {
debug('error exporting telemetry data from browser %s', err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const AppCaptureProtocol = class {
this.beforeTest = this.beforeTest.bind(this)
this.commandLogAdded = this.commandLogAdded.bind(this)
this.commandLogChanged = this.commandLogChanged.bind(this)
this.viewportChanged = this.viewportChanged.bind(this)
}

connectToBrowser (cdpClient) {
Expand All @@ -22,6 +23,7 @@ const AppCaptureProtocol = class {
beforeTest (test) {}
commandLogAdded (log) {}
commandLogChanged (log) {}
viewportChanged (input) {}
}

module.exports = {
Expand Down
16 changes: 16 additions & 0 deletions packages/server/test/unit/cloud/protocol_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,20 @@ describe('lib/cloud/protocol', () => {

expect(protocol.commandLogChanged).to.be.calledWith(log)
})

it('should be able to handle changing the viewport', () => {
sinon.stub(protocol, 'viewportChanged')

const input = {
viewport: {
width: 100,
height: 200,
},
timestamp: 1234,
}

protocolManager.viewportChanged(input)

expect(protocol.viewportChanged).to.be.calledWith(input)
})
})
2 changes: 2 additions & 0 deletions packages/types/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface AppCaptureProtocolInterface {
afterTest(test: Record<string, any>): void
commandLogAdded (log: any): void
commandLogChanged (log: any): void
viewportChanged (input: any): void
}

export interface ProtocolManager {
Expand All @@ -35,4 +36,5 @@ export interface ProtocolManager {
afterTest(test: Record<string, any>): void
commandLogAdded (log: any): void
commandLogChanged (log: any): void
viewportChanged (input: any): void
}

4 comments on commit a11a376

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a11a376 Apr 18, 2023

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.10.1/linux-arm64/feat/protocol-a11a376a54ca2a9d935a8601265edc3533f6906f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a11a376 Apr 18, 2023

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.10.1/linux-x64/feat/protocol-a11a376a54ca2a9d935a8601265edc3533f6906f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a11a376 Apr 18, 2023

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.10.1/darwin-x64/feat/protocol-a11a376a54ca2a9d935a8601265edc3533f6906f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on a11a376 Apr 18, 2023

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.10.1/darwin-arm64/feat/protocol-a11a376a54ca2a9d935a8601265edc3533f6906f/cypress.tgz

Please sign in to comment.