我使用selenium webdriver来自动执行单击下载图像打开新窗口的场景。这个新窗口将有"另存为"对话框。
只要我点击图像,新窗口打开,但立即关闭,不显示"另存为"对话框。
我的场景是确保文件存在,并通过单击另存为按钮读取文件名。
我代码:WebElement e1= driver.findElement(By.id("id of image"));
e1.click();
Set<String> set = driver.getWindowHandles();
List<String> handles = new ArrayList<String>(set);
driver.switchTo().window(handles.get(1)); // switch to file download dialog box
根据我的经验,Seleniun文件下载测试对自动化工程师来说是一件痛苦的事情。我们可以使用HttpURLConnection, Apache HttpComponents(或者可能只是通过URL获取文件)来指定链接,并断言200 OK响应,而不是一步一步地下载。
有关更多信息,您可以参考下面的链接,以获取处理这种情况的工具
https://github.com/Ardesco/Ebselen/blob/master/ebselen-core/src/main/java/com/lazerycode/ebselen/customhandlers/FileDownloader.java http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/