Selenium 当我运行无头(Chrome)量角器测试时,如何处理二级浏览器选项卡中的页面加载?
当此代码单击“打印”图标时,将生成一个PDF文件并显示在新的浏览器选项卡中。我想切换到此选项卡,等待PDF在那里完成加载,检查URL的一部分,然后关闭该辅助选项卡Selenium 当我运行无头(Chrome)量角器测试时,如何处理二级浏览器选项卡中的页面加载?,selenium,google-chrome,protractor,automated-tests,headless,Selenium,Google Chrome,Protractor,Automated Tests,Headless,当此代码单击“打印”图标时,将生成一个PDF文件并显示在新的浏览器选项卡中。我想切换到此选项卡,等待PDF在那里完成加载,检查URL的一部分,然后关闭该辅助选项卡 it( "should open document as PDF-file in new browser-tab", async () => { const mUrl = "TherapyReportForm/Export"; await mTherapyReportVi
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 headless模式下运行它,否则测试运行在第二行中断
expect(await browser.getCurrentUrl()).toContain(mUrl);
控制台输出证明它切换到辅助选项卡,但显然从未尝试加载url。由于无法关闭此辅助选项卡,因此整个套件将在此时中断
我做错了什么?这里有一件事。。。下载功能在无头chrome中不可用。那是肯定的。下面我要说的是,我有点不确定是不是真的 在浏览器中没有“打开”pdf文件这样的东西。原因是,在幕后,浏览器实际上下载了它(可能是暂时的)。这就是为什么你永远无法在无头的情况下做到这一点
但这是一种冒险基于我所说的都是事实的假设,尝试用谷歌搜索更多信息