如何在Java项目的资源目录中下载文件
我有一个场景,我点击下载链接在我的应用程序,它下载我的本地驱动器的Excel表。 我正在使用seleniumwebdriver自动化和validation,如果文件实际上已下载。 我能够下载文件并validation其在本地文件系统中的存在。 但是,如何在我的项目的资源目录中制作webdriver下载文件。
这就是我现在正在做的事情:
System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver_Path_Win); HashMap<String, Object> chromePrefs = new HashMap<String, Object>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", "C:\\pixeldownload"); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("prefs", chromePrefs); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, options); driver = new ChromeDriver(cap);
现在我硬编码“C:\ pixeldownload”,但我想把文件放到我的Java项目中的“资源”文件夹。
chromePrefs.put(“download.default_directory”,“资源”)。 把目的地作为“资源”并不能帮助我。 我如何下载“资源”目录下的文件。
public boolean isFileDownloaded(String downloadPath,String fileName) { boolean flag = false; File dir = new File(System.getProperty("user.dir") + "\\resources\\downloads"); File[] dir_contents = dir.listFiles(); for (int i = 0; i < dir_contents.length; i++) { if (dir_contents[0].getName().contains(fileName)) return flag=true; } return flag; }
如果你想下载文件到你的testing资源目录,试试这个:
String downloadDir = System.getProperty("user.dir") + "\\src\\test\\resources"; chromePrefs.put("download.default_directory", downloadDir);
我在我的项目中使用这个。
- 想要Excel中的VBA读取非常大的CSV并创buildCSV的一小部分的输出文件
- 根据电子表格select页面上的值 – Java – Selenium Webdriver
- 如何传递和循环,一个不确定的数组在VBA中的函数?
- Excel删除空行
- 有没有办法阻止在Outlook自动解决?
- 有没有办法用C#创build/读取LibreOffice Spreadsheet就像MS Office Excel?
- 任何方式来改变标题行属性使用CsvHelper?
- 检查excel单元格是否被locking的方法是什么?
- 无法将string转换为VBA中的整数或Excel公式(即使string都是数字,也无法转换或input错误的数字)