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

Error: "TypeError: window.location.toString is not a function" #2178

Closed
jsnanigans opened this issue Nov 7, 2019 · 6 comments
Closed

Error: "TypeError: window.location.toString is not a function" #2178

jsnanigans opened this issue Nov 7, 2019 · 6 comments

Comments

@jsnanigans
Copy link

The issue has been reproduced here:
https://github.com/jsnanigans/hammerhead_testcafe_error

What is your Test Scenario?

Speculation: A iframe is focused, the iframe sends a message to the main window, the main window closes the iframe before testcafe can focus the main window.

A Iframe is opened where the user enters some data and clicks on submit, then the iframe is redirected with 307 to where some javascript sends a postMessage to our host window which closes the iframe. After the iframe is closed and this error occurs something in the redux dispatch or in the react core breaks and the state is not updated/the app is not re-rendered.
After some debugging we found that it might have something to do with hammerhead.js internals and some focus method.
Important things to note here are that this happens if the iframe has a src (like in this example) or has no src attribute (like in our application)

What is the Current behavior?

error is thrown and prevents react from rendering

What is the Expected behavior?

the main window is focused in testcafe and continues as usual

What is your web application and your TestCafe test code?

The issue has been reproduced here:
https://github.com/jsnanigans/hammerhead_testcafe_error

Steps to Reproduce:

  1. clone repo: https://github.com/jsnanigans/hammerhead_testcafe_error
  2. run npm install
  3. run npm start to start the local server on port 3000.
  4. run testcafe -c 1 chrome tests/Sample.ts --skip-js-errors to start testcafe.
  5. when the test stops for debugging, open the console in the browser, there you will see the error.

Your Environment details:

  • testcafe version: 1.6.1
  • node.js version: v12.11.1
  • command-line arguments: testcafe -c 1 chrome tests/Sample.ts --skip-js-errors
  • browser name and version: Google Chrome Version 78.0.3904.87 (Official Build) (64-bit)
  • platform and version: macOS 10.14.6 (18G103)
@aleks-pro aleks-pro self-assigned this Nov 8, 2019
@aleks-pro
Copy link
Collaborator

Hello @jsnanigans ,

Thank you for submitting the issue. I've managed to reproduce it. We will research it and update this thread as soon as we have any results.

@aleks-pro aleks-pro removed their assignment Nov 8, 2019
@miherlosev miherlosev transferred this issue from DevExpress/testcafe Nov 11, 2019
@LavrovArtem LavrovArtem added this to the Sprint #46 milestone Nov 20, 2019
@LavrovArtem LavrovArtem modified the milestones: Sprint #46, Planned Dec 9, 2019
@LavrovArtem LavrovArtem added this to Need research in Categorization Feb 13, 2020
@LavrovArtem LavrovArtem removed this from the Planned milestone Feb 26, 2020
@LavrovArtem
Copy link
Contributor

I've researched this issue and created a simplified example. The problem occurs when the focus method is called on some element from a removed iframe.

<body>
<script>
    var iframe = document.createElement('iframe');

    iframe.addEventListener('load', function () {
        var body = iframe.contentDocument.body;

        document.body.removeChild(iframe);
        body.focus();
    });

    document.body.appendChild(iframe);
</script>
VM2693 hammerhead.js:23480 Uncaught TypeError: window.location.toString is not a function
    at getLocation (VM2693 hammerhead.js:23480)
    at get (VM2693 hammerhead.js:23508)
    at Object.getParsed (VM2693 hammerhead.js:23523)
    at Function._wrapMessage (VM2693 hammerhead.js:13646)
    at MessageSandbox.sendServiceMsg (VM2693 hammerhead.js:13766)
    at ActiveWindowTracker.makeCurrentWindowActive (VM2693 hammerhead.js:11772)
    at raiseFocusEvent (VM2693 hammerhead.js:12664)
    at FocusBlurSandbox.focus (VM2693 hammerhead.js:12730)
    at HTMLBodyElement.focus (VM2693 hammerhead.js:13011)
    at HTMLIFrameElement.<anonymous> (iframe-focus.html:10)

@LavrovArtem
Copy link
Contributor

I cannot reproduce this issue.

@Farfurix
Copy link
Contributor

#2716 (comment)

@nottoseethesun
Copy link

nottoseethesun commented Jan 4, 2022

#2716 (comment)

@Farfurix

Easier Issue Debugging

This fork I made:

https://github.com/nottoseethesun/hammerhead_testcafe_error

uses the latest testcafe (at the time of this writing) and has a couple features to make reproduction and analysis a lot more convenient (need to check the README though for a manual set-up step). It ensures that the Chrome DevTools debugger stops on the error, with live code, so that the stack can be walked backwards (there's also a stack-size setting that could be added to increase stack depth if needed).

Temporary Workaround

As long as the iframe in question is not going to be re-used (e.g., reattached to the dom), it should be fine to just simply swallow the error in a try-catch block. Note that the OP noted in the Issue Description at the top that the issue happens whether or not the iframe has an src attribute. So it seems that at least where there is no src attribute and thus nothing to be proxied, simply swallowing the error should work.

In the case of React though for example, if the iframe both has an src property (e.g., is an iframe that needs to be proxied by hammerhead) and also is being only temporarily detached for the purpose of a re-render from the virtual-dom, then that iframe would not have the test scripts injected into it via hammerhead and tests could be skipped (doc).

So, separately, to unblock myself I am going to try to workaround by wrapping the code where location.toString is invoked in the case where there is no browsing context, in a try-catch block, with a console.warn (source code of the function in question) to note the condition in case it does prove to be causing skipped tests as noted above.

The minified file in question is ./node_modules/testcafe-hammerhead/lib/client/hammerhead.min.js and the function is function re, around line 254 under a Chrome DevTools pretty-print.

The patched file can be copied over to replace the original at npm/yarn postinstall, which will be reliable as long as the package.json file pegs the version of testcafe (well, might need to peg hammerhead actually; we'll see).

@miherlosev
Copy link
Contributor

Hi @nottoseethesun

Thank you for the shared information. We will investigate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants