Skip to content

Commit

Permalink
skip injection for non-chromium browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
mschile committed Apr 16, 2024
1 parent 446d5ea commit 49b6a7d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/driver/cypress/e2e/issues/1244.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ describe('issue 1244', () => {

// not all pages that get unloaded during this spec have getCounters()
if (win.location.href.includes('issue-1244.html')) {
// expect(win.getCounters()).to.deep.equal({ getCounter: 0, setCounter: 0 })
expect(win.getCounters()).to.deep.equal({ getCounter: 0, setCounter: 0 })
}
})
})
})

for (const [el, target, action] of [['button', 'form', 'submit']]) {
for (const [el, target, action] of [['button', 'form', 'submit'], ['a', 'a', 'click']]) {
// <form> submit, <a> click
// TODO(webkit): fix+unskip for webkit release, or make skip more specific (only 4 of these generated tests fail in webkit)
describe(`<${target}> ${action}`, { browser: '!webkit', retries: 0 }, () => {
describe(`<${target}> ${action}`, { browser: '!webkit' }, () => {
it('correctly redirects when target=_top with target.target =', () => {
cy.get(`${el}.setTarget`).click()
cy.get('#dom').should('contain', 'DOM')
Expand Down
6 changes: 5 additions & 1 deletion packages/proxy/lib/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type { Request, Response } from 'express'
import type { RemoteStates } from '@packages/server/lib/remote_states'
import type { CookieJar, SerializableAutomationCookie } from '@packages/server/lib/util/cookies'
import type { ResourceTypeAndCredentialManager } from '@packages/server/lib/util/resourceTypeAndCredentialManager'
import type { ProtocolManagerShape } from '@packages/types'
import type { FoundBrowser, ProtocolManagerShape } from '@packages/types'
import type Protocol from 'devtools-protocol'
import type { ServiceWorkerClientEvent } from './util/service-worker-manager'

Expand Down Expand Up @@ -101,6 +101,7 @@ export type ServerCtx = Readonly<{
socket: CyServer.Socket
request: any
serverBus: EventEmitter
getCurrentBrowser: () => FoundBrowser
}>

const READONLY_MIDDLEWARE_KEYS: (keyof HttpMiddlewareThis<{}>)[] = [
Expand Down Expand Up @@ -272,6 +273,7 @@ export class Http {
middleware: HttpMiddlewareStacks
netStubbingState: NetStubbingState
preRequests: PreRequests = new PreRequests()
getCurrentBrowser: () => FoundBrowser
request: any
socket: CyServer.Socket
serverBus: EventEmitter
Expand All @@ -296,6 +298,7 @@ export class Http {
this.serverBus = opts.serverBus
this.resourceTypeAndCredentialManager = opts.resourceTypeAndCredentialManager
this.getCookieJar = opts.getCookieJar
this.getCurrentBrowser = opts.getCurrentBrowser

if (typeof opts.middleware === 'undefined') {
this.middleware = defaultMiddleware
Expand Down Expand Up @@ -361,6 +364,7 @@ export class Http {
this.preRequests.removePendingRequest(pendingRequest)
},
protocolManager: this.protocolManager,
getCurrentBrowser: this.getCurrentBrowser,
}

const onError = (error: Error): Promise<void> => {
Expand Down
3 changes: 2 additions & 1 deletion packages/proxy/lib/http/response-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,8 @@ const MaybeInjectServiceWorker: ResponseMiddleware = function () {

span?.setAttributes({ hasServiceWorkerHeader: hasHeader })

if (!hasHeader) {
// skip if we don't have the header or we're not in chromium
if (!hasHeader || this.getCurrentBrowser().family !== 'chromium') {
span?.end()

return this.next()
Expand Down
9 changes: 6 additions & 3 deletions packages/proxy/lib/http/util/prerequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class PreRequests {
const key = `${browserPreRequest.method}-${tryDecodeURI(browserPreRequest.url)}`

// if we have a pending request to remove, we should remove it now and return
// since we don't want to add this pre-request to the pending list,
// since we don't want to proceed with this pre-request anymore. In practice,
// this typically happens when we receive a cached response while executing
// the async function to add the pre-request
if (this.checkAndRemovePendingPreRequest(browserPreRequest.requestId)) {
Expand Down Expand Up @@ -212,7 +212,7 @@ export class PreRequests {
return
}

debugVerbose('Caching pre-request %s to be matched later. %o', key, browserPreRequest.requestId)
debugVerbose('Caching pre-request %s to be matched later. %o', key, browserPreRequest)
this.pendingPreRequests.push(key, {
browserPreRequest,
cdpRequestWillBeSentTimestamp: browserPreRequest.cdpRequestWillBeSentTimestamp,
Expand Down Expand Up @@ -255,6 +255,9 @@ export class PreRequests {
return !matches
})

// if we didn't find a pre-request to remove, add it to the pending list so we can remove it later,
// this typically happens when we receive a cached response while executing
// the async function to add the pre-request
if (!removed) {
debugVerbose('No pre-request found to remove, adding to pending list: %s', requestId)
this.pendingPreRequestsToRemove.add(requestId)
Expand All @@ -274,7 +277,7 @@ export class PreRequests {

if (pendingPreRequest) {
metrics.immediatelyMatchedRequests++
ctxDebug('Incoming request %s matches known pre-request: %o', key, pendingPreRequest.browserPreRequest.requestId)
ctxDebug('Incoming request %s matches known pre-request: %o', key, pendingPreRequest.browserPreRequest)

callback({
browserPreRequest: {
Expand Down
1 change: 0 additions & 1 deletion packages/server/lib/project-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export class ProjectBase extends EE {
const [port, warning] = await this._server.open(cfg, {
getCurrentBrowser: () => this.browser,
getSpec: () => this.spec,
exit: this.options.args?.exit,
onError: this.options.onError,
onWarning: this.options.onWarning,
shouldCorrelatePreRequests: this.shouldCorrelatePreRequests,
Expand Down
11 changes: 3 additions & 8 deletions packages/server/lib/server-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,13 @@ export interface OpenServerOptions {
testingType: Cypress.TestingType
onError: any
onWarning: any
exit?: boolean
getCurrentBrowser: () => Browser
getSpec: () => FoundSpec | null
shouldCorrelatePreRequests: () => boolean
protocolManager?: ProtocolManagerShape
}

export class ServerBase<TSocket extends SocketE2E | SocketCt> {
private _middleware
private _protocolManager?: ProtocolManagerShape
protected request: Request
protected isListening: boolean
protected socketAllowed: SocketAllowed
Expand Down Expand Up @@ -214,8 +211,6 @@ export class ServerBase<TSocket extends SocketE2E | SocketCt> {
}

setProtocolManager (protocolManager: ProtocolManagerShape | undefined) {
this._protocolManager = protocolManager

this._socket?.setProtocolManager(protocolManager)
this._networkProxy?.setProtocolManager(protocolManager)
}
Expand Down Expand Up @@ -329,8 +324,6 @@ export class ServerBase<TSocket extends SocketE2E | SocketCt> {
shouldCorrelatePreRequests,
testingType,
SocketCtor,
exit,
protocolManager,
}: OpenServerOptions) {
debug('server open')
this.testingType = testingType
Expand All @@ -356,6 +349,7 @@ export class ServerBase<TSocket extends SocketE2E | SocketCt> {
remoteStates: this._remoteStates,
resourceTypeAndCredentialManager: this.resourceTypeAndCredentialManager,
shouldCorrelatePreRequests,
getCurrentBrowser,
})

if (config.experimentalSourceRewriting) {
Expand Down Expand Up @@ -443,7 +437,7 @@ export class ServerBase<TSocket extends SocketE2E | SocketCt> {
return e
}

createNetworkProxy ({ config, remoteStates, resourceTypeAndCredentialManager, shouldCorrelatePreRequests }) {
createNetworkProxy ({ config, remoteStates, resourceTypeAndCredentialManager, shouldCorrelatePreRequests, getCurrentBrowser }) {
const getFileServerToken = () => {
return this._fileServer?.token
}
Expand All @@ -461,6 +455,7 @@ export class ServerBase<TSocket extends SocketE2E | SocketCt> {
request: this.request,
serverBus: this._eventBus,
resourceTypeAndCredentialManager,
getCurrentBrowser,
})
}

Expand Down

5 comments on commit 49b6a7d

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 49b6a7d Apr 17, 2024

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/13.7.4/linux-arm64/mschile/service_worker_uncontrolled-49b6a7d0bf748ca5ec315f2ac42ad83ee3dcbeb8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 49b6a7d Apr 17, 2024

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/13.7.4/linux-x64/mschile/service_worker_uncontrolled-49b6a7d0bf748ca5ec315f2ac42ad83ee3dcbeb8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 49b6a7d Apr 17, 2024

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/13.7.4/darwin-x64/mschile/service_worker_uncontrolled-49b6a7d0bf748ca5ec315f2ac42ad83ee3dcbeb8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 49b6a7d Apr 17, 2024

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 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/13.7.4/win32-x64/mschile/service_worker_uncontrolled-49b6a7d0bf748ca5ec315f2ac42ad83ee3dcbeb8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 49b6a7d Apr 17, 2024

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/13.7.4/darwin-arm64/mschile/service_worker_uncontrolled-49b6a7d0bf748ca5ec315f2ac42ad83ee3dcbeb8/cypress.tgz

Please sign in to comment.