-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Wrap native methods in secondary domain (#17303)
- Loading branch information
1 parent
66d947d
commit 700636a
Showing
6 changed files
with
141 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 43 additions & 18 deletions
61
packages/driver/cypress/integration/e2e/multidomain_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,48 @@ | ||
// FIXME: Skip this for now since it's flaky | ||
it.skip('verifies initial implementation of sibling iframe and switchToDomain', (done) => { | ||
top.addEventListener('message', (event) => { | ||
if (event.data && event.data.textFromParagraph !== undefined) { | ||
expect(event.data.host).to.equal('127.0.0.1:3501') | ||
expect(event.data.textFromParagraph).to.equal('From a secondary domain with window.Cypress') | ||
done() | ||
// FIXME: Skip these for now since they're flaky | ||
describe.skip('multidomain', () => { | ||
const expectTextMessage = (text, done) => { | ||
const onMessage = (event) => { | ||
if (event.data && event.data.queriedText !== undefined) { | ||
expect(event.data.host).to.equal('127.0.0.1:3501') | ||
expect(event.data.queriedText).to.equal(text) | ||
|
||
top.removeEventListener('message', onMessage) | ||
|
||
done() | ||
} | ||
} | ||
}, false) | ||
|
||
cy.viewport(900, 300) | ||
cy.visit('/fixtures/multidomain.html') | ||
// @ts-ignore | ||
cy.anticipateMultidomain() | ||
cy.get('a').click() | ||
// @ts-ignore | ||
cy.switchToDomain('127.0.0.1:3501', () => { | ||
|
||
top.addEventListener('message', onMessage, false) | ||
} | ||
|
||
beforeEach(() => { | ||
cy.visit('/fixtures/multidomain.html') | ||
// @ts-ignore | ||
cy.anticipateMultidomain() | ||
cy.get('a').click() | ||
}) | ||
|
||
it('runs synchronous commands in secondary domain', (done) => { | ||
expectTextMessage('From a secondary domain', done) | ||
|
||
// @ts-ignore | ||
cy.switchToDomain('127.0.0.1:3501', () => { | ||
// @ts-ignore | ||
cy.now('get', '[data-cy="dom-check"]').then(($el) => { | ||
top.postMessage({ host: location.host, queriedText: $el.text() }, '*') | ||
}) | ||
}) | ||
}) | ||
|
||
it('sets up window.Cypress in secondary domain', (done) => { | ||
expectTextMessage('Has window.Cypress', done) | ||
|
||
// @ts-ignore | ||
cy.now('get', 'p').then(($el) => { | ||
top.postMessage({ host: location.host, textFromParagraph: $el.text() }, '*') | ||
cy.switchToDomain('127.0.0.1:3501', () => { | ||
// @ts-ignore | ||
cy.now('get', '[data-cy="cypress-check"]').then(($el) => { | ||
top.postMessage({ host: location.host, queriedText: $el.text() }, '*') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const { registerFetch } = require('unfetch') | ||
const $selection = require('../dom/selection') | ||
|
||
export const create = (state, config, focused, snapshots) => { | ||
const wrapNativeMethods = function (contentWindow) { | ||
try { | ||
// return null to trick contentWindow into thinking | ||
// its not been iframed if modifyObstructiveCode is true | ||
if (config('modifyObstructiveCode')) { | ||
Object.defineProperty(contentWindow, 'frameElement', { | ||
get () { | ||
return null | ||
}, | ||
}) | ||
} | ||
|
||
contentWindow.HTMLElement.prototype.focus = function (focusOption) { | ||
return focused.interceptFocus(this, contentWindow, focusOption) | ||
} | ||
|
||
contentWindow.HTMLElement.prototype.blur = function () { | ||
return focused.interceptBlur(this) | ||
} | ||
|
||
contentWindow.SVGElement.prototype.focus = function (focusOption) { | ||
return focused.interceptFocus(this, contentWindow, focusOption) | ||
} | ||
|
||
contentWindow.SVGElement.prototype.blur = function () { | ||
return focused.interceptBlur(this) | ||
} | ||
|
||
contentWindow.HTMLInputElement.prototype.select = function () { | ||
return $selection.interceptSelect.call(this) | ||
} | ||
|
||
contentWindow.document.hasFocus = function () { | ||
return focused.documentHasFocus.call(this) | ||
} | ||
|
||
const cssModificationSpy = function (original, ...args) { | ||
snapshots.onCssModified(this.href) | ||
|
||
return original.apply(this, args) | ||
} | ||
|
||
const { insertRule } = contentWindow.CSSStyleSheet.prototype | ||
const { deleteRule } = contentWindow.CSSStyleSheet.prototype | ||
|
||
contentWindow.CSSStyleSheet.prototype.insertRule = _.wrap(insertRule, cssModificationSpy) | ||
contentWindow.CSSStyleSheet.prototype.deleteRule = _.wrap(deleteRule, cssModificationSpy) | ||
|
||
if (config('experimentalFetchPolyfill')) { | ||
// drop "fetch" polyfill that replaces it with XMLHttpRequest | ||
// from the app iframe that we wrap for network stubbing | ||
contentWindow.fetch = registerFetch(contentWindow) | ||
// flag the polyfill to test this experimental feature easier | ||
state('fetchPolyfilled', true) | ||
} | ||
} catch (error) {} // eslint-disable-line no-empty | ||
} | ||
|
||
return { | ||
wrapNativeMethods, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters