Skip to content

Commit

Permalink
chore: Update switchToDomain signature with args key (#20722)
Browse files Browse the repository at this point in the history
* chore: update data argument to be object containing args key over being an array

* chore: remove done_reference_mismatch in error_messages as done is now removed

* fix: alias args as data going through communicator to keep common interface and exclude user serialized data

* rename data references to options in switchToDomain

* use isPlainObject to simplify conditional in validator

* refactor switchToDomain options validation to check for invalid keys in options argument over missing the args key

Co-authored-by: Matt Henkes <mjhenkes@gmail.com>
  • Loading branch information
AtofStryker and mjhenkes authored Mar 24, 2022
1 parent c7aa180 commit 90d1eb7
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 114 deletions.
10 changes: 8 additions & 2 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1912,16 +1912,22 @@ declare namespace Cypress {
* })
*/
switchToDomain(originOrDomain: string, fn: () => void): Chainable

// TODO: when we find other options to put into the 'data' argument of switchToDomain, we may want to overload this type with
// a 'data' paramater that contains all data options, including args, and one that contains all data options, excluding args.
// This will provide better typings support for whatever args is set to as opposed to an optional undefined
/**
* Enables running Cypress commands in a secondary domain
* @see https://on.cypress.io/switchToDomain
* @example
* cy.switchToDomain('example.com', [{ key: 'value' }, 'foo'], ([{ key }, foo]) => {
* cy.switchToDomain('example.com', { args: { key: 'value', foo: 'foo' } }, ({ key, foo }) => {
* expect(key).to.equal('value')
* expect(foo).to.equal('foo')
* })
*/
switchToDomain<T>(originOrDomain: string, data: T[], fn: (data: T[]) => void): Chainable
switchToDomain<T>(originOrDomain: string, options: {
args: T
}, fn: (args: T) => void): Chainable

/**
* Run a task in Node via the plugins file.
Expand Down
15 changes: 8 additions & 7 deletions cli/types/tests/cypress-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,17 +830,18 @@ namespace CypressKeyboardTests {

namespace CypressMultiDomainTests {
cy.switchToDomain('example.com', () => {})
cy.switchToDomain('example.com', [{}], (value: object[]) => {})
cy.switchToDomain('example.com', [], (value: any[]) => {})
cy.switchToDomain('example.com', [1, 'value', {}, true], (value: Array<string | number | boolean | {}>) => {})
cy.switchToDomain('example.com', ['value'], (value: string[]) => {})
cy.switchToDomain('example.com', [1], (value: number[]) => {})
cy.switchToDomain('example.com', [true], (value: boolean[]) => {})
cy.switchToDomain('example.com', { args: {}}, (value: object) => {})
cy.switchToDomain('example.com', { args: { one: 1, key: 'value', bool: true } }, (value: { one: number, key: string, bool: boolean}) => {})
cy.switchToDomain('example.com', { args: [1, 'value', true ] }, (value: Array<(number | string | boolean)>) => {})
cy.switchToDomain('example.com', { args : 'value'}, (value: string) => {})
cy.switchToDomain('example.com', { args: 1 }, (value: number) => {})
cy.switchToDomain('example.com', { args: true }, (value: boolean) => {})

cy.switchToDomain() // $ExpectError
cy.switchToDomain('example.com') // $ExpectError
cy.switchToDomain(true) // $ExpectError
cy.switchToDomain('example.com', {}) // $ExpectError
cy.switchToDomain('example.com', {}, {}) // $ExpectError
cy.switchToDomain('example.com', ['value'], (value: boolean[]) => {}) // $ExpectError
cy.switchToDomain('example.com', { args: ['value'] }, (value: boolean[]) => {}) // $ExpectError
cy.switchToDomain('example.com', {}, (value: undefined) => {}) // $ExpectError
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('basic login', { experimentalSessionSupport: true }, () => {
const login = (name) => {
cy.session(name, () => {
// Note, this assumes localhost is the primary domain, ideally we'd be able to specify this directly.
cy.switchToDomain('http://idp.com:3500', [name], ([name]) => {
cy.switchToDomain('http://idp.com:3500', { args: name }, (name) => {
cy.visit('http://www.idp.com:3500/fixtures/auth/idp.html')
cy.get('[data-cy="username"]').type(name)
cy.get('[data-cy="login"]').click()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('captures the fullPage', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
const automationStub = cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)

cy.screenshot({ capture: 'fullPage' })
Expand All @@ -36,7 +36,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('captures the runner', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
const automationStub = cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)

cy.screenshot({ capture: 'runner' })
Expand All @@ -48,7 +48,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('captures the viewport', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
const automationStub = cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)

cy.screenshot({ capture: 'viewport' })
Expand Down Expand Up @@ -80,7 +80,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('supports multiple titles', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
const automationStub = cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)

cy.screenshot()
Expand All @@ -91,7 +91,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('supports the blackout option', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)

cy.screenshot({
Expand All @@ -109,7 +109,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('supports element screenshots', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
const automationStub = cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)

cy.get('.tall-element').screenshot()
Expand All @@ -121,7 +121,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('supports screenshot retrying with appropriate naming', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
const automationStub = cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)

cy.state('runnable')._currentRetry = 2
Expand All @@ -134,7 +134,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('calls the onBeforeScreenshot callback', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)
const onBeforeScreenshot = cy.stub()

Expand All @@ -144,7 +144,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('calls the onAfterScreenshot callback', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)
const onAfterScreenshot = cy.stub()

Expand All @@ -154,7 +154,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('supports the Cypress.screenshot callbacks', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').resolves(serverResult)
const onAfterScreenshot = cy.stub()
const onBeforeScreenshot = cy.stub()
Expand All @@ -171,7 +171,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('supports pausing timers', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').returns(Cypress.Promise.delay(500, serverResult))

cy.window().then((win) => {
Expand Down Expand Up @@ -204,7 +204,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
})

it('does not pause timers when disableTimersAndAnimations is false', () => {
cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').returns(Cypress.Promise.delay(500, serverResult))

cy.window().then((win) => {
Expand Down Expand Up @@ -234,7 +234,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
expect(err.message).to.include('setTimeout error after screenshot')
})

cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').returns(Cypress.Promise.delay(100, serverResult))

cy.window().then((win) => {
Expand All @@ -259,7 +259,7 @@ context('multi-domain screenshot', { experimentalSessionSupport: true }, () => {
expect(err.docsUrl).to.deep.eq(['https://on.cypress.io/uncaught-exception-from-application'])
})

cy.switchToDomain('http://foobar.com:3500', [this.serverResult], ([serverResult]) => {
cy.switchToDomain('http://foobar.com:3500', { args: this.serverResult }, (serverResult) => {
cy.stub(Cypress, 'automation').withArgs('take:screenshot').returns(Cypress.Promise.delay(100, serverResult))

cy.window().then((win) => {
Expand Down
Loading

0 comments on commit 90d1eb7

Please sign in to comment.