Skip to content

Commit

Permalink
Merge branch 'feature-multidomain' into update_switch_to_domain_signa…
Browse files Browse the repository at this point in the history
…ture
  • Loading branch information
mjhenkes authored Mar 23, 2022
2 parents 5a7a365 + c7aa180 commit 331cf39
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { assertLogLength } from '../../../../support/utils'

// @ts-ignore / session support is needed for visiting about:blank between tests
context('log', { experimentalSessionSupport: true }, () => {
let logs: any = []
let lastTestLogId = ''

beforeEach(() => {
logs = []

cy.on('log:added', (attrs, log) => {
logs.push(log)
})

cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="dom-link"]').click()
})

it('logs in primary and secondary domains', () => {
cy.switchToDomain('http://foobar.com:3500', () => {
const afterLogAdded = new Promise<void>((resolve) => {
const listener = (attrs) => {
if (attrs.message === 'test log in multi-domain') {
expect(attrs.message).to.eq('test log in multi-domain')
cy.removeListener('log:added', listener)
resolve(attrs.id)
}
}

cy.on('log:added', listener)
})

cy.log('test log in multi-domain')
cy.wrap(afterLogAdded)
}).then((id) => {
lastTestLogId = id
// Verify the log is also fired in the primary domain.
expect(logs[6].get('message')).to.eq('test log in multi-domain')
// Verify the log has the same ID as was generated in the cross-origin
expect(logs[6].get('id')).to.equal(id)
assertLogLength(logs, 11)
})
})

it('has a different id in a second test', () => {
cy.switchToDomain('http://foobar.com:3500', () => {
const afterLogAdded = new Promise<void>((resolve) => {
const listener = (attrs) => {
if (attrs.message === 'test log in multi-domain') {
expect(attrs.message).to.eq('test log in multi-domain')
cy.removeListener('log:added', listener)
resolve(attrs.id)
}
}

cy.on('log:added', listener)
})

cy.log('test log in multi-domain')
cy.wrap(afterLogAdded)
}).then((id) => {
// Verify the log is also fired in the primary domain.
expect(logs[6].get('message')).to.eq('test log in multi-domain')
// Verify the log has the same ID as was generated in the cross-origin
expect(logs[6].get('id')).to.equal(id)
expect(logs[6].get('id')).to.not.equal(lastTestLogId)
assertLogLength(logs, 12)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,6 @@ context('multi-domain misc', { experimentalSessionSupport: true }, () => {
})
})

it('.log()', () => {
cy.switchToDomain('http://foobar.com:3500', () => {
const afterLogAdded = new Promise<void>((resolve) => {
cy.once('log:added', () => {
resolve()
})
})

cy.log('test log in multi-domain')
cy.wrap(afterLogAdded)
})
})

it('.pause()', () => {
cy.switchToDomain('http://foobar.com:3500', () => {
const afterPaused = new Promise<void>((resolve) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ describe('multi-domain yields', { experimentalSessionSupport: true }, () => {

it('throws if subject cannot be serialized and is accessed synchronously', (done) => {
cy.on('fail', (err) => {
assertLogLength(logs, 6)
expect(logs[5].get('error')).to.eq(err)
assertLogLength(logs, 7)
expect(logs[6].get('error')).to.eq(err)
expect(err.message).to.include('`cy.switchToDomain()` could not serialize the subject due to one of its properties not being supported by the structured clone algorithm.')

done()
Expand Down Expand Up @@ -112,8 +112,8 @@ describe('multi-domain yields', { experimentalSessionSupport: true }, () => {

it('throws if subject cannot be serialized and is accessed', (done) => {
cy.on('fail', (err) => {
assertLogLength(logs, 6)
expect(logs[5].get('error')).to.eq(err)
assertLogLength(logs, 8)
expect(logs[7].get('error')).to.eq(err)
expect(err.message).to.include('`cy.switchToDomain()` could not serialize the subject due to one of its properties not being supported by the structured clone algorithm.')

done()
Expand All @@ -128,8 +128,8 @@ describe('multi-domain yields', { experimentalSessionSupport: true }, () => {

it('throws if an object contains a function', (done) => {
cy.on('fail', (err) => {
assertLogLength(logs, 6)
expect(logs[5].get('error')).to.eq(err)
assertLogLength(logs, 8)
expect(logs[7].get('error')).to.eq(err)
expect(err.message).to.include('`cy.switchToDomain()` could not serialize the subject due to one of its properties not being supported by the structured clone algorithm.')

done()
Expand All @@ -148,8 +148,8 @@ describe('multi-domain yields', { experimentalSessionSupport: true }, () => {

it('throws if an object contains a symbol', (done) => {
cy.on('fail', (err) => {
assertLogLength(logs, 6)
expect(logs[5].get('error')).to.eq(err)
assertLogLength(logs, 8)
expect(logs[7].get('error')).to.eq(err)
expect(err.message).to.include('`cy.switchToDomain()` could not serialize the subject due to one of its properties not being supported by the structured clone algorithm.')

done()
Expand All @@ -165,8 +165,8 @@ describe('multi-domain yields', { experimentalSessionSupport: true }, () => {

it('throws if an object is a function', (done) => {
cy.on('fail', (err) => {
assertLogLength(logs, 6)
expect(logs[5].get('error')).to.eq(err)
assertLogLength(logs, 8)
expect(logs[7].get('error')).to.eq(err)
expect(err.message).to.include('`cy.switchToDomain()` could not serialize the subject due to functions not being supported by the structured clone algorithm.')

done()
Expand All @@ -185,8 +185,8 @@ describe('multi-domain yields', { experimentalSessionSupport: true }, () => {

it('throws if an object is a symbol', (done) => {
cy.on('fail', (err) => {
assertLogLength(logs, 6)
expect(logs[5].get('error')).to.eq(err)
assertLogLength(logs, 8)
expect(logs[7].get('error')).to.eq(err)
expect(err.message).to.include('`cy.switchToDomain()` could not serialize the subject due to symbols not being supported by the structured clone algorithm.')

done()
Expand All @@ -204,8 +204,8 @@ describe('multi-domain yields', { experimentalSessionSupport: true }, () => {

cy.on('fail', (err) => {
if (!isChromium) {
assertLogLength(logs, 6)
expect(logs[5].get('error')).to.eq(err)
assertLogLength(logs, 8)
expect(logs[7].get('error')).to.eq(err)
expect(err.message).to.include('`cy.switchToDomain()` could not serialize the subject due to one of its properties not being supported by the structured clone algorithm.')
}

Expand Down
2 changes: 2 additions & 0 deletions packages/driver/src/cy/multi-domain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createUnserializableSubjectProxy } from './unserializable_subject_proxy
import { serializeRunnable } from './util'
import { preprocessConfig, preprocessEnv, syncConfigToCurrentDomain, syncEnvToCurrentDomain } from '../../util/config'
import { $Location } from '../../cypress/location'
import { LogUtils } from '../../cypress/log'

const reHttp = /^https?:\/\//

Expand Down Expand Up @@ -193,6 +194,7 @@ export function addCommands (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
},
config: preprocessConfig(Cypress.config()),
env: preprocessEnv(Cypress.env()),
logCounter: LogUtils.getCounter(),
})
} catch (err: any) {
const wrappedErr = $errUtils.errByPath('switchToDomain.run_domain_fn_errored', {
Expand Down
4 changes: 4 additions & 0 deletions packages/driver/src/cypress/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export const LogUtils = {
setCounter: (num) => {
return counter = num
},

getCounter: () => {
return counter
},
}

const defaults = function (state, config, obj) {
Expand Down
8 changes: 6 additions & 2 deletions packages/driver/src/multi-domain/domain_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import $errUtils from '../cypress/error_utils'
import $utils from '../cypress/utils'
import { syncConfigToCurrentDomain, syncEnvToCurrentDomain } from '../util/config'
import type { Runnable, Test } from 'mocha'
import { LogUtils } from '../cypress/log'

interface RunDomainFnOptions {
config: Cypress.Config
Expand All @@ -11,7 +12,7 @@ interface RunDomainFnOptions {
fn: string
skipConfigValidation: boolean
state: {}
isStable: boolean
logCounter: number
}

interface serializedRunnable {
Expand Down Expand Up @@ -82,12 +83,15 @@ export const handleDomainFn = (Cypress: Cypress.Cypress, cy: $Cy) => {
}

Cypress.specBridgeCommunicator.on('run:domain:fn', async (options: RunDomainFnOptions) => {
const { config, args, env, fn, state, skipConfigValidation } = options
const { config, args, env, fn, state, skipConfigValidation, logCounter } = options

let queueFinished = false

reset(state)

// Set the counter for log ids
LogUtils.setCounter(logCounter)

// @ts-ignore
window.__cySkipValidateConfig = skipConfigValidation || false

Expand Down
13 changes: 11 additions & 2 deletions packages/runner-shared/src/event-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,21 @@ export const eventManager = {

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

const crossOriginLogs = {}

Cypress.multiDomainCommunicator.on('log:added', (attrs) => {
reporterBus.emit('reporter:log:add', attrs)
// Create a new local log representation of the cross origin log.
// It will be attached to the current command.
// We also keep a reference to it to update it in the future.
crossOriginLogs[attrs.id] = Cypress.log(attrs)
})

Cypress.multiDomainCommunicator.on('log:changed', (attrs) => {
reporterBus.emit('reporter:log:state:changed', attrs)
// Retrieve the referenced log and update it.
const log = crossOriginLogs[attrs.id]

// this will trigger a log changed event for the log itself.
log?.set(attrs)
})
},

Expand Down

0 comments on commit 331cf39

Please sign in to comment.