【问题标题】:How to close print windows dialog in Protractor Test如何在量角器测试中关闭打印窗口对话框
【发布时间】:2018-03-20 20:40:56
【问题描述】:

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

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

编辑

我试过这样的..

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

但是它没有为打印对话框打开一个新标签。在同一个标​​签中打开打印对话框。

【问题讨论】:

  • 如果这个pdf在另一个窗口打开,你只需要切换那个窗口并以browser.switchTo().window(browser.getAllWindowHandles()[1]).close();...关闭
  • 您可以查看已编辑的帖子。Saurabh请帮忙。
  • 请给一个完整的答案..我没有得到..Saurabh
  • Have a look here知道如何切换窗口
  • 是一个窗口还是多个??

标签: javascript jquery angular protractor


【解决方案1】:

您需要将转义发送到右侧窗口。还要等待窗口打开,然后再发送。您可能还需要切换回句柄[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])
            });
        });
    });
});

【讨论】:

  • 感谢您的尝试。 Barteld 我已经尝试过像上面那样。它无法打开打印对话框的第二个窗口。看起来 browser.sleep 在这里不起作用
  • 你的函数是打开一个新标签,还是只打开一个打印对话框?如果您的打印选项从新选项卡打开对话框,您只需要切换窗口。否则你只需要等待对话框打开。
  • 不,它不会打开新选项卡。它只会打开一个打印对话框Barteld 单击打印按钮后,打印对话框打开得很快,就像没有浏览器一样。睡眠工作,因此 printDialog 以相同的方式打开窗户。
  • 我不确定您所说的“打印对话框打开得如此之快以至于没有 browser.sleep 工作”是什么意思。 browser.sleep 是延迟转义键的发送。不要拖延对话。量角器浏览器类无法控制对话框。
  • 我的意思是,我像上面一样尝试过,但没有为打印对话框过程创建新选项卡。 Barteld。此代码还会在同一选项卡中打开打印对话框。
【解决方案2】:

如下设置能力;这将禁用从 chrome 浏览器弹出的打印预览

能力:{ “浏览器名称”:“铬”, 铬选项:{ args: ['--disable-print-preview','start-maximized'] }

},

【讨论】:

    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多