Testing 将焦点切换到弹出窗口并拍摄屏幕截图

Testing 将焦点切换到弹出窗口并拍摄屏幕截图,testing,popup,selenium-webdriver,screenshot,Testing,Popup,Selenium Webdriver,Screenshot,我正在用selenium 2编写一个测试脚本,其中包含一个弹出窗口的截图。弹出窗口为pdf格式 单击链接后,我正在使用代码 try { File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png")); } catch (IOException e) { e.printStac

我正在用selenium 2编写一个测试脚本,其中包含一个弹出窗口的截图。弹出窗口为pdf格式

单击链接后,我正在使用代码

try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
} catch (IOException e) {
    e.printStackTrace();
}
}

但是,要拍摄屏幕截图,它只拍摄主页而不是弹出窗口。有没有办法让selenium 2将焦点切换到新的弹出窗口,拍摄屏幕截图,然后关闭弹出窗口并切换回主窗口?

您必须使用以下方式切换驱动程序的焦点:

String mainWindow = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(mainWindow)) {
        driver.switchTo().window(handle)
        //put your screenshot call here
        driver.close();
        driver.switchTo().window(mainWindow);
    }
}
当然,如果你有更多的窗口,这将截图所有其他窗口。然后你需要知道确切的窗口句柄,然后切换到那个