【问题标题】:Testcafe close windowTestcafe 关闭窗口
【发布时间】:2022-01-13 20:01:47
【问题描述】:

我有一个可以打开新窗口的简单页面。

<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>

我也有这个测试:

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();
});

当尝试根据谓词查找窗口时,测试失败

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)

我不明白我应该怎么做才能获得从 js 代码打开的窗口。

如何获取窗口并检查其中的一些元素?

【问题讨论】:

    标签: javascript testing automated-tests e2e-testing testcafe


    【解决方案1】:

    我们已修复与“多个浏览器窗口”模式相关的问题。请安装当前的 alpha 版本 (npm i testcafe@alpha / npm i testcafe@1.17.2-alpha.2)。此外,您需要修复谓词的 host 值。这个 CNN 页面的实际主机名是 edition.cnn.com(您可以通过在浏览器控制台中观察 window.location.hostname 属性来查看不使用 TestCafe 的主机名):

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

    或者,您可以使用t.getCurrentWindow 代替谓词:

    await t.click("#open");
    
    const cnnWindow = await t.getCurrentWindow();
    
    await t.closeWindow(cnnWindow);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多