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

Replace x.top, x.parent accessors with correct reference (at runtime) #5271

Closed
flotwig opened this issue Oct 2, 2019 · 4 comments · Fixed by #5273
Closed

Replace x.top, x.parent accessors with correct reference (at runtime) #5271

flotwig opened this issue Oct 2, 2019 · 4 comments · Fixed by #5273
Assignees
Labels
pkg/driver This is due to an issue in the packages/driver directory pkg/server This is due to an issue in the packages/server directory

Comments

@flotwig
Copy link
Contributor

flotwig commented Oct 2, 2019

PR: #5273

Current behavior:

window.top and window.parent references are naively corrected like so:

const topOrParentEqualityBeforeRe = /((?:window|self)(?:\.|\[['"](?:top|self)['"]\])?\s*[!=]==?\s*(?:(?:window|self)(?:\.|\[['"]))?)(top|parent)(?![\w])/g
const topOrParentEqualityAfterRe = /(top|parent)((?:["']\])?\s*[!=]==?\s*(?:window|self))/g
const topOrParentLocationOrFramesRe = /([^\da-zA-Z\(\)])?(top|parent)([.])(location|frames)/g
const jiraTopWindowGetterRe = /(!function\s*\((\w{1})\)\s*{\s*return\s*\w{1}\s*(?:={2,})\s*\w{1}\.parent)(\s*}\(\w{1}\))/g
const strip = (html) => {
return html
.replace(topOrParentEqualityBeforeRe, '$1self')
.replace(topOrParentEqualityAfterRe, 'self$2')
.replace(topOrParentLocationOrFramesRe, '$1self$3$4')
.replace(jiraTopWindowGetterRe, '$1 || $2.parent.__Cypress__$3')
}
const stripStream = () => {
return pumpify(
utf8Stream(),
replaceStream(
[
topOrParentEqualityBeforeRe,
topOrParentEqualityAfterRe,
topOrParentLocationOrFramesRe,
jiraTopWindowGetterRe,
],
[
'$1self',
'self$2',
'$1self$3$4',
'$1 || $2.parent.__Cypress__$3',
]
)
)
}

Desired behavior:

  • Replace /(.*)\.(top|parent)/ with (window.top.Cypress.resolveWindowReference($1, "$2")) using AST parser or regexes
  • window.top.Cypress.resolveWindowReference(maybeWindow, prop) is a function that checks if maybeWindow is a Window and, if so, corrects the property access to be 1 level deeper than it would've been (to skip window.top).
  • If maybeWindow is not a Window, and if maybeWindow[prop] is a Function, it will bind maybeWindow[prop] to maybeWindow and return it.
  • Else, it will return maybeWindow[prop].
@flotwig flotwig added pkg/driver This is due to an issue in the packages/driver directory pkg/server This is due to an issue in the packages/server directory labels Oct 2, 2019
@flotwig flotwig self-assigned this Oct 2, 2019
@cypress-bot cypress-bot bot added the stage: ready for work The issue is reproducible and in scope label Oct 2, 2019
@flotwig flotwig changed the title Replace x.top, x.parent accessors with correct reference (via ternary) Replace x.top, x.parent accessors with correct reference (at runtime) Oct 2, 2019
@cypress-bot cypress-bot bot added stage: work in progress and removed stage: ready for work The issue is reproducible and in scope labels Oct 2, 2019
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels May 8, 2020
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels May 11, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 11, 2020

The code for this is done in cypress-io/cypress#5273, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 20, 2020

Released in 4.6.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v4.6.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators May 20, 2020
@flotwig
Copy link
Contributor Author

flotwig commented May 20, 2020

This fix is available starting in 4.6.0 as an experiment which you can access by setting this config option in your cypress.json or elsewhere:

{
	"experimentalSourceRewriting": true
}

The fix is experimental, so there may be some situations where the this is not fixed.

If you're still this issue while setting the experimentalSourceRewriting to true in 4.6.0 - open a new issue with a reproducible example + screenshots, etc - filling out our issue template.

@cypress-io cypress-io unlocked this conversation May 20, 2020
@fderraz
Copy link

fderraz commented Oct 18, 2024

Hello,

My Cypress test fails because the home page is not loaded:
(uncaught exception)TypeError: top.getTabFrom is not a function
(uncaught exception)TypeError: top.getParamWindow is not a function

The following error originated from your application code, not from Cypress. > top.getTabFrom is not a function

I addedto the cypress.json file, the following params: “experimentalSourceRewriting”: true
Except that it doesn't fix my problem

Version Cypress: "13.15.0"

Any suggestions you may have to solve this problem, will be appreciated :)

Error

Please find enclosed my code: Test.cy.js
The following code snippet corrects the loading problem, except that it's not a relevant solution because the win.top is used everywhere in the application under Test ==> it's not possible to include it after each click.

cy.window().then((win) => {
win.top.getTabFrom = win.top[0].getTabFrom;
win.top.getParamWindow = win.top[0].getParamWindow;
});

Test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg/driver This is due to an issue in the packages/driver directory pkg/server This is due to an issue in the packages/server directory
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants