【问题标题】:How do I deal with a page loading in a secondary browser tab when I run Protractor tests headless (Chrome)?当我运行 Protractor 无头测试 (Chrome) 时,如何处理辅助浏览器选项卡中的页面加载?
【发布时间】:2020-10-19 16:02:59
【问题描述】:

当此代码单击“打印”图标时,将生成 PDF 文件并显示在新的浏览器选项卡中。我想切换到此选项卡,等到 PDF 在那里完成加载,检查 URL 的一部分,然后关闭该辅助选项卡。

it( "should open document as PDF-file in new browser-tab", async () => {
    const mUrl = "TherapyReportForm/Export";

    await mTherapyReportView.btnPrintform.click();
    await browser.getAllWindowHandles().then(async (handles) => {
        //if there is a secondary browser-tab open...
        if (handles.length > 1) {
            //...click on it
            await browser.driver.switchTo().window(handles[1]);
        }
    });

    //confirm that the url of the secondary tab matches the print-url pattern
    await browser.sleep( 18000 );
    expect( await browser.getCurrentUrl() ).toContain( mUrl );

    await browser.getAllWindowHandles().then( async (handles) => {
        //if there are multiple browser-tabs open
        if (handles.length > 1) {
            //close the secondary and move back to first
            await browser.driver.close();
            await browser.driver.switchTo().window( handles[0] );
        }
    } );
} );

上面的测试工作可靠,除非我在 chromes 无头模式下运行它,否则测试运行会中断

expect(await browser.getCurrentUrl()).toContain(mUrl);

console output

控制台输出证明它切换到辅助选项卡,但显然从未尝试加载 url。由于它无法关闭此辅助选项卡,因此整个套件将在那时中断。

我做错了什么?

【问题讨论】:

    标签: selenium google-chrome protractor automated-tests headless


    【解决方案1】:

    这是一件事……下载功能在 headless chrome 中不可用。这是肯定的。下面我要说的,我有点不确定是不是这样

    在浏览器中没有“打开”pdf文件之类的东西。原因是浏览器在后台实际下载它(可能是暂时的)。这就是为什么你永远无法在 headless 中做到这一点

    但这只是在黑暗中拍摄

    【讨论】:

    • 假设我说的是真的,试着用谷歌搜索更多信息
    猜你喜欢
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 2014-12-28
    • 2018-03-06
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多