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

TestCafé hangs & fails when a button click triggers a download in a separate browser window #6242

Closed
mhanci92 opened this issue May 20, 2021 · 2 comments
Assignees
Labels
FREQUENCY: level 2 SYSTEM: window management TYPE: bug The described behavior is considered as wrong (bug).

Comments

@mhanci92
Copy link

What is your Test Scenario?

As part of a test, we click on a button which leads to a document being downloaded. The download url is being supplied by our backend, and the frontend basically calls a window.open(documentUrl, "_blank") on it. TestCafe opens a blank window (about:blank), where the download is being completed in.

What is the Current behavior?

When a button is clicked that triggers a download, TestCafè opens a separate browser window and seems not to be able to switch to this download-window. Sooner or later the test runs into an error with the message "Cannot switch to the window.".

What is the Expected behavior?

TestCafé is able to continue with the test after clicking a button that triggers a download in a separate browser window.

What is your web application and your TestCafe test code?

A minimal example to reproduce the mentioned problem:

The HTML-Code:

<html>
 <head>
   <script>
     function download() {
       window.open("testcafe-download.zip");
     }
   </script>
 </head>
 <body>
   <h1>TestCafe Download</h1>
   <button onclick="javascript:download();">Download Test File</button>
 </body>
</html>

The TestCafé/TypeScript code to reproduce the problem:

import { Selector, ClientFunction } from "testcafe";

fixture`My download tests`.page("file://C:/Users/path/to/testcafe-test.html");

test("First download test", async (t) => {
  const downloadButton = Selector("button");

  await t.expect(Selector("button").exists).ok("Seems like the page is missing key components!");
  await t.click(downloadButton);
});

The TestCafé/TypeScript code including a workaround that I came up with:

import { Selector, ClientFunction } from "testcafe";

fixture`My download tests`.page("file://C:/Users/path/to/testcafe-test.html").beforeEach(async (t) => {
  console.log("Another test starts..");
});

const overrideWindowOpen = ClientFunction(() => {
  // @ts-ignore
  window.open = function (url) {
    // @ts-ignore
    window.__lastWindowOpenUrl = url;
  };
});

// @ts-ignore
const getLastWindowOpenUrl = ClientFunction(() => window.__lastWindowOpenUrl);

test("First download test", async (t) => {
  const downloadButton = Selector("button");

  await t.expect(Selector("button").exists).ok("Seems like the page is missing key components!");
  await overrideWindowOpen();
  await t.click(downloadButton);
  const downloadUrl = await getLastWindowOpenUrl();
  await t.navigateTo(downloadUrl); // navigate to the download-url within the same browser window to trigger the download in there
});

Calling the ClientFunction "overrideWindowOpen" right before clicking the button solved the issue for me as a workaround.

Steps to Reproduce:

  1. Save the given HTML code as well as zip folder with the name "testcafe-download.zip" together in one folder
  2. Make sure that the page() call in the TestCafé/TypeScript code points correctly to the HTML file saved locally
  3. Run your test command testcafe chrome src/to/my/testfile.ts (has to be adjusted accordingly)
  4. See the blank window that pops up in which the download completes. TestCafé hangs from there and can not finish the test properly.

Your Environment details:

  • testcafe version: 1.14.2
  • node.js version: 14.16.1
  • command-line arguments: testcafe chrome src/to/my/testfile.ts
  • browser name and version: Chrome 90.0.4430.93
  • platform and version: Windows 10
@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label May 20, 2021
@mhanci92 mhanci92 changed the title TestCafé hangs & fails when a button click triggers a download via window.open TestCafé hangs & fails when a button click triggers a download in a separate browser window May 20, 2021
@AlexKamaev AlexKamaev added SYSTEM: window management FREQUENCY: level 1 TYPE: bug The described behavior is considered as wrong (bug). and removed STATE: Need response An issue that requires a response or attention from the team. labels May 21, 2021
@maksnester
Copy link

maksnester commented Jul 28, 2021

I also caught the same issue when upgraded testcafe from "1.7.0" to "1.15.0". I checked what's different by running testcafe with --live flag and in 1.15 a new window is opened to download a file and it hangs the test:

new-window-for-downloading

While in 1.7.0 no new browser windows are opened, the downloaded file appears at the bottom of the browser window and the same test doesn't fail.

What happens in common chrome browser not controlled by testcafe – a new tab is opened to display that pdf file.

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Jul 28, 2021
@AlexanderMoiseev
Copy link
Contributor

Hello,

Thank you for your input. We have reproduced the problem and need some time to investigate it, please stay tuned.

@need-response-app need-response-app bot removed the STATE: Need response An issue that requires a response or attention from the team. label Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FREQUENCY: level 2 SYSTEM: window management TYPE: bug The described behavior is considered as wrong (bug).
Projects
None yet
Development

No branches or pull requests

4 participants