Home > Mobile >  Testcafe close window
Testcafe close window

Time:12-10

I have this simple page that opens a new window.

<html>
    <body>
        <button id="open">Open window</button>
    </body>

    <script>
        var windowObjectReference;

        function openRequestedPopup() {
        windowObjectReference = window.open("http://www.cnn.com/", "CNN");
        }
        document.querySelector("#open").addEventListener("click", openRequestedPopup);
    </script>
</html>

I also have this test:

import {Selector} from 'testcafe';

fixture `Close window`
    .page('http://127.0.0.1:8080/');

test('Close window', async t => {
    await t.click("#open");
    await t.switchToWindow(f => f.url.host==="www.cnn.com").closeWindow();
});

When trying to find the window based on a predicate the test fails

PS C:\work\New folder> testcafe edge .\test.js
 Running tests in:
 - Microsoft Edge 96.0.1054.43 / Windows 10

 Example page
 × Close the current window

   1) Cannot find the window specified in the action parameters.

      Browser: Microsoft Edge 96.0.1054.43 / Windows 10



 1/1 failed (5s)

I am not able to understand what should I do in order to get hold of the window oppened from js code.

How can I get the window and inspect some elements inside?

CodePudding user response:

We have fixed issues related to "Multiple browser windows" mode. Please install the current alpha version (npm i testcafe@alpha / npm i [email protected]). Also, you need to fix your predicate's host value. The actual hostname of this CNN page is edition.cnn.com (you can check it without TestCafe by observing the window.location.hostname property in the browser console):

await t.switchToWindow(f => f.url.host === 'edition.cnn.com').closeWindow();

Alternatively, you can use t.getCurrentWindow instead of the predicate:

await t.click("#open");

const cnnWindow = await t.getCurrentWindow();

await t.closeWindow(cnnWindow);
  • Related