Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [Multi-domain] Handle stability with delayed navigation #20642

Merged
merged 6 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/driver/cypress/fixtures/auth/delayedNavigate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button data-cy="to-localhost" onclick="setTimeout(()=>{window.location.href = 'http://localhost:3500/fixtures/auth/index.html'}, 1500)">navigate to localhost</button>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a bit easier to read the contents of these functions if we did something like a script tag that has a function that calls the setTimeout navigation

<button data-cy="to-foobar" onclick="setTimeout(()=>{window.location.href = 'http://www.foobar.com:3500/fixtures/auth/index.html'}, 1500)">navigate to foobar</button>
<button data-cy="to-idp" onclick="setTimeout(()=>{window.location.href = 'http://www.idp.com:3500/fixtures/auth/index.html'}, 1500)">navigate to idp</button>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,42 @@ describe('navigation events', { experimentalSessionSupport: true }, () => {
})
})
})

// @ts-ignore / session support is needed for visiting about:blank between tests
describe('delayed navigation', { experimentalSessionSupport: true, defaultCommandTimeout: 2000 }, () => {
it('localhost -> localhost', () => {
cy.visit('/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-localhost"]').click()
cy.get('[data-cy="login-idp"]')
})

it('localhost -> foobar, delay in', () => {
cy.visit('/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-foobar"]').click()
cy.switchToDomain('http://foobar.com:3500', () => {
cy.get('[data-cy="login-idp"]')
})
})

it('foobar -> localhost, delay out', () => {
cy.visit('/fixtures/auth/index.html')
cy.switchToDomain('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-localhost"]').click()
})

cy.get('[data-cy="login-idp"]')
})

it('foobar -> idp, delay out', () => {
cy.visit('/fixtures/auth/index.html')
cy.switchToDomain('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-idp"]').click()
})

cy.switchToDomain('http://idp.com:3500', () => {
cy.get('[data-cy="login-idp"]')
})
})
})
5 changes: 5 additions & 0 deletions packages/driver/src/multi-domain/events/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ export const handleMiscEvents = (Cypress: Cypress.Cypress, cy: $Cy) => {
Cypress.on('url:changed', (url) => {
Cypress.specBridgeCommunicator.toPrimary('url:changed', { url })
})

// Listen for any unload events in other domains, if any have unloaded we should also become unstable.
Cypress.specBridgeCommunicator.on('before:unload', () => {
cy.state('isStable', false)
})
}
17 changes: 10 additions & 7 deletions packages/runner-shared/src/event-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ export const eventManager = {
Cypress.multiDomainCommunicator.toAllSpecBridges('test:before:run:async', ...args)
})

// Inform all spec bridges that the primary domain has begun to unload.
Cypress.on('window:before:unload', () => {
Cypress.multiDomainCommunicator.toAllSpecBridges('before:unload')
})

Cypress.multiDomainCommunicator.on('window:load', ({ url }, domain) => {
// Sync stable if the expected domain has loaded.
// Only listen to window load events from the most recent secondary domain, This prevents nondeterminism in the case where we redirect to an already
Expand All @@ -535,14 +540,12 @@ export const eventManager = {
}
})

Cypress.multiDomainCommunicator.on('before:unload', (_unused, domain) => {
Cypress.multiDomainCommunicator.on('before:unload', () => {
// We specifically don't call 'cy.isStable' here because we don't want to inject another load event.
// 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) {
// Unstable is unstable regardless of where it initiated from.
cy.state('isStable', false)
}
// Unstable is unstable regardless of where it initiated from.
cy.state('isStable', false)
// Re-broadcast to any other specBridges.
Cypress.multiDomainCommunicator.toAllSpecBridges('before:unload')
})

Cypress.multiDomainCommunicator.on('expect:domain', (domain) => {
Expand Down