javascript - 如何关闭 Protractor Test 中的打印窗口对话框

标签 javascript jquery angular protractor

我正在使用 Protractor 进行端到端测试。在某个测试中,我需要测试打印按钮是否正在创建pdf。因此,当 Test 单击该按钮时,它会打开打印窗口对话框,如下所示。

enter image description here

现在这个测试还无法完成。因为这个打印窗口。我的问题是如何关闭 Protractor 中的打印对话框?因此,其余的测试都将悬而未决。请帮忙。提前致谢。

编辑

我已经尝试过这样的..

var printButton=element(by.css('[class="print"]'));
        /* This print button should be present first*/
        expect(printButton.isPresent()).toBe(true);

        browser.actions().mouseMove(printButton).perform();
        printButton.click().then(function () {
             // fill in the form here
             browser.sleep(2000);
            // For Pressing Escape key
            browser.actions().sendKeys(protractor.Key.ESC).perform();

        });

我想如果我成功按下退出键,那么它就会解决问题。但没有成功。

下一个编辑-- 我已经尝试过新的 Windows 更改,如下所示

printButton.click().then(function () {
             // fill in the form here

            browser.sleep(4000);
            browser.getAllWindowHandles().then(function(handles){
                browser.switchTo().window(handles[1]).then(function(){
                    //do your stuff on the pop up window

                    browser.driver.close();
                    browser.switchTo().window(handles[0]);
                });
            });

        });

但它在控制台中显示错误,实际上它没有打开任何窗口。并像以前一样卡在打印对话框上。

 Failed: unknown error: failed to close window in 20 seconds

编辑3

我在 Angular JS 中遇到这个问题,而不是在 Java 中。

编辑 4 我最后的尝试

printButton.click().then(function () {
             // fill in the form here
            return browser.getAllWindowHandles().then(function (handles) {
                var newWindowHandle = handles[1]; // this is your new window
                return browser.switchTo().window(newWindowHandle).then(function () {
                    return browser.sleep(5000).then(function () {
                        return browser.actions().sendKeys(protractor.Key.ESCAPE).perform().then(function () {
                            return browser.switchTo().window(handles[0])
                        });
                    });
                });
            });

但它不会打开打印对话框的新选项卡..在同一选项卡中打开打印对话框。

最佳答案

您需要将转义发送到右侧窗口。发送之前还要等待窗口打开。您可能还需要切换回handles[0]。

return browser.getAllWindowHandles().then(function (handles) {
    var newWindowHandle = handles[1]; // this is your new window
    return browser.switchTo().window(newWindowHandle).then(function () {
        return browser.sleep(5000).then(function () {
            return browser.actions().sendKeys(protractor.Key.ESCAPE).perform().then(function () {
                return browser.switchTo().window(handles[0])
            });
        });
    });
});

关于javascript - 如何关闭 Protractor Test 中的打印窗口对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45939646/

相关文章:

javascript - 在 Chart.js v2.x 中放置 "multiTooltipTemplate"的位置

javascript - 将 ES5 JavaScript 用于 Angular 2 应用程序和使用 TypeScript 的优缺点是什么?

Angular Material slider - 显示最小和最大标签

javascript - 无法从 jquery 访问数组

javascript - 函数中多次返回会导致返回值错误吗?

javascript - 错误 : Cyclic dependency, 节点为 :"dateA"

javascript - 在ajax调用后更改div内的行高

javascript - Bootstrap slider 始终返回 "10"

jquery - 使用 jQuery 控制 CSS 转换?

javascript - 通过装饰器执行方法