Skip to content

Commit

Permalink
chore: [Multi-domain] Clean up stability code. (#20656)
Browse files Browse the repository at this point in the history
* chore: [Multi-domain] Fix Flake when subdomain loads faster than toSpecBridge Command called.

* Add Test

* simplify test

* Loading a cross domain page won't happen until the spec bridge is active (or a timeout will happen)

* Update packages/driver/src/multi-domain/domain_fn.ts
  • Loading branch information
mjhenkes authored and mschile committed Mar 22, 2022
1 parent c33b44c commit 6178ec5
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/driver/src/cy/multi-domain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ export function addCommands (Commands, Cypress: Cypress.Cypress, cy: Cypress.cy,
hookId: state('hookId'),
hasVisitedAboutBlank: state('hasVisitedAboutBlank'),
multiDomainBaseUrl: location.origin,
isStable: state('isStable'),
},
config: preprocessConfig(Cypress.config()),
env: preprocessEnv(Cypress.env()),
isStable: state('isStable'),
})
} catch (err: any) {
const wrappedErr = $errUtils.errByPath('switchToDomain.run_domain_fn_errored', {
Expand Down
3 changes: 1 addition & 2 deletions packages/driver/src/multi-domain/cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ const onBeforeAppWindowLoad = (Cypress: Cypress.Cypress, cy: $Cy) => (autWindow:
cy.overrides.wrapNativeMethods(autWindow)

const onWindowLoadPrimary = ({ url }) => {
// If the primary domain has indicated a load event, set stability to undefined, not true since the load happened in another domain.
cy.isStable(undefined, 'primary onload')
cy.isStable(true, 'primary onload')
Cypress.emit('internal:window:load', { type: 'cross:domain', url })
}

Expand Down
14 changes: 1 addition & 13 deletions packages/driver/src/multi-domain/domain_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const handleDomainFn = (Cypress: Cypress.Cypress, cy: $Cy) => {
}

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

let queueFinished = false

Expand All @@ -109,18 +109,6 @@ export const handleDomainFn = (Cypress: Cypress.Cypress, cy: $Cy) => {
Cypress.specBridgeCommunicator.toPrimary('queue:finished', { err }, { syncGlobals: true })
})

// We specifically don't call 'cy.isStable' here because we don't want to inject another load event.
// If stability is true in this domain, that means this domain has loaded itself, in that case trust the domain and run the next command.
if (cy.state('isStable') !== true) {
if (isStable) {
// If stability is established in a different domain, set this domain to undefined
cy.state('isStable', undefined)
} else {
// If the calling domain is unstable set this domain to also be unstable. It indicates that a load even occurred prior to running this domain.
cy.state('isStable', isStable)
}
}

try {
const value = window.eval(`(${fn})`)(data)

Expand Down
4 changes: 1 addition & 3 deletions packages/runner-shared/src/event-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,7 @@ export const eventManager = {
// Only listen to window load events from the most recent secondary domain, This prevents nondeterminism in the case where we redirect to an already
// established spec bridge, but one that is not the current or next switchToDomain command.
if (cy.state('latestActiveDomain') === domain) {
// Since stability was established in another domain set stable to undefined, not true. Undefined and true are treated the same stability.ts, but
// it allows us to distinguish between a load event that ocurred in this domain and some that didn't (or the initial state)
cy.isStable(undefined, 'load')
cy.isStable(true, 'load')
// Prints out the newly loaded URL
Cypress.emit('internal:window:load', { type: 'cross:domain', url })
}
Expand Down

0 comments on commit 6178ec5

Please sign in to comment.