Testcafe: Cannot close the window because it does not have a parent. The parent window was closed or you are attempting to close the root browser window where tests were launched.

Created on 1 Oct 2020  ·  2Comments  ·  Source: DevExpress/testcafe

What is your Test Scenario?


I would like to open a new window with a different application under different url. Sign in there and do some actions then return to the parent window to continue my test.

As an example the code below uses github as a platform to sign in.

What is the Current behavior?


The test fails because the window cannot be closed.

1) Cannot close the window because it does not have a parent. The parent window was closed or
      you are attempting to close the root browser window where tests were launched.

What is the Expected behavior?

The test should succeed and close the recently opened window.

What is your web application and your TestCafe test code?


Your complete test code (or attach your test files):

import { Selector, t } from 'testcafe';

const GITHUB_LOGIN = 'YOUR GITHUB LOGIN';
const GITHUB_PASSWORD = 'YOUR GITHUB PASSWORD';

export const resize = () => t.resizeWindow(1366, 700);

const getWindowUrl = () => t.eval(() => document.documentURI) as Promise<string>;

async function openAsNewWindow(windowUrl: string) {
  const parentUrl = await getWindowUrl();

  const childWindow = await t.openWindow(windowUrl);
  const childUrl = await getWindowUrl();

  await t.expect(childUrl).eql(windowUrl);

  return async () => {
    await t.closeWindow(childWindow);
    await t.expect(getWindowUrl()).eql(parentUrl);
  };
}

fixture('New window')
  .page('https://github.com/join?source=login')
  .beforeEach(resize);

test.only('should open a new window', async () => {
  await t.typeText(Selector('input[type="text"][name="user[login]"]'), 'TestCafe');

  const closeWindow = await openAsNewWindow('https://github.com/login');

  await t
    .typeText(Selector('input[type="text"][name="login"]'), GITHUB_LOGIN)
    .typeText(Selector('input[type="password"][name="password"]'), GITHUB_PASSWORD)
    .click(Selector('input[type="submit"][value="Sign in"]'));

  await closeWindow();

  await t.click(Selector('input[type="submit"][value="Sign in"]'));
});


Your complete test report:

15:32 $ npm run test

> [email protected] test /Users/john/Desktop/Dev/myApp/e2e
> npm run clean && npm run env && testcafe chrome src -S -s screenshots


> [email protected] clean /Users/john/Desktop/Dev/myApp/e2e
> rm -rf ./screenshots && mkdir screenshots


> [email protected] env /Users/john/Desktop/Dev/myApp/e2e
> rm -rf ./.env && cp ./.env.dev ./.env

 Running tests in:
 - Chrome 85.0.4183.121 / macOS 10.14.6

New window
 ✖ should open a new window
  (screenshots:
 /Users/john/Desktop/Dev/myApp/e2e/screenshots/2020-10-01_15-32-55/test-1/Chrome_85.0.4183.121_macOS_10.14.6/errors/1.png)

   1) Cannot close the window because it does not have a parent. The parent window was closed or
      you are attempting to close the root browser window where tests were launched.

      Browser: Chrome 85.0.4183.121 / macOS 10.14.6
      Screenshot:

   /Users/john/Desktop/Dev/myApp/e2e/screenshots/2020-10-01_15-32-55/test-1/Chrome_85.0.4183.121_macOS_10.14.6/errors/1.png

         19 |    const pageWindowUrl = await getWindowUrl();
         20 |
         21 |    await t.expect(pageWindowUrl).eql(loginUrl);
         22 |
         23 |    return async () => {
       > 24 |      await t.closeWindow(pageWindow);
         25 |
         26 |      const currentMainWindowUrl = await getWindowUrl();
         27 |      await t.expect(currentMainWindowUrl).eql(mainWindowUrl);
         28 |    };
         29 |  }

         at PageObject.<anonymous>

   (/Users/john/Desktop/Dev/myApp/e2e/src/PageObject.ts:24:15)
         at <anonymous>

   (/Users/john/Desktop/Dev/myApp/e2e/src/PageObject.ts:7:71)
         at __awaiter

   (/Users/john/Desktop/Dev/myApp/e2e/src/PageObject.ts:3:12)
         at <anonymous>

   (/Users/john/Desktop/Dev/myApp/e2e/src/PageObject.ts:23:23)
         at Object.<anonymous>

   (/Users/john/Desktop/Dev/myApp/e2e/src/myTest.spec.ts:47:9)
         at fulfilled
      (/Users/john/Desktop/Dev/myApp/e2e/src/myTest.spec.ts:4:58)



 1/1 failed (34s)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `npm run clean && npm run env && testcafe chrome src -S -s screenshots`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/john/.npm/_logs/2020-10-01T13_33_33_023Z-debug.log

Steps to Reproduce:

  1. Use your own login / password to be able to login to github in the test.

The problem doesn't occur if a form is not submitted in the freshly opened window.
Is there anything specific that could break TestCafe window management mechanism?

Your Environment details:

  • testcafe version: 1.9.3
  • node.js version: v12.18.3
  • command-line arguments: testcafe chrome src -S -s screenshots
  • browser name and version: Chrome 85.0.4183.121
  • platform and version: macOS 10.14.6
  • other: n/a
window management bug

All 2 comments

Hello. Thank you for your input. I was able to reproduce the issue.

I examined our code, and it looks like your assumption is correct. The issue occurs only after the form is submitted in the child window. We'll try to fix the issue in the near future.

@Farfurix @AlexKamaev Many thanks guys for fixing the bug!

Was this page helpful?
0 / 5 - 0 ratings